Skip to main content

Anywise let you write your application anywise

Project description

Anywise

Anywise provides a universal and flexible API for your application by abstracting function calls into message passing, make it easy to build scalable, maintainable, and testable applications.

  • Decouples the intention behind the execution of application services from the application service itself
  • Eliminates direct dependencies on implementation details.
  • Improves development speed, reduces testing complexity, and enhances the reusability of the application as a whole.

Documentation: https://raceychan.github.io/anywise/

Source Code: https://github.com/raceychan/anywise


Install

pip install anywise

Quck Start

Let start with defining messages:

from anywise import Anywise, MessageRegistry, use

class UserCommand: ...
class CreateUser(UserCommand): ...
class UserEvent: ...
class UserCreated(UserEvent): ...

Next step, Register command handler and event listeners.

handler/listener

for simplicity, we will use function-based handler here

registry = MessageRegistry(command_base=UserCommand, event_base=UserEvent)

@registry 
async def create_user(
    command: CreateUser, 
    anywise: Anywise, 
    service: UserService = use(user_service_factory)
):
    await users.signup(command.username, command.user_email)
    await anywise.publish(UserCreated(command.username, command.user_email))

@registry
async def notify_user(event: UserCreated, service: EmailSender):
    await service.send_greeting(command.user_email)

@reigstry
class ProductService:
    def __init__(self, repo: ProductRepository, aw: Anywise):
        self._repo = repo
        self._aw = aw

    async def add_item(self, command: AddItem):
        item = Item(sku=command.sku, name="item")
        await self._repo.add(item)
        await self._aw.publish(ItemAdded(sku=item.sku, name=item.name))

    async def on_item_added(self, event: ItemAdded):
        product = await self._repo.get(event.sku)
        if len(product.items) >= 1:
            product.status = "available"
        await self._repo.update(prodct)

you can use registry as a decorator to register handler/listeners

or you can register many handler at once using MessageRegistry.register_all

registry.register_all(create_user, notify_user)

Message Source

Message source is where you can your message from.

Here we use fastapi as our message source, but it can be other choices.

from anywise import Anywise
from anywise.integration.fastapi import FastWise

@app.post("/users")
async def signup(command: CreateUser, anywise: FastWise) -> User:
    return await anywise.send(command)

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

anywise-0.1.7.tar.gz (84.1 kB view details)

Uploaded Source

Built Distribution

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

anywise-0.1.7-py3-none-any.whl (24.6 kB view details)

Uploaded Python 3

File details

Details for the file anywise-0.1.7.tar.gz.

File metadata

  • Download URL: anywise-0.1.7.tar.gz
  • Upload date:
  • Size: 84.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.9

File hashes

Hashes for anywise-0.1.7.tar.gz
Algorithm Hash digest
SHA256 d23c169a11bfc3f3b41d70ac9a800b92a101cc7be2801eac3d517b26effe6acc
MD5 78be235b7c5f8070494f2a961302caf2
BLAKE2b-256 9b3c610f31e327da240e3ca2827bd7fa1c501e924bb88481ff2da3f80815cea4

See more details on using hashes here.

File details

Details for the file anywise-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: anywise-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 24.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.9

File hashes

Hashes for anywise-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 95a45c6f3ad2dccd94dc46813e9c972bd861f6d889a0e6b6e80ce909ac69c4f6
MD5 1b46265752af11beb5aa2580547c54c9
BLAKE2b-256 8f6883f38dd69bfd6fef14dcc137cd484ce3a4f90151b947060aabb310748f19

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