Request Mediator PyPI (Python Package Index) Package
Project description
RmediatoR
Introduction
RmediatoR is a Python package inspired by the MediatR library available on NuGet. It allows developers to implement the mediator design pattern in their applications, promoting a clean separation of concerns by centralizing request/response logic and eliminating direct dependencies between components. This package simplifies the communication between different parts of your application, making your code more maintainable and scalable.
Demonstration
Here's a quick example to demonstrate how RmediatoR works:
- Define a Request and Handler: Create a request class and a handler class that processes the request.
from rmediator.decorators import request, request_handler
# Define a response
class SomeResponse:
def __init__(self, message):
self.message = message
# Define a request
@request(SomeResponse)
class SomeRequest:
pass
# Define a handler for the request
@request_handler(SomeRequest, SomeResponse)
class SomeRequestHandler:
def handle(self, request: SomeRequest) -> SomeResponse:
return SomeResponse("Handled!")
- Initialize the mediator and register the handlers
from rmediator import Mediator
mediator = Mediator()
mediator.register_handler(SomeRequest, SomeRequestHandler())
- Send a Request: Use the mediator to send a request and get a response.
# Create a request instance
request = SomeRequest()
# Send the request through the mediator
response = mediator.send(request)
# Output the response
print(response.message) # Output: Handled!
How to Use
To start using RmediatoR, follow these steps:
- Installation: First, install the package using pip (or use other options like poetry).
pip install rmediator
-
Creating Requests and Handlers: Define your request and handler classes as shown in the demonstration above. Each request should inherit from the Request class, and each handler should inherit from the Handler class.
-
Registering Handlers: Register your handlers with the mediator. This tells the mediator which handler to use for each request type.
mediator.register_handler(YourRequestClass, YourHandlerClass)
- Sending Requests: Use the mediator to send requests. The mediator will find the appropriate handler and return the response.
response = mediator.send(your_request_instance)
License
RmediatoR is licensed under the MIT License. See the LICENSE file for more details.
Contributing
Contributions are welcome! Please read the CONTRIBUTING guidelines for more information on how to get started.
Contact
For questions or issues, please open an issue on GitHub or contact me on my channels.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for rmediator-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c59fd1092b2eae6e5b01710b766d4f21976cc4b52d368db080287057edcf1aa5 |
|
MD5 | 651650011eb96f32815a750c86c110b5 |
|
BLAKE2b-256 | 2e9762a6f67830b51ce6771a25a9aed68a9051d78e69ecabbfba3540631b45de |