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, tags=None, deprecated=None, security=None)
Bases: 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:
|
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
119 120 121 122 123 124 125 126 127 128 129 130 131 132 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 |
|
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
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 |
|
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
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 |
|
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
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 |
|
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)
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:
|
Source code in esmerald/routing/router.py
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 |
|
add_websocket_route
¶
add_websocket_route(path, handler, name=None, dependencies=None, interceptors=None, permissions=None, exception_handlers=None, middleware=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:
|
Source code in esmerald/routing/router.py
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 |
|