Skip to main content

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:

  1. OS Environment Variable PUBSUB_DECORATOR_API_KEY
  2. Flask App Config PUBSUB_DECORATOR_API_KEY
  3. 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

Metadata

Release files for Flask-PubSubDecorator 1.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Flask-PubSubDecorator 1.1.6
File Size Uploaded
Flask-PubSubDecorator-1.1.6.tar.gz 5.5 kB Details

Release files / Flask-PubSubDecorator-1.1.6.tar.gz

Download URL Flask-PubSubDecorator-1.1.6.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ce05c5f1495c7f6b9bc8c04257cc6b12a00cf3f42975bd16c2501155b7522deb
BLAKE2b-256 checksum
How to use checksums
346efb14f6403aa8f3cb70873522fb1355ecc5a3d1cfbf87c6fbc91285edfdd6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.1.6 This release

1 release file

1.1.5

1 release file

1.1.4

1 release file

1.1.3

1 release file

1.1.2

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.9

1 release file

1.0.8

1 release file

1.0.7

1 release file

1.0.6

1 release file

1.0.5

1 release file

1.0.4

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page