Replies: 1 comment 1 reply
-
class Controller:
def __init__(self, endpoint: ASGIApp):
self.endpoint = endpoint
async def __call__(self, scope, receive, send):
method = scope['method']
# do here any internal routing
# eg. map POST to ` self.endpoint.create` or map GET to `self.endpoint.find`
routes = [
Controller('/users', MyEndpoint),
] |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
For some use-cases (ex. Model-View-Controller), we might need more flexibility in the routing. For example, we might have several "GET" routes all going to the same Endpoint, but with different methods (depending on the particular path or path parameters).
An easy way to do this would be to allow the addition of user-chosen keywords to the Route class:
These keywords could then be passed to the Endpoint by simply adding them to the scope, i.e. in the handle() function:
Now the endpoint can pull this from the scope and modify how it behaves.
If you prefer, instead of keywords, an additional
context
dictionary could be passed and added into the scope that way:Route('/abc', MyEndpoint, methods=['GET'], context={'action' : 'list', 'authenticated' : True} )
What is your opinion?
An easy PR, I would think.
Beta Was this translation helpful? Give feedback.
All reactions