Metadata-Version: 2.2
Name: async-py-bus
Version: 0.1.5
Summary: Async message bus framework designed for event-driven, cqrs python projects
Author-email: andrei samofalov <andrei.e.samofalov@gmail.com>
License: Copyright 2025 Andrei Samofalov
        
        Permission is hereby granted, free of charge, to any person obtaining
        a copy of this software and associated documentation files (the “Software”),
        to deal in the Software without restriction, including without limitation
        the rights to use, copy, modify, merge, publish, distribute, sublicense,
        and/or sell copies of the Software, and to permit persons to whom the Software
        is furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
        INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
        PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
        HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
        OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
        OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
        
Project-URL: guthub, https://github.com/andrei-samofalov/async-py-bus
Keywords: python,async,bus,edd,cqrs
Requires-Python: >=3.11
Description-Content-Type: text/markdown

### async-py-bus

The library is designed for async `event-driven` and `cqrs` python projects

#### Basic usage

```python
import asyncio
from collections.abc import Callable
from dataclasses import dataclass
from typing import AsyncContextManager

from pybus import dispatcher as dp, AbstractHandler


@dp.events.register('on startup')
@dp.events.register('on message received')
async def on_startup(event):
    # do some on startup
    print(f"got {event} event")


@dataclass
class Event:
    name: str
    somelogiccalue: str


class CustomHandler:

    async def __call__(self, event: Event):
        print(f'got {event} event')


@dataclass
class Command:
    dosomething: bool


@dp.commands.register(Event, session_factory=Provide['session_factory'])
class AnotherCustomHandler:

    def __init__(self, session_factory: Callable[[], AsyncContextManager[AsyncSession]]):
        self._session_factory = session_factory

    async def handle(self, command: Command) -> None:
        async with self._session_factory() as session:
            print(f'got {command} command and session is provided!')
            # do something with session


class YetAnotherHandler(AbstractHandler):

    async def handle(self, command: Command):
        print(f'got {command} message')
        await self.add_event('on command received')


async def main():
    dp.start()

    dp.commands.bind(Command, YetAnotherHandler)
    # or
    # dp.commands.bind(Command, YetAnotherHandler().handle)
    # or
    # dp.register_command_handler(Command, YetAnotherHandler)
    # and decorator ofc... Choose your best way!

    await dp.events.send(Event(name='someevent', somelogiccalue='emit'))
    await dp.commands.send(Command(dosomething=True))

    await asyncio.sleep(1)


if __name__ == '__main__':
    asyncio.run(main())
```
