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.

  • Eliminates direct dependencies on implementation details.
  • Improves development speed, reduces testing complexity, and enhances the reusability of the application as a whole.
  • Promotes best practices and loose coupling.

Features

  • minimal change to existing code, easy to adopt.
  • integrated dependency injection system, automatically inject dependency at runtime.
  • type-based message system
  • strong support to AOP, middlewares, decorators, etc.

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

from anywise import MessageRegistr, BaseGuard
registry = MessageRegistry(command_base=UserCommand, event_base=UserEvent)

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))

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

class IPContext(TypeDict):
    ip: str

class IPLimiter(BaseGuard):
    def __init__(self, throttle_list: tuple[str], white_lst: WhiteList):
        self._lst = throttle_list
        self._white_lst = white_lst

    async def __call__(self, command: UserCommand, context: IPContext):
        if not await self._white_lst.should_pass(command.user_id):
            if context["ip"] in self._lst:
                return ThrottleResponse()

registry.register(IPLimiter, create_user, notify_user)

NOTE: you can also use registry as a decorator to register handler/listeners.

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.8.tar.gz (84.6 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.8-py3-none-any.whl (24.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for anywise-0.1.8.tar.gz
Algorithm Hash digest
SHA256 c8e711425a4944f3ba6fc4665f22a6bbc9c4489bb19ede9bc2082d5ce0d0b4af
MD5 41a67669dcb4ad5292d14c0b191e9f78
BLAKE2b-256 2ae05232853b99c42073fec8389d2a2a305eda337f696b78ea28686cea3e6ab7

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for anywise-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 86b12407ca48b0b792e1c52313390ab5275536fad1a2230060dc1cf645401491
MD5 a1486dcd6892d1c5125ab9c3afce8806
BLAKE2b-256 7105de58274a5436763871a8861f6bb00fef37a3dd7336de0a945b52b2502eb6

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