Routed Function¶
sensei._internal._core._types.RoutedFunction
¶
Bases: Callable[..., _RT]
Function produced by routed decorators, such as:
@router.get
@router.post
@router.put
@router.delete
@router.patch
@router.head
@router.options
Example
from sensei import Router
router = Router('https://api.example.com')
@router.get('/users')
def get_user(id: int) -> User:
pass
prepare
¶
prepare(preparer=None)
Attach preparer to the routed function. Preparer is a function that takes an instance of Args
as the argument
and returns the modified. Preparers are used for request preparation. That means adding or changing arguments
before request. Can be represented as async function.
Preparers are executed after internal argument parsing. So, all request parameters are available in
Args
model within a preparer.
Example
from sensei import APIModel, format_str, Router, Args
from pydantic import NonNegativeInt, EmailStr
router = Router('https://api.example.com')
class User(APIModel):
id: NonNegativeInt
email: EmailStr
nickname: str
@router.patch('/users/{id_}')
def update(
self,
name: str,
job: str
) -> None:
pass
@update.prepare
def _update_in(self, args: Args) -> Args:
args.url = format_str(args.url, {'id_': self.id})
return args
PARAMETER | DESCRIPTION |
---|---|
preparer
|
Function that takes an instance of
TYPE:
|
Returns (Preparer): Function that was passed
Source code in sensei/_internal/_core/_types.py
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 |
|
finalize
¶
finalize(finalizer=None)
Attach response finalizer to the routed function. Response Finalizer is a function that takes an instance of
httpx.Response
as the argument and returns the result of calling the associated routed function.
The return value must be of the same type as the routed function. Can be represented as async function.
Response Finalizers are used for response transformation, which can't be performed automatically if you set a corresponding response type from the category of automatically handled.
Example
from sensei import Router, APIModel, Form
from pydantic import EmailStr
from typing import Annotated
from httpx import Response
router = Router('https://api.example.com')
class UserCredentials(APIModel):
email: EmailStr
password: str
@router.post('/register')
def sign_up(user: Annotated[UserCredentials, Form(embed=False)]) -> str:
pass
@sign_up.finalize
def _sign_up_out(response: Response) -> str:
print(f'Finalizing response for request {response.request.url}')
return response.json()['token']
token = sign_up(UserCredentials(
email='[email protected]',
password='secret_password')
)
print(f'JWT token: {token}')
PARAMETER | DESCRIPTION |
---|---|
finalizer
|
Function that takes an instance of
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
ResponseFinalizer
|
Function that was passed |
Source code in sensei/_internal/_core/_types.py
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 |
|