def main( url: Optional[str] = typer.Option(None, envvar='ASYNCAPI_URL'), server: Optional[str] = typer.Option(None, envvar='ASYNCAPI_SERVER'), server_bindings: Optional[str] = typer.Option( None, envvar='ASYNCAPI_SERVER_BINDINGS'), api_module: str = typer.Option('', envvar='ASYNCAPI_MODULE'), republish_errors: Optional[bool] = typer.Option( None, envvar='ASYNCAPI_REPUBLISH_ERRORS'), channel: Optional[str] = typer.Option(None, envvar='ASYNCAPI_CHANNEL'), workers: int = typer.Option(1, envvar='ASYNCAPI_WORKERS'), channels_subscribes: Optional[str] = typer.Option( None, envvar='ASYNCAPI_CHANNELS_SUBSCRIBES'), republish_errors_channels: Optional[str] = typer.Option( None, envvar='ASYNCAPI_REPUBLISH_ERRORS_CHANNELS'), ) -> None: if url is None: if api_module is None: raise UrlOrModuleRequiredError() api = build_api_auto_spec( module_name=api_module, server=server, server_bindings=server_bindings, republish_errors=republish_errors, channels_subscribes=channels_subscribes, republish_errors_channels=republish_errors_channels, ) else: api = build_api( url, server=server, module_name=api_module, server_bindings=server_bindings, republish_errors=republish_errors, channels_subscribes=channels_subscribes, republish_errors_channels=republish_errors_channels, ) fork_app(workers) graceful_stop() loop = get_event_loop() start(loop, api, channel) loop.run_forever()
# publish.py import asyncio from asyncapi import build_api api = build_api('api-spec.yaml') channel_id = 'user/update' message = api.payload(channel_id, id='fake-user', name='Fake User', age=33) async def publish() -> None: await api.connect() await api.publish(channel_id, message) await api.disconnect() asyncio.run(publish()) print(f"Published update for user={message.id}")
# publish.py import asyncio from asyncapi import build_api api = build_api('http://localhost:5000/asyncapi.yaml') channel_id = 'user/update' message = api.payload(channel_id, id='fake-user', name='Fake User', age=33) async def publish() -> None: await api.connect() await api.publish(channel_id, message) await api.disconnect() asyncio.run(publish()) print(f"Published update for user={message.id}")
#! /usr/bin/env python3 import asyncio from asyncapi import build_api async def publish(channel_id, message) -> None: await api.connect() await api.publish(channel_id, message) await api.disconnect() if __name__ == "__main__": api = build_api('kafka.yml') channel_id = "one" message = api.payload(channel_id, id=00001, message='ABC')