Package: asyncapi
Python AsyncAPIPython library for translateasyncapispecification to python code, without code generation.AsyncAPI Pattern:https://asyncapi.ioDocumentation:https://dutradda.github.io/asyncapi-python/Source Code:https://github.com/dutradda/asyncapi-pythonKey FeaturesReads an asyncapi specification and create publishers and subscribers from itSupport for specification declaration with dataclassesProvides application for create subscribersSupport for kafka, redis and postgres protocols (same as broadcaster library)Extra support for google cloud pubsub serviceExpose in http the auto-generated specificationRequirementsPython 3.8+broadcasterjsondaorarequests (Optional for http specification)typer (Optional for subscriber application)pyyaml (Optional for yaml specification)apidaora (Optional for expose specification)Package extra installs:httpyamlkafkaredispostgressubscriberdocsgoogle-cloud-pubsubInstallation$ pip install asyncapi[http,yaml,redis,subscriber,docs]YAML Specification Exampleasyncapi:2.0.0info:title:User APIversion:'1.0.0'description:API to manage usersservers:development:url:localhostprotocol:redisdescription:Development Broker Serverchannels:user/update:description:Topic for user updatessubscribe:operationId:receive_user_updatemessage:$ref:'#/components/messages/UserUpdate'publish:message:$ref:'#/components/messages/UserUpdate'components:messages:UserUpdate:name:userUpdatetitle:User Updatesummary:Inform about users updatespayload:type:objectrequired:-idproperties:id:type:stringname:type:stringage:type:integerdefaultContentType:application/jsonCreating subscribers module# user_events.pyfromtypingimportAnyasyncdefreceive_user_update(message:Any)->None:print(f"Received update for user id={message.id}")Start subscriber to listen eventsPYTHONPATH=.asyncapi-subscriber\--urlapi-spec.yaml\--api-moduleuser_eventsWaiting messages...Publishing Updates# publish.pyimportasynciofromasyncapiimportbuild_apiapi=build_api('api-spec.yaml')channel_id='user/update'message=api.payload(channel_id,id='fake-user',name='Fake User',age=33)asyncdefpublish()->None:awaitapi.connect()awaitapi.publish(channel_id,message)awaitapi.disconnect()asyncio.run(publish())print(f"Published update for user={message.id}")python publish.py


Published update for user=fake-userReceive UpdatesWaiting messages...
Received update for user id=fake-userExpose Specificationasyncapi-docs--pathapi-spec.yamlcurl-ilocalhost:5000/asyncapi.yamlPython Specification Example# specification.pyimportdataclassesfromtypingimportOptionalimportasyncapi@dataclasses.dataclassclassUserUpdatePayload:id:strname:Optional[str]=Noneage:Optional[int]=Nonedev_server=asyncapi.Server(url='localhost',protocol=asyncapi.ProtocolType.REDIS,description='Development Broker Server',)message=asyncapi.Message(name='userUpdate',title='User Update',summary='Inform about users updates',payload=UserUpdatePayload,)user_update_channel=asyncapi.Channel(description='Topic for user updates',subscribe=asyncapi.Operation(operation_id='receive_user_update',message=message,),publish=asyncapi.Operation(message=message),)spec=asyncapi.Specification(info=asyncapi.Info(title='User API',version='1.0.0',description='API to manage users',),servers={'development':dev_server},channels={'user/update':user_update_channel},components=asyncapi.Components(messages={'UserUpdate':message}),)Creating subscribers module# py_spec_user_events.pyimportspecificationspec=specification.specasyncdefreceive_user_update(message:specification.UserUpdatePayload,)->None:print(f"Received update for user id={message.id}")Start subscriber to listen eventsPYTHONPATH=.asyncapi-subscriber--api-moduleuser_eventsWaiting messages...Publishing Updates# publish.pyimportasynciofromasyncapiimportbuild_api_auto_specapi=build_api_auto_spec('specification')channel_id='user/update'message=api.payload(channel_id,id='fake-user',name='Fake User',age=33)asyncdefpublish()->None:awaitapi.connect()awaitapi.publish(channel_id,message)awaitapi.disconnect()asyncio.run(publish())print(f"Published update for user={message.id}")python publish.py


Published update for user=fake-userReceive UpdatesWaiting messages...
Received update for user id=fake-userExpose SpecificationPYTHONPATH=.asyncapi-docs--api-modulespecificationcurl-ilocalhost:5000/asyncapi.yaml