Router
class¶
This is the reference for the Router
that contains all the parameters,
attributes and functions.
How to import¶
from esmerald import Router
esmerald.Router
¶
Router(path=None, app=None, parent=None, on_startup=None, on_shutdown=None, redirect_slashes=None, default=None, routes=None, name=None, dependencies=None, interceptors=None, permissions=None, exception_handlers=None, middleware=None, response_class=None, response_cookies=None, response_headers=None, lifespan=None, before_request=None, after_request=None, tags=None, deprecated=None, security=None)
Bases: RoutingMethodsMixin
, BaseRouter
The Router
object used by Esmerald
upon instantiation.
The router is what is created by default when the routes
parameter is
defined.
This object is complex and very powerful. Read more in detail about the Router and how to use it.
PARAMETER | DESCRIPTION |
---|---|
path
|
Relative path of the Example
Example with parameters
TYPE:
|
app
|
A Example
TYPE:
|
parent
|
Who owns the Gateway. If not specified, the application automatically it assign it. This is directly related with the application levels.
TYPE:
|
on_startup
|
A Read more about the events. Example
TYPE:
|
on_shutdown
|
A Read more about the events. Example
TYPE:
|
redirect_slashes
|
Boolean flag indicating if the redirect slashes are enabled for the routes or not.
TYPE:
|
default
|
A
TYPE:
|
routes
|
A This is also an entry-point for the routes of the Read more about how to use and leverage the Esmerald routing system. Example
Note The Include is very powerful and this example is not enough to understand what more things you can do. Read in more detail about this.
TYPE:
|
name
|
The name for the Gateway. The name can be reversed by
TYPE:
|
dependencies
|
A dictionary of string and Inject instances enable application level dependency injection.
TYPE:
|
interceptors
|
A list of interceptors to serve the application incoming requests (HTTP and Websockets).
TYPE:
|
permissions
|
A list of permissions to serve the application incoming requests (HTTP and Websockets).
TYPE:
|
exception_handlers
|
A dictionary of exception types (or custom exceptions) and the handler functions on an application top level. Exception handler callables should be of the form of
TYPE:
|
middleware
|
A list of middleware to run for every request. The middlewares of an Include will be checked from top-down or Lilya Middleware as they are both converted internally. Read more about Python Protocols.
TYPE:
|
response_class
|
Default response class to be used within the Esmerald application. Read more about the Responses and how to use them. Example
TYPE:
|
response_cookies
|
A sequence of Read more about the Cookies. Example
TYPE:
|
response_headers
|
A mapping of Read more about the ResponseHeader. Example
TYPE:
|
lifespan
|
A Read more about the lifespan.
TYPE:
|
before_request
|
A Read more about the events. Example
TYPE:
|
after_request
|
A Read more about the events. Example
TYPE:
|
tags
|
A list of strings tags to be applied to the path operation. It will be added to the generated OpenAPI documentation. Note almost everything in Esmerald can be done in levels, which means these tags on a Esmerald instance, means it will be added to every route even if those routes also contain tags.
TYPE:
|
deprecated
|
TYPE:
|
security
|
Used by OpenAPI definition, the security must be compliant with the norms. Esmerald offers some out of the box solutions where this is implemented. The Esmerald security is available to automatically used. The security can be applied also on a level basis. For custom security objects, you must subclass
TYPE:
|
Source code in esmerald/routing/router.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 |
|
permissions
instance-attribute
¶
permissions = [permission for permission in __base_permissions__ or [] if is_esmerald_permission(permission)]
app
async
¶
app(scope, receive, send)
Handle ASGI messages, managing different scopes and routing.
PARAMETER | DESCRIPTION |
---|---|
scope
|
TYPE:
|
receive
|
TYPE:
|
send
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
scope
|
The ASGI scope.
TYPE:
|
receive
|
The ASGI receive channel.
TYPE:
|
send
|
The ASGI send channel.
TYPE:
|
Source code in lilya/routing.py
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 |
|
lifespan
async
¶
lifespan(scope, receive, send)
Handle ASGI lifespan messages, managing application startup and shutdown events.
PARAMETER | DESCRIPTION |
---|---|
scope
|
TYPE:
|
receive
|
TYPE:
|
send
|
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
scope
|
The ASGI scope.
TYPE:
|
receive
|
The receive channel.
TYPE:
|
send
|
The send channel.
TYPE:
|
Source code in lilya/routing.py
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 |
|
add_apiview
¶
add_apiview(value)
Adds an APIView or related to the application routing.
Example
from esmerald import Router, APIView, Gateway, get
class View(APIView):
path = "/"
@get(status_code=status_code)
async def hello(self) -> str:
return "Hello, World!"
gateway = Gateway(handler=View)
app = Router()
app.add_apiview(value=gateway)
PARAMETER | DESCRIPTION |
---|---|
value
|
The
TYPE:
|
Source code in esmerald/routing/router.py
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 |
|
add_route
¶
add_route(path, handler, dependencies=None, interceptors=None, permissions=None, exception_handlers=None, middleware=None, name=None, include_in_schema=True, deprecated=None, before_request=None, after_request=None)
Adds a Route to the application routing.
This is a dynamic way of adding routes on the fly.
Example
from esmerald import get
@get(status_code=status_code)
async def hello(self) -> str:
return "Hello, World!"
app = Esmerald()
app.add_route(path="/hello", handler=hello)
PARAMETER | DESCRIPTION |
---|---|
path
|
Relative path of the
TYPE:
|
handler
|
An instance of handler.
TYPE:
|
dependencies
|
A dictionary of string and Inject instances enable application level dependency injection.
TYPE:
|
interceptors
|
A list of interceptors to serve the application incoming requests (HTTP and Websockets).
TYPE:
|
permissions
|
A list of permissions to serve the application incoming requests (HTTP and Websockets).
TYPE:
|
exception_handlers
|
A dictionary of exception types (or custom exceptions) and the handler functions on an application top level. Exception handler callables should be of the form of
TYPE:
|
middleware
|
A list of middleware to run for every request. The middlewares of an Include will be checked from top-down or Lilya Middleware as they are both converted internally. Read more about Python Protocols.
TYPE:
|
name
|
The name for the Gateway. The name can be reversed by
TYPE:
|
include_in_schema
|
Boolean flag indicating if it should be added to the OpenAPI docs.
TYPE:
|
deprecated
|
Boolean flag for indicating the deprecation of the Gateway and to display it in the OpenAPI documentation..
TYPE:
|
before_request
|
A list of events that are triggered before the application processes the request.
TYPE:
|
after_request
|
A list of events that are triggered after the application processes the request.
TYPE:
|
Source code in esmerald/routing/router.py
1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 |
|
add_websocket_route
¶
add_websocket_route(path, handler, name=None, dependencies=None, interceptors=None, permissions=None, exception_handlers=None, middleware=None, before_request=None, after_request=None)
Adds a websocket Route to the application routing.
This is a dynamic way of adding routes on the fly.
Example
from esmerald import websocket
@websocket()
async def websocket_route(socket: WebSocket) -> None:
await socket.accept()
data = await socket.receive_json()
assert data
await socket.send_json({"data": "esmerald"})
await socket.close()
app = Esmerald()
app.add_websocket_route(path="/ws", handler=websocket_route)
PARAMETER | DESCRIPTION |
---|---|
path
|
Relative path of the
TYPE:
|
handler
|
An instance of websocket handler.
TYPE:
|
name
|
The name for the Gateway. The name can be reversed by
TYPE:
|
dependencies
|
A dictionary of string and Inject instances enable application level dependency injection.
TYPE:
|
interceptors
|
A list of interceptors to serve the application incoming requests (HTTP and Websockets).
TYPE:
|
permissions
|
A list of permissions to serve the application incoming requests (HTTP and Websockets).
TYPE:
|
exception_handlers
|
A dictionary of exception types (or custom exceptions) and the handler functions on an application top level. Exception handler callables should be of the form of
TYPE:
|
middleware
|
A list of middleware to run for every request. The middlewares of an Include will be checked from top-down or Lilya Middleware as they are both converted internally. Read more about Python Protocols.
TYPE:
|
before_request
|
A list of events that are triggered before the application processes the request.
TYPE:
|
after_request
|
A list of events that are triggered after the application processes the request.
TYPE:
|
Source code in esmerald/routing/router.py
2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 |
|