https://github.com/stanwood/gae-webapp2-utils
Set of utilities modules for webapp2 framework on GAE: http://webapp2.readthedocs.io/en/latest/
https://github.com/stanwood/gae-webapp2-utils
google-app-engine pytest python27 webapp2
Last synced: 4 months ago
JSON representation
Set of utilities modules for webapp2 framework on GAE: http://webapp2.readthedocs.io/en/latest/
- Host: GitHub
- URL: https://github.com/stanwood/gae-webapp2-utils
- Owner: stanwood
- License: mit
- Created: 2018-07-31T11:38:10.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-08-01T07:14:15.000Z (about 7 years ago)
- Last Synced: 2025-02-26T15:34:20.321Z (8 months ago)
- Topics: google-app-engine, pytest, python27, webapp2
- Language: Python
- Homepage:
- Size: 22.5 KB
- Stars: 2
- Watchers: 8
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GAE Webapp2 utils
## About
Helper module for most common features used in webapp2 and Google App Engine.
## Install
Add folder as a git submodule:
```
git submodule add git:...
```Add submodule folder to the `appengine_config.py`:
```python
import osfrom google.appengine.ext import vendor
vendor.add(os.path.join(os.path.dirname(__file__), 'path/to/submodule/webapp2_utils'))
```## Usage
### Handlers
Create simple webapp2 handler which returns JSON body with CORS and Public Cache
```python
from webapp2_utils.handlers import base
from webapp2_utils.handlers.mixins import cache
from webapp2_utils.handlers.mixins import corsclass SampleHandler(base.BaseHandler, cors.HandlerMixin, cache.PublicCachingMixin):
def get(self):
self.json_response({'msg': 'My sample response in json'})
```---
Create simple webapp2 handler which downloads file from Google Cloud Storage
```python
from webapp2_utils.handlers import base
from webapp2_utils.handlers.mixins import gcsclass BlobHandler(base.BaseHandler, gcs.CloudStorageMixin):
def get(self):
file_name = self.request.GET.get('file_name')
blob = self.bucket.get_blob(file_name)
if not blob:
self.abort(404, 'File does not exist on GCS')
self.json_response({'file_url': blob.generate_signed_url()})
```---
Create secured handler with auth token
```python
from webapp2_utils.handlers import base
from webapp2_utils.handlers import decoratorsclass SecuredHandler(base.BaseHandler):
@decorators.token_required('my-auth-token')
def get(self):
self.json_response({'msg': 'Success auth'})
```---
Create internationalized handler
```python
from webapp2_extras.i18n import gettext as _from webapp2_utils.handlers import base
from webapp2_utils.handlers.mixins import i18n as i18n_mixinclass TranslatedHandler(base.BaseHandler, i18n_mixin.I18nRequestHandler):
def get(self):
self.json_response({'msg': _('This is my translated text')})
```## Pytest
Test webapp2 handler
```python
import pytest
import webtest@pytest.fixture
def app(testbed):
from main import app
return webtest.TestApp(app)
from webapp2_utils.pytest.appengine_fixtures import *
def test_handler(app):
response = app.get('/my/url')
assert response.status_code == 200
```Using `freezegun` in Google App Engine test config
```python
import datetimeimport freezegun
from webapp2_utils.handlers import base
class DateHandler(base.BaseHandler):
"""Routed as /date url"""
def get(self):
self.json_response({'date': datetime.datetime.now().date().isoformat()})from webapp2_utils.pytest.appengine_fixtures import * # import it in conftest
@freezegun.freeze_time('2018-08-08')
def test_date_handler(app, freezegun_patch):
response = app.get('/date')
assert response.json['date'] == '2018-08-08'
```Testing `taskqueues` in Google App Engine
```python
import webapp2
from google.appengine.api import taskqueuefrom webapp2_utils.handlers import base
class TaskHandler(base.BaseHandler):
@webapp2.cached_property
def queue(self):
return taskqueue.Queue('sampleTask')def get(self):
url = '/_ah/queue/sample/task'
self.queue.add(taskqueue.Task(url=url))from webapp2_utils.pytest.appengine_fixtures import * # import it in conftest
def test_task_handler(app, taskqueue):
app.get('/task')
tasks = taskqueue.GetTasks(queue_name='queueName')assert len(tasks) == 1
assert tasks[0]['url'] == '/_ah/queue/sample/task'
```## ndb
Create base model with created and updated fields
```python
from google.appengine.ext import ndbfrom webapp2_utils.ndb.models.base import Model
class MyModel(Model):
new_field = ndb.StringProperty()
```