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.
- HTTP request validation, upon connection, based on the channel binding schemata within the API specification.
- Automatic registration of all event and error handlers defined within the API specification.
- AsyncAPI playground (coming soon)
- Authentication à la Connexion (coming soon)
Prerequisites
- Python 3.7 (or higher)
Install
$ pip install asynction
Usage
Example event and error handler callables located at ./my_api/handlers.py
:
# /user namespace
def user_sign_up(data):
logger.info("Signing up user...")
emit("metrics", "signup", namespace="/admin")
def user_log_in(data):
logger.info("Logging in user...")
emit("metrics", "login", namespace="/admin")
def user_error(e):
logger.error("Error: %s", e)
# /admin namespace
def authenticated_connect():
token = request.args["token"]
def admin_error(e):
logger.error("Admin error: %s", e)
Example specification located at ./docs/asyncapi.yaml
:
asyncapi: 2.0.0
info:
title: User Account Service
version: 1.0.0
description: This service is in charge of processing user accounts
channels:
/user: # A channel is essentially a SocketIO namespace
publish:
message:
oneOf: # The oneOf Messages relationship expresses the supported events that a client may emit under the `/user` namespace
- $ref: '#/components/messages/UserSignUp'
- $ref: '#/components/messages/UserLogIn'
x-handlers: # Default namespace handlers (such as connect, disconnect and error)
error: my_api.handlers.user_error # Equivelant of: `@socketio.on_error("/user")
/admin:
subscribe:
message:
oneOf:
- '#/components/messages/Metrics'
x-handlers:
connect: my_api.handlers.authenticated_connect # Equivelant of: `@socketio.on("connect", namespace="/admin")
error: my_api.handlers.admin_error
bindings:
ws: # Bindings are used to validate the HTTP request upon connection
query:
type: object
properties:
token:
type: string
required: [token]
components:
messages:
UserSignUp:
name: sign up # The SocketIO event name. Use `message` or `json` for unnamed events.
payload: # Asynction uses payload JSON Schemata for message validation
type: object
x-handler: my_api.handlers.user_sign_up # The handler that is to be registered. Equivelant of: `@socketio.on("sign up", namespace="/user")
UserLogIn:
name: log in
payload:
type: object
x-handler: my_api.handlers.user_log_in
Metrics:
name: metrics
payload:
type: string
enum: [signup, login]
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 and the channel bindings 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
Asynction has extended the AsyncAPI 2.0.0 specification to provide support for coupling SocketIO entities (such as namespaces and events) to python objects (handlers). The extentions introduced adhere to the Specification Extention guidelines of the AsyncAPI spec.
Event handler
The x-handler
field MAY be defined as an additional property of the Message Object. The value of this field MUST be of string
type, expressing a dot joint path to a python callable (the event handler).
Message Objects listed under a subscribe
operation MUST include the x-handler
field.
Message Objects listed under a publish
operation SHOULD NOT include the x-handler
field.
Default namespace handlers
The x-handlers
field MAY be defined as an additional property of the Channel Item Object. The value of this field SHOULD be a Channel Handlers Object.
Channel Handlers Object
Field Name | Type | Description |
---|---|---|
connect | string |
Dot joint path to the python connect handler callable |
disconnect | string |
Dot joint path to the python disconnect handler callable |
error | string |
Dot joint path to the python error handler callable |
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.8.tar.gz
.
File metadata
- Download URL: asynction-0.0.8.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ccbc5b84859df04824e615a74840d7bc1c300f6d865233e9d4fbb63fdf5025f |
|
MD5 | 6c977ae717d0ce5fb422fd9beabf1086 |
|
BLAKE2b-256 | 9df10dc2e9d6f5da67ab20354e4193554db1f2b401c77d6822b49690dae47704 |
File details
Details for the file asynction-0.0.8-py3-none-any.whl
.
File metadata
- Download URL: asynction-0.0.8-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 031df5d7fe7b31bc00f027333426925560ab5bb1e2f01b6845414bbe31712684 |
|
MD5 | 13aaaee186c030b18108d6e6f512913b |
|
BLAKE2b-256 | a96bd7a7bf319ddfa584ba471c5917b2f8fd825af322c5508377bc9fa0b80622 |