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.9.tar.gz (87.3 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.9-py3-none-any.whl (25.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for anywise-0.1.9.tar.gz
Algorithm Hash digest
SHA256 0ef762ecfc14645a65e04fbc97b804c165057f6a14ef5756fe76b12b9bf8edc4
MD5 712bc390fe046ed2d31828d97af6a834
BLAKE2b-256 41979d9bd47f0918f41eb69d3d77b8c7e32b88a9b397c8c8b5cc8cb1315e9ce4

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for anywise-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 06315002d9f9c4fd80d04536c5285d69a7fb667a5dd8e85f75d08e775898536d
MD5 b30b3965e8bc056e6161ab704a168b9d
BLAKE2b-256 3c742fefc129966add5bed36d271da68cf12a916059fdb08b282d6c6a4f26c92

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