Skip to main content

Async Python DDD utilities library.

Project description

Async Python DDD utilities library

PyPI version PyPIDownloads CI

aioddd is an async Python DDD utilities library.

Installation

Use the package manager pip to install aioddd.

pip install aioddd

Documentation

Usage

from asyncio import get_event_loop
from dataclasses import dataclass
from typing import Type
from aioddd import NotFoundError, \
    Command, CommandHandler, SimpleCommandBus, \
    Query, QueryHandler, OptionalResponse, SimpleQueryBus, Event

_products = []

class ProductStored(Event):
    @dataclass
    class Attributes:
        ref: str

    attributes: Attributes

class StoreProductCommand(Command):
    def __init__(self, ref: str):
        self.ref = ref

class StoreProductCommandHandler(CommandHandler):
    def subscribed_to(self) -> Type[Command]:
        return StoreProductCommand

    async def handle(self, command: StoreProductCommand) -> None:
        _products.append(command.ref)

class ProductNotFoundError(NotFoundError):
    _code = 'product_not_found'
    _title = 'Product not found'

class FindProductQuery(Query):
    def __init__(self, ref: str):
        self.ref = ref

class FindProductQueryHandler(QueryHandler):
    def subscribed_to(self) -> Type[Query]:
        return FindProductQuery

    async def handle(self, query: FindProductQuery) -> OptionalResponse:
        if query.ref != '123':
            raise ProductNotFoundError.create(detail={'ref': query.ref})
        return {'ref': query.ref}

async def main() -> None:
    commands_bus = SimpleCommandBus([StoreProductCommandHandler()])
    await commands_bus.dispatch(StoreProductCommand('123'))
    query_bus = SimpleQueryBus([FindProductQueryHandler()])
    response = await query_bus.ask(FindProductQuery('123'))
    print(response)


if __name__ == '__main__':
    get_event_loop().run_until_complete(main())

Requirements

  • Python >= 3.10

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

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

aioddd-1.6.0.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aioddd-1.6.0-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file aioddd-1.6.0.tar.gz.

File metadata

  • Download URL: aioddd-1.6.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for aioddd-1.6.0.tar.gz
Algorithm Hash digest
SHA256 0c679e525028352e72f923c0f001ef7dfcda172ace22c9d91bc477960af19e38
MD5 0e3fce070b92fcd3bc06f86f1ee3957b
BLAKE2b-256 9cea5d0b804fd095c0eb5bc46b61db1a8557ee5a55630245601a45de6dff6fd2

See more details on using hashes here.

File details

Details for the file aioddd-1.6.0-py3-none-any.whl.

File metadata

  • Download URL: aioddd-1.6.0-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for aioddd-1.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b940b18e7e478bd0218fad87f31be6c53df5f83d177344dee3ce9b75aaf9b84e
MD5 bb10e450eb5d9d60a98fe9d478086632
BLAKE2b-256 172937365d07f40314b3ea2d923f89bf7eef966caf2be96b4f060269b200597a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page