asyncapi-eventrouter
Work in Progress
Write Python code for Event-Driven Architectures! The asyncapi-eventrouter
prototype library creates Websocket, PubSub, and other asynchronous frameworks with message validation and automatic schema documentation in the AsyncAPI specification. It's heavily inspired by how FastAPI documents REST endpoints in the OpenAPI specification.
Example
What would the Streetlights API look like in Python code with asyncapi-eventrouter
?
# asyncapi.py
from pydantic import BaseModel, Field
from datetime import datetime
from asyncapi_eventrouter import Application
asyncapi_app = Application()
class LightMeasured(BaseModel):
id: int = Field(..., gte=0, description="ID of the streetlight.")
lumens: int = Field(..., gte=0, description="Light intensity measured in lumens.")
sentAt: datetime = Field(..., description="Date and time when the message was sent.")
@asyncapi_app.subscribe(channel_name="light/measured",
event_name="LightMeasured")
async def record_measurement(measurement: LightMeasured):
# record to db or take some other action
return {'received': datetime.now()}
# main.py
from fastapi import FastAPI, WebSocket
from .asyncapi import asyncapi_app
app = FastAPI()
@app.websocket('/ws')
async def ws(websocket: Websocket):
await ws.accept()
while True:
while True:
content = await websocket.receive_text()
response = asyncapi_app.process(content)
await websocket.send_json(response)
@app.get('/ws-schema')
async def ws_schema():
return asyncapi_app.schema()
Development
This project uses poetry and pre-commit for development.
poetry env use 3.9
will create a.venv
directory in yourasyncapi-eventrouter
directory.poetry install
will installasyncapi-eventrouter
and all dependencies into that virtual environment.pre-commit run --all-files
will show you what will be executed any time yougit commit
.
Skip Pre-commit
If the pre-commit
hooks can not be easily resolved, you can still commit using git commit --no-verify
.
Inspiration and gratitude
The useful FastAPI project inspires this project. Any code snippets from FastAPI are given credit and attribution in the source code. We are thankful to the FastAPI community for their work.