Decorates publisher functions and subscriber routes creating topics/subscriptions if necessary
Project description
Flask-PubSubDecorator
Decorates publisher functions and subscriber routes creating topics/subscriptions if necessary.
Installation
Add this line to your application's requirements.txt
Flask-PubSubDecorator
And then execute:
$ pip install -r requirements.txt
Or install it yourself as:
$ pip install Flask-PubSubDecorator
Usage
Using PubSubDecorator is dead simple. First set your GOOGLE_APPLICATION_CREDENTIALS environment variable to point at a valid JSON creds file.
$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/creds.json
The following snippet should get you coding
from flask import Flask, request
from PubSubDecorator import PubSubDecorator
import base64
import json
app = Flask(__name__)
# blueprint can optionally be passed in for registering subscribers in a blueprint endpoint
app.pubsub = PubSubDecorator(app)
# publisher decorator will inject publisher client and topic path
@app.pubsub.publisher(topic='user_confirmed')
def user_confirmed(publisher, topic, user):
publisher.publish(topic, data=json.dumps({
'user_id': user.id
}))
# subscriber decorator will register the Flask route, prefixing /_ah/push-handlers
# subscriber decorator will parse and inject pubsub message
@app.pubsub.subscriber(
subscription='process_user_confirmation',
topic='user_confirmed',
route='/process_user_confirmation',
methods=['POST']
)
def process_user_confirmation(message, *args, **kwargs):
try:
user_id = message.get('user_id')
# do some async work here!
except Exception:
_logger.exception(
'An unexpected error occurred processing subscription "{0}": {1}'.format(
kwargs.get('__subscription__'), request.data
)
)
# Unexpected failure, do not ack message
return '', 422
return '', 200
Security
PubSub push subscriptions are inherently public facing and should therefore be secured. Googles recommended solution is to attach a secret key to a registered subscription pushEndpoint. PubSubDecorator handles this for you if any of the following is provided:
- OS Environment Variable
PUBSUB_DECORATOR_API_KEY
- Flask App Config
PUBSUB_DECORATOR_API_KEY
- api_key is passed into PubSubDecorator constructor.
Best Practice: encrypt your key with GCloud KMS, store it in GCloud Datastore, and export to OS environment variable at runtime.
Logging
Decorators will log to 'flask-pubsub-decorator' namespace.
import sys
import logging
import logging.handlers
logger = logging.getLogger('flask-pubsub-decorator')
logger.setLevel(logging.DEBUG)
log_file = logging.handlers.RotatingFileHandler(
'log_file_name.log', maxBytes=5 * 1024 * 1024, backupCount=10
)
logger.addHandler(log_file)
Testing
$ pytest -s tests.py
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for Flask-PubSubDecorator-1.1.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92b782db81637e12ea14a4d6a48c33e46a44e4ae10082ab53945d7e92a8690a4 |
|
MD5 | 2885825d7db62c3fa6f7cc2eff151533 |
|
BLAKE2b-256 | c9ad34ae079645154397022e5d3bbed8cd43764a5c8129f73f4bc81918f5ab6e |