Skip to main content

SocketIO framework driven by the AsyncAPI specification. Built on top of Flask-SocketIO. Inspired by Connexion.

Project description

Asynction

Tests Status codecov

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 located at ./my_api/handlers.py:

def user_sign_up(data):
    logger.info("Signing up user")

def user_log_in(data):
    logger.info("Logging in user")

def user_error(e):
    logger.error("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-handers:  # Default namespace handlers (such as connect, disconnect and error)
      error: my_api.handlers.user_error  # Equivelant of: `@socketio.on_error("/user")

components:
  messages:
    UserSignUp:
      name: sign up  # The SocketIO event name
      payload:  # Asynction uses payload JSON Schemata for message validation
        type: object
      x-hander: 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-hander: my_api.handlers.user_log_in

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

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

TODOs

  1. Binding validation
  2. Authentication à la https://connexion.readthedocs.io/en/latest/security.html
  3. Expose spec via a flask route. Provide a playground.
  4. Increase JSON Schema reference resolution test coverage. Allow refs to be used together with other keys. Merge upon ref resolution.

Limitations / Thoughts

  1. How can the spec express event handler return types (that are to be passed as args to the client callbacks)?

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asynction-0.0.7.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

asynction-0.0.7-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file asynction-0.0.7.tar.gz.

File metadata

  • Download URL: asynction-0.0.7.tar.gz
  • Upload date:
  • Size: 6.8 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

Hashes for asynction-0.0.7.tar.gz
Algorithm Hash digest
SHA256 c06d3eb668e388f1751189b2e44f3b005eae5515444fd460ea1128a4c9bd1153
MD5 bed0dd8514bb9d271699cca11c814457
BLAKE2b-256 ac12931a32198f3567fdc4dbfbf69648d28852e1b07d6e89c8329d9767bc9806

See more details on using hashes here.

File details

Details for the file asynction-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: asynction-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 8.3 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

Hashes for asynction-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 c3df366f6597f6cfabfcbd3d4416f43bfa0f567fe190ce5b0710ebae67d16f13
MD5 df5093d7fee8ddf89f6896328ca78284
BLAKE2b-256 d8f3e4ebc58a23249700c7bd0ba5a34cc5c1c4b6957c74768f66b832624a27bb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page