Router
sensei.Router
¶
Router(host, *, port=None, rate_limit=None, manager=None, default_case=None, query_case=None, body_case=None, cookie_case=None, header_case=header_case, response_case=None, __finalize_json__=identical, __prepare_args__=identical)
Bases: IRouter
Router for managing API routes and handling HTTP requests. Provides decorators for defining API endpoints with various HTTP methods:
- GET
- POST
- PATCH
- PUT
- DELETE
- HEAD
- OPTIONS
Import it directly from Sensei:
from sensei import Router
Example
from typing import Annotated
from sensei import Router, Path, APIModel
router = Router('https://pokeapi.co/api/v2/')
class Pokemon(APIModel):
name: str
id: int
height: int
weight: int
@router.get('/pokemon/{name}')
def get_pokemon(name: Annotated[str, Path(max_length=300)]) -> Pokemon:
pass
pokemon = get_pokemon(name="pikachu")
print(pokemon)
PARAMETER | DESCRIPTION |
---|---|
host
|
The root URL of the associated API. It may contain a colon and a placeholder for the port,
e.g., Example
TYPE:
|
port
|
The port number of the associated API. If
TYPE:
|
rate_limit
|
An object implementing the Example
TYPE:
|
manager
|
A Example
TYPE:
|
default_case
|
Case converter for all parameters.
TYPE:
|
query_case
|
Case converter of query parameters. Example
TYPE:
|
body_case
|
Case converter of body.
TYPE:
|
cookie_case
|
Case converter of cookies.
TYPE:
|
header_case
|
Case converter of headers.
TYPE:
|
response_case
|
Case converter of JSON response.
TYPE:
|
__finalize_json__
|
A function to finalize the JSON response. It's applied for each routed function, associated with the Router. The final value must be JSON serializable. Can be represented as async function. JSON finalizer is used for JSON response transformation before internal or user-defined response finalizing. Example
TYPE:
|
__prepare_args__
|
A preparer function used to prepare the arguments for the request before it is sent. It's applied for
each routed function, associated with the Router. The final value must be an instance of Preparer is executed after internal argument parsing. So, all request parameters are available in
Example
TYPE:
|
Source code in sensei/_internal/_core/router.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 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 |
|
base_url
property
¶
base_url
Get the base URL constructed from the host and port.
RETURNS | DESCRIPTION |
---|---|
str
|
The base URL.
TYPE:
|
port
property
writable
¶
port
Get the port number of the associated API.
RETURNS | DESCRIPTION |
---|---|
int
|
The port number of the associated API. If
TYPE:
|
rate_limit
property
writable
¶
rate_limit
Get the rate limit used to handle API rate limiting.
RETURNS | DESCRIPTION |
---|---|
IRateLimit
|
An object implementing the
TYPE:
|
manager
property
writable
¶
manager
Get the manager used to provide an HTTP client to the router.
RETURNS | DESCRIPTION |
---|---|
Manager
|
The
TYPE:
|
default_case
property
¶
default_case
RETURNS | DESCRIPTION |
---|---|
CaseConverter
|
Case converter for all parameters.
TYPE:
|
query_case
property
¶
query_case
RETURNS | DESCRIPTION |
---|---|
CaseConverter
|
Case converter of query parameters.
TYPE:
|
body_case
property
¶
body_case
RETURNS | DESCRIPTION |
---|---|
CaseConverter
|
Case converter of body parameters.
TYPE:
|
cookie_case
property
¶
cookie_case
RETURNS | DESCRIPTION |
---|---|
CaseConverter
|
Case converter of cookies.
TYPE:
|
header_case
property
¶
header_case
RETURNS | DESCRIPTION |
---|---|
CaseConverter
|
Case converter of headers.
TYPE:
|
response_case
property
¶
response_case
RETURNS | DESCRIPTION |
---|---|
CaseConverter
|
Case converter of JSON response.
TYPE:
|
get
¶
get(path, /, *, default_case=None, query_case=None, cookie_case=None, header_case=None, response_case=None, skip_finalizer=False, skip_preparer=False)
Create a route using the GET method.
This decorator transforms a function into a routed function that sends a GET request to the specified path. Routed function handles parameter case conversion, argument validation, argument preparation, response finalizing.
PARAMETER | DESCRIPTION |
---|---|
path
|
The relative path of the route.
TYPE:
|
default_case
|
Case converter for all parameters. Defaults to using the router's default.
TYPE:
|
query_case
|
Case converter for query parameters. Defaults to using the router's default.
TYPE:
|
cookie_case
|
Case converter for cookie parameters. Defaults to using the router's default.
TYPE:
|
header_case
|
Case converter for header parameters. Defaults to using the router's default.
TYPE:
|
response_case
|
Case converter for JSON response. Defaults to using the router's default.
TYPE:
|
skip_finalizer
|
Skip JSON finalizer, when finalizing response. Defaults to
TYPE:
|
skip_preparer
|
Skip preparing the arguments for the request. Defaults to
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
RoutedFunction
|
A routed function that sends a GET request according to the specified path and validates
TYPE:
|
_RouteDecorator
|
its arguments based on type annotations. |
RAISES | DESCRIPTION |
---|---|
ValidationError
|
If type validation of arguments fails. |
Source code in sensei/_internal/_core/router.py
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 |
|
post
¶
post(path, /, *, default_case=None, query_case=None, body_case=None, cookie_case=None, header_case=None, response_case=None, skip_finalizer=False, skip_preparer=False)
Create a route using the POST method.
This decorator transforms a function into a routed function that sends a POST request to the specified path. Routed function handles parameter case conversion, argument validation, argument preparation, response finalizing.
PARAMETER | DESCRIPTION |
---|---|
path
|
The relative path of the route.
TYPE:
|
default_case
|
Case converter for all parameters. Defaults to using the router's default.
TYPE:
|
query_case
|
Case converter for query parameters. Defaults to using the router's default.
TYPE:
|
body_case
|
Case converter for body parameters. Defaults to using the router's default.
TYPE:
|
cookie_case
|
Case converter for cookie parameters. Defaults to using the router's default.
TYPE:
|
header_case
|
Case converter for header parameters. Defaults to using the router's default.
TYPE:
|
response_case
|
Case converter for JSON response. Defaults to using the router's default.
TYPE:
|
skip_finalizer
|
Skip JSON finalizer, when finalizing response. Defaults to
TYPE:
|
skip_preparer
|
Skip preparing the arguments for the request. Defaults to
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
RoutedFunction
|
A routed function that sends a POST request according to the specified path and validates
TYPE:
|
_RouteDecorator
|
its arguments based on type annotations. |
RAISES | DESCRIPTION |
---|---|
ValidationError
|
If type validation of arguments fails. |
Source code in sensei/_internal/_core/router.py
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 |
|
patch
¶
patch(path, /, *, default_case=None, query_case=None, body_case=None, cookie_case=None, header_case=None, response_case=None, skip_finalizer=False, skip_preparer=False)
Create a route using the PATCH method.
This decorator transforms a function into a routed function that sends a PATCH request to the specified path. Routed function handles parameter case conversion, argument validation, argument preparation, response finalizing.
PARAMETER | DESCRIPTION |
---|---|
path
|
The relative path of the route.
TYPE:
|
default_case
|
Case converter for all parameters. Defaults to using the router's default.
TYPE:
|
query_case
|
Case converter for query parameters. Defaults to using the router's default.
TYPE:
|
body_case
|
Case converter for body parameters. Defaults to using the router's default.
TYPE:
|
cookie_case
|
Case converter for cookie parameters. Defaults to using the router's default.
TYPE:
|
header_case
|
Case converter for header parameters. Defaults to using the router's default.
TYPE:
|
response_case
|
Case converter for JSON response. Defaults to using the router's default.
TYPE:
|
skip_finalizer
|
Skip JSON finalizer, when finalizing response. Defaults to
TYPE:
|
skip_preparer
|
Skip preparing the arguments for the request. Defaults to
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
RoutedFunction
|
A routed function that sends a PATCH request according to the specified path and validates
TYPE:
|
_RouteDecorator
|
its arguments based on type annotations. |
RAISES | DESCRIPTION |
---|---|
ValidationError
|
If type validation of arguments fails. |
Source code in sensei/_internal/_core/router.py
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 |
|
put
¶
put(path, /, *, default_case=None, query_case=None, body_case=None, cookie_case=None, header_case=None, response_case=None, skip_finalizer=False, skip_preparer=False)
Create a route using the PUT method.
This decorator transforms a function into a routed function that sends a PUT request to the specified path. Routed function handles parameter case conversion, argument validation, argument preparation, response finalizing.
PARAMETER | DESCRIPTION |
---|---|
path
|
The relative path of the route.
TYPE:
|
default_case
|
Case converter for all parameters. Defaults to using the router's default.
TYPE:
|
query_case
|
Case converter for query parameters. Defaults to using the router's default.
TYPE:
|
body_case
|
Case converter for body parameters. Defaults to using the router's default.
TYPE:
|
cookie_case
|
Case converter for cookie parameters. Defaults to using the router's default.
TYPE:
|
header_case
|
Case converter for header parameters. Defaults to using the router's default.
TYPE:
|
response_case
|
Case converter for JSON response. Defaults to using the router's default.
TYPE:
|
skip_finalizer
|
Skip JSON finalizer, when finalizing response. Defaults to
TYPE:
|
skip_preparer
|
Skip preparing the arguments for the request. Defaults to
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
RoutedFunction
|
A routed function that sends a PUT request according to the specified path and validates
TYPE:
|
_RouteDecorator
|
its arguments based on type annotations. |
RAISES | DESCRIPTION |
---|---|
ValidationError
|
If type validation of arguments fails. |
Source code in sensei/_internal/_core/router.py
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 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 |
|
delete
¶
delete(path, /, *, default_case=None, query_case=None, cookie_case=None, header_case=None, response_case=None, skip_finalizer=False, skip_preparer=False)
Create a route using the DELETE method.
This decorator transforms a function into a routed function that sends a DELETE request to the specified path. Routed function handles parameter case conversion, argument validation, argument preparation, response finalizing.
PARAMETER | DESCRIPTION |
---|---|
path
|
The relative path of the route.
TYPE:
|
default_case
|
Case converter for all parameters. Defaults to using the router's default.
TYPE:
|
query_case
|
Case converter for query parameters. Defaults to using the router's default.
TYPE:
|
cookie_case
|
Case converter for cookie parameters. Defaults to using the router's default.
TYPE:
|
header_case
|
Case converter for header parameters. Defaults to using the router's default.
TYPE:
|
response_case
|
Case converter for JSON response. Defaults to using the router's default.
TYPE:
|
skip_finalizer
|
Skip JSON finalizer, when finalizing response. Defaults to
TYPE:
|
skip_preparer
|
Skip preparing the arguments for the request. Defaults to
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
RoutedFunction
|
A routed function that sends a DELETE request according to the specified path and validates
TYPE:
|
_RouteDecorator
|
its arguments based on type annotations. |
RAISES | DESCRIPTION |
---|---|
ValidationError
|
If type validation of arguments fails. |
Source code in sensei/_internal/_core/router.py
681 682 683 684 685 686 687 688 689 690 691 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 |
|
head
¶
head(path, /, *, default_case=None, query_case=None, cookie_case=None, header_case=None, response_case=None, skip_finalizer=False, skip_preparer=False)
Create a route using the HEAD method.
This decorator transforms a function into a routed function that sends a HEAD request to the specified path. Routed function handles parameter case conversion, argument validation, argument preparation, response finalizing.
PARAMETER | DESCRIPTION |
---|---|
path
|
The relative path of the route.
TYPE:
|
default_case
|
Case converter for all parameters. Defaults to using the router's default.
TYPE:
|
query_case
|
Case converter for query parameters. Defaults to using the router's default.
TYPE:
|
cookie_case
|
Case converter for cookie parameters. Defaults to using the router's default.
TYPE:
|
header_case
|
Case converter for header parameters. Defaults to using the router's default.
TYPE:
|
response_case
|
Case converter for JSON response. Defaults to using the router's default.
TYPE:
|
skip_finalizer
|
Skip JSON finalizer, when finalizing response. Defaults to
TYPE:
|
skip_preparer
|
Skip preparing the arguments for the request. Defaults to
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
RoutedFunction
|
A routed function that sends a HEAD request according to the specified path and validates
TYPE:
|
_RouteDecorator
|
its arguments based on type annotations. |
RAISES | DESCRIPTION |
---|---|
ValidationError
|
If type validation of arguments fails. |
Source code in sensei/_internal/_core/router.py
744 745 746 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 |
|
options
¶
options(path, /, *, default_case=None, query_case=None, cookie_case=None, header_case=None, response_case=None, skip_finalizer=False, skip_preparer=False)
Create a route using the OPTIONS method.
This decorator transforms a function into a routed function that sends a OPTIONS request to the specified path. Routed function handles parameter case conversion, argument validation, argument preparation, response finalizing.
PARAMETER | DESCRIPTION |
---|---|
path
|
The relative path of the route.
TYPE:
|
default_case
|
Case converter for all parameters. Defaults to using the router's default.
TYPE:
|
query_case
|
Case converter for query parameters. Defaults to using the router's default.
TYPE:
|
cookie_case
|
Case converter for cookie parameters. Defaults to using the router's default.
TYPE:
|
header_case
|
Case converter for header parameters. Defaults to using the router's default.
TYPE:
|
response_case
|
Case converter for JSON response. Defaults to using the router's default.
TYPE:
|
skip_finalizer
|
Skip JSON finalizer, when finalizing response. Defaults to
TYPE:
|
skip_preparer
|
Skip preparing the arguments for the request. Defaults to
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
RoutedFunction
|
A routed function that sends a OPTIONS request according to the specified path and validates
TYPE:
|
_RouteDecorator
|
its arguments based on type annotations. |
RAISES | DESCRIPTION |
---|---|
ValidationError
|
If type validation of arguments fails. |
Source code in sensei/_internal/_core/router.py
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 |
|