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

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.

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():
    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
    subscribe:
      operationId: my_api.handlers.user_signedup
      message:
        $ref: '#/components/messages/UserSignedUp'
components:
  messages:
    UserSignedUp:
      payload:
        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",
    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. Without Asynction, one would need to add additional boilerplate to register the handlers (as shown here).

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 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 namespaced (/) is assumed.

TODOs

  1. Payload validation
  2. Increase JSON Schema reference resolution test coverage. Allow refs to be used with other keys. Merge upon ref resolution.
  3. Authentication

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.2.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

asynction-0.0.2-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

Hashes for asynction-0.0.2.tar.gz
Algorithm Hash digest
SHA256 50c41ed83384743acdb6acea169988964d630e67a960a8116728b858d40ef7b5
MD5 e6ae90d22507f94fd5ef53bc01c7ca0e
BLAKE2b-256 f37c50eb76f16f51688466e2fcb12bd16456e9480b702e5f05b281275b989581

See more details on using hashes here.

File details

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

File metadata

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

Hashes for asynction-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ddbd69d68c7a5fbdda0fed06726f7fd322cda7a9e38b682c80ae4f350985f868
MD5 f8eba622b70e4f24a26a517c67a0e761
BLAKE2b-256 0c1979a2e08bf24fbd6791a76ba6fa7279f068793a7a80d69d4e1432ea893157

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