Skip to main content

Async Python DDD utilities library.

Project description

Async Python DDD utilities library

aioddd is an async Python DDD utilities library.

Installation

Use the package manager pip to install aiocli.

pip install aioddd

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.6

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.2.12.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

aioddd-1.2.12-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aioddd-1.2.12.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.6.12

File hashes

Hashes for aioddd-1.2.12.tar.gz
Algorithm Hash digest
SHA256 1f2701aad500e6f2f6d4bed05c34f988387da36985d5478ba6eb714f236ca1f3
MD5 50c13d62a74dab03202fdbbd759a261a
BLAKE2b-256 6ee0e1ab838795ac758a339fd2cfcedf747e3c08b8d19a3347b89050a06a8832

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: aioddd-1.2.12-py3-none-any.whl
  • Upload date:
  • Size: 18.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.6.12

File hashes

Hashes for aioddd-1.2.12-py3-none-any.whl
Algorithm Hash digest
SHA256 963c952af24899fe9ddb4929daf63499cebbf8405e667b73efd0e66a2c62f6bd
MD5 bf1a7475a106e2570d15a746cd0008aa
BLAKE2b-256 d0eb67d190e70a0c9f7a6ea81feb5af8b7bd14cbe29c17401f0808d14e04b1a6

See more details on using hashes here.

Provenance

Supported by

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