SocketIO framework driven by the AsyncAPI specification. Built on top of Flask-SocketIO. Inspired by Connexion.
Project description
Asynction
SocketIO python framework driven by the AsyncAPI specification. Built on top of Flask-SocketIO. Inspired by Connexion.
The purpose of Asynction is to empower a specification first approach when developing SocketIO APIs in Python.
Disclaimer: Asynction is still at a very early stage and should not be used in production codebases.
Features
- Payload validation (for both incoming and outgoing events), based on the message schemata within the API specification.
- Automatic registration of all event and error handlers defined within the API specification.
Prerequisites
- Python 3.7 (or higher)
Install
$ pip install asynction
Usage
Example event and error handler callables sitting under ./my_api/handlers.py
:
def user_signedup(message):
logger.info("Registered user")
def user_error(e):
logger.error("Error: %s", e)
Example specification sitting under ./docs/asyncapi.yaml
:
asyncapi: 2.0.0
info:
title: Account Service
version: 1.0.0
description: This service is in charge of processing user signups
channels:
user/signedup: # A namespace can be specified by prefixing the channel name
publish:
operationId: my_api.handlers.user_signedup
message:
$ref: '#/components/messages/UserSignedUp'
components:
messages:
UserSignedUp:
payload: # Asynction uses payload JSON Schemata for message validation
type: object
x-namespaces:
/user:
description: Special namespace that only registered users have access to
errorHandler: my_api.handlers.user_error
Bootstrap the AsynctionSocketIO server:
from asynction import AsynctionSocketIO
from flask import Flask
flask_app = Flask(__name__)
asio = AsynctionSocketIO.from_spec(
spec_path="./docs/asyncapi.yaml",
app=flask_app,
message_queue="redis://",
# any other kwarg that the flask_socketio.SocketIO constructor accepts
)
The AsynctionSocketIO
class extends the SocketIO
class of the Flask-SocketIO library.
The above asio
server object has all the event and error handlers registered, and is ready to run.
Validation of the message payloads is also enabled by default.
Without Asynction, one would need to add additional boilerplate to register the handlers (as shown here) and implement the respective validators.
Specification Extentions (support for SocketIO Namespaces)
Asynction has extended the AsyncAPI 2.0.0 specification to provide support for the Namespaces concept of the SocketIO protocol. The extentions introduced adhere to the Specification Extention guidelines of the AsyncAPI spec.
Namespace definition (object)
An x-namespaces
field has been defined as a top level key of the AsyncAPI object. The value of this field is a Namespace Definitions Object. The Namespace Definitions Object is a map object (with patterned fields).
Namespace Definitions Object
Field Pattern | Type | Description |
---|---|---|
^[A-Za-z0-9_\-]+/$ |
Namespace Item Object | Each key must correspond to a namespace supported by the SocketIO server. Each value is a Namespace Item Object, providing the definition of that namespace. |
Namespace Item Object
Field Name | Type | Description |
---|---|---|
description | string |
An optional description of this namespace |
errorHandler | string |
Dot joint path to the python error handler callable |
Event handler namespacing (semantic)
A new semantic added to the AsyncAPI 2.0.0 spec is the prefixing of the channel paths (keys of the Channels Object). This allows the registration of an event handler or a message validator under a particular namespace. The prefix expressed namespace should be included in the Namespace Definitions Object.
The pattern of the channel path is: ^(?<namespace>[A-Za-z0-9_\-/]+/)?(?<channel_name>[A-Za-z0-9_\-]+)$
If the namespace prefix is omitted, the main namespace (/
) is assumed.
TODOs
- Split the functionality into multiple atomic modules.
- Binding validation (query params and headers)
- Unnamed events (
json
andmessage
- see the Flask-SocketIO docs) - Authentication
- Expose spec via a flask route. Provide a playground.
- Increase JSON Schema reference resolution test coverage. Allow refs to be used with other keys. Merge upon ref resolution.
Limitations / Thoughts
- How can the spec express event handler return types (that are to be passed as args to the client callbacks)?
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
Built Distribution
File details
Details for the file asynction-0.0.3.tar.gz
.
File metadata
- Download URL: asynction-0.0.3.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d2f2d7fd8f7bf5107b9f39fb07db5ed61703d01007de70bd467a45663e0e403 |
|
MD5 | 6354e258e45c52040d8e4f1ac12a3cc4 |
|
BLAKE2b-256 | 243cf57abe5290ed89021227bff08378c9263d65e8e6bbda1e4af476e9af3594 |
File details
Details for the file asynction-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: asynction-0.0.3-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c85901e8a5975ee88ba60eb1feff838abb8e02b48788849dafd46c9c77665e62 |
|
MD5 | c934cd26d8b40732210f6821781d2f6f |
|
BLAKE2b-256 | c2130095d6dfe261910191e05d3c0b6ee5cba404134509463ce1d240e487e253 |