REPR
The REPR Design Pattern (Request, EndPoint, Response) is the typical usage of mediatpy.
Request/Response
class MyResponse:
pass
class MyRequest(Request[MyResponse]):
pass
In both the mediatpy.Request and the mediatpy.Response you can define any number of attributes.
Response
It’s not necessary to explicitly declare a response, you could use any type as the response of your mediatpy.RequestHandler.
Command
If you declare the mediatpy.RequestHandler to return None, you will get a canonical command.
EndPoint
Then, you must create a mediatpy.RequestHandler to manage the mediatpy.Request and return the expected mediatpy.Response.
class MyRequestHandler(RequestHandler[MyRequest, MyResponse]):
async def handle(self, request: MyRequest) -> MyResponse:
return MyResponse()
Registration
Manually
mediator = Mediator()
mediator.register_request_handler(MyRequestHandler)
Decorator
@mediator.request_handler
class MyRequestHandler(RequestHandler[MyRequest, MyResponse]):
async def handle(self, request: MyRequest) -> MyResponse:
return MyResponse()
Example
import asyncio
from mediatpy import Request, RequestHandler, Mediator
class MyResponse:
pass
class MyRequest(Request[MyResponse]):
pass
mediator = Mediator()
@mediator.request_handler
class MyRequestHandler(RequestHandler[MyRequest, MyResponse]):
async def handle(self, request: MyRequest) -> MyResponse:
return MyResponse()
async def main():
request = MyRequest()
response = await mediator.send(request)
assert isinstance(response, MyResponse)
if __name__ == '__main__':
asyncio.run(main())