|
| 1 | +# Module Router |
| 2 | + |
| 3 | +ModuleRouter allows you to define your route handlers as standalone functions, providing an alternative to using classes. |
| 4 | +This can be beneficial for python developers who prefer using functions. |
| 5 | +It is important to note that using ModuleRouter does not limit your access to other features provided by Ellar. |
| 6 | + |
| 7 | +## **Usage** |
| 8 | +The Ellar CLI tool generates a `routers.py` file in every `create-module` scaffold command. |
| 9 | +This file contains a quick guide on how to use the `ModuleRouter` class. |
| 10 | + |
| 11 | +Let's use the **routers.py** created in our previous project. And create **two** route functions, **addition** and **subtraction** |
| 12 | + |
| 13 | +```python |
| 14 | +# project_name/apps/dogs/routers.py |
| 15 | +""" |
| 16 | +Define endpoints routes in python function fashion |
| 17 | +example: |
| 18 | +
|
| 19 | +my_router = ModuleRouter("/cats", tag="Cats", description="Cats Resource description") |
| 20 | +
|
| 21 | +@my_router.get('/') |
| 22 | +def index(request: Request): |
| 23 | + return {'detail': 'Welcome to Cats Resource'} |
| 24 | +""" |
| 25 | +from ellar.common import ModuleRouter |
| 26 | + |
| 27 | +math_router = ModuleRouter('/math', tag='Math') |
| 28 | + |
| 29 | +@math_router.get('/add') |
| 30 | +def addition(a:int, b:int): |
| 31 | + return a + b |
| 32 | + |
| 33 | + |
| 34 | +@math_router.get('/subtract') |
| 35 | +def subtraction(a:int, b:int): |
| 36 | + return a - b |
| 37 | +``` |
| 38 | +In the example above, we created `math_router` with a prefix `/math` and a OPENAPI tag 'math'. Then we added two routes `addition(a:int, b:int)` and `subtraction(a:int, b:int)`. |
| 39 | +Each route takes two query parameters, 'a' and 'b' which are declared as int type. These functions handle the query parameters and return the result of the mathematical operation. |
| 40 | + |
| 41 | +Next, we have to make the `math_router` visible to the application |
| 42 | + |
| 43 | +## **Registering Module Router** |
| 44 | +Like controllers, ModuleRouters also need to be registered to their root module in order to be used in a web application. |
| 45 | +In the example provided above, the `math_router` would be registered under the `project_name/apps/dogs/module.py` file. |
| 46 | + |
| 47 | +This registration process typically involves importing the `math_router` and then adding it to the list of `routers` in the `module.py` file. |
| 48 | +This allows the router to be recognized by the application and its routes to be available to handle requests. |
| 49 | + |
| 50 | +```python |
| 51 | + |
| 52 | +from ellar.common import Module |
| 53 | +from ellar.core import ModuleBase |
| 54 | +from ellar.di import Container |
| 55 | + |
| 56 | +from .controllers import DogsController |
| 57 | +from .routers import math_router |
| 58 | + |
| 59 | + |
| 60 | +@Module( |
| 61 | + controllers=[DogsController], |
| 62 | + providers=[], |
| 63 | + routers=[math_router], |
| 64 | +) |
| 65 | +class DogsModule(ModuleBase): |
| 66 | + def register_providers(self, container: Container) -> None: |
| 67 | + # for more complicated provider registrations |
| 68 | + # container.register_instance(...) |
| 69 | + pass |
| 70 | +``` |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +## **Accessing Other Request Object** |
| 76 | +In functional route handle, we can access request object and response object through custom decorators or type annotation as shown below. |
| 77 | + |
| 78 | +### By Type Annotation |
| 79 | +Let's inject request and response object in `addition` route handler function from our previous example |
| 80 | + |
| 81 | +```python |
| 82 | +from ellar.core import Request, Response |
| 83 | +from ellar.common import ModuleRouter |
| 84 | + |
| 85 | + |
| 86 | +math_router = ModuleRouter('/math', tag='Math') |
| 87 | + |
| 88 | +@math_router.get('/add') |
| 89 | +def addition(request: Request, res: Response, a:int, b:int): |
| 90 | + res.headers['x-operation'] = 'Addition' |
| 91 | + return dict(is_request_object=isinstance(request, Request), is_response_object=isinstance(res, Response), operation_result=a + b) |
| 92 | + |
| 93 | +``` |
| 94 | + |
| 95 | +### **By Custom decorators** |
| 96 | +You can also achieve the same result by using custom decorator. |
| 97 | + |
| 98 | +```python |
| 99 | +from ellar.core import Request, Response |
| 100 | +from ellar.common import ModuleRouter, Req, Res |
| 101 | + |
| 102 | + |
| 103 | +math_router = ModuleRouter('/math', tag='Math') |
| 104 | + |
| 105 | +@math_router.get('/add') |
| 106 | +def addition(*, request=Req(), res=Res(), a:int, b:int): |
| 107 | + res.headers['x-operation'] = 'Addition' |
| 108 | + return dict(is_request_object=isinstance(request, Request), is_response_object=isinstance(res, Response), operation_result=a + b) |
| 109 | + |
| 110 | +``` |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | +## **Inject Services** |
| 115 | +We can also inject service providers just like controller routes using the `Provide` function. |
| 116 | + |
| 117 | +```python |
| 118 | +from ellar.core import Request, Response, IExecutionContext |
| 119 | +from ellar.common import ModuleRouter, Provide |
| 120 | + |
| 121 | + |
| 122 | +math_router = ModuleRouter('/math', tag='Math') |
| 123 | + |
| 124 | +@math_router.get('/subtract') |
| 125 | +def subtraction(a:int, b:int, res=Provide(Response), req=Provide(Request), ctx=Provide(IExecutionContext)): |
| 126 | + res.headers['x-operation'] = 'Subtraction' |
| 127 | + return dict( |
| 128 | + is_request_object=isinstance(req, Request), |
| 129 | + is_response_object=isinstance(res, Response), |
| 130 | + is_context_object=isinstance(ctx, IExecutionContext), |
| 131 | + operation_result=a - b |
| 132 | + ) |
| 133 | + |
| 134 | +``` |
0 commit comments