Path
This section explains how to extract and validate path parameters in Rapidy
.
You can validate data using any type supported by pydantic
.
Description
Path parameters allow you to create dynamic routes in your application.
You can define path parameters using Python's formatted string syntax:
from rapidy.http import get, PathParam
@get('/{user_id}')
async def handler(
user_id: str = PathParam(),
) -> ...:
For more details on dynamic routes in aiohttp
, see here.
Extracting a Single Path Parameter
PathParam
allows you to extract a single path parameter.
from rapidy.http import get, PathParam
@get('/{user_id}')
async def handler(
user_id: str = PathParam(),
) -> ...:
from rapidy.http import get, PathParam
@get('/{user_id}/{user_data}')
async def handler(
user_id: str = PathParam(),
user_data: str = PathParam(),
) -> ...:
Extracting All Path Parameters
PathParams
allows you to extract all path parameters at once.
Extraction into a Predefined Schema
pydantic.BaseModel
from pydantic import BaseModel
from rapidy.http import get, PathParams
class PathData(BaseModel):
user_id: str
user_data: str
@get('/{user_id}/{user_data}')
async def handler(
path_data: PathData = PathParams(),
) -> ...:
dataclasses.dataclass
dataclasses.dataclass
is supported as a model type, but setting an alias
using standard dataclasses
tools is not possible.
from dataclasses import dataclass
from rapidy.http import get, PathParams
@dataclass
class PathData:
user_id: str
user_data: str
@get('/{user_id}/{user_data}')
async def handler(
path_data: PathData = PathParams(),
) -> ...:
Extraction into a Dictionary
from rapidy.http import get, PathParams
@get('/{user_id}/{user_data}')
async def handler(
path_data: dict[str, str] = PathParams(),
) -> ...:
# {'user_id': ..., 'user_data': ...}
Extraction Without Validation
Disabling validation is not recommended.
If validation is disabled, parameters are returned in the base aiohttp
structure:
PathParam
→str
PathParams
→dict[str, str]
Ways to Disable Validation
Explicit Disabling
from rapidy.http import get, PathParam, PathParams
@get('/')
async def handler_1(
user_id: int = PathParam(validate=False)
) -> ...:
# ...
@get('/')
async def handler_2(
path_data: int = PathParams(validate=False)
) -> ...:
# {'user_id': ..., 'user_data': ...}
Using Any
from typing import Any
@get('/')
async def handler_1(
user_id: Any = PathParam()
) -> ...:
# "0.0.0.0:8080"
@get('/')
async def handler_2(
path_data: Any = PathParams()
) -> ...:
# {'user_id': ..., 'user_data': ...}
No Type Annotation
If the type is not specified, Any
is used by default.
@get('/')
async def handler_1(
user_id=PathParam()
) -> ...:
# ...
@get('/')
async def handler_2(
path_data=PathParams()
) -> ...:
# {'user_id': ..., 'user_data': ...}
Default Values
PathParam
and PathParams
do not support default values.
This is an intentional architectural limitation: without it, dynamic routing cannot be properly implemented.
Attempting to set default
or default_factory
for a path parameter
will raise a ParameterCannotUseDefaultError
or ParameterCannotUseDefaultFactoryError
exception.
Warnings and Specifics
Using PathParam
and PathParams
together
You cannot use Header
and Headers
in the same handler.
@get('/{user_id}/{user_data}')
async def handler(
user_id: str = PathParam(),
path_data: PathData = PathParams(),
) -> ...:
When the application starts, an AnotherDataExtractionTypeAlreadyExistsError
exception will be raised.
------------------------------
Attribute with this data extraction type cannot be added to the handler - another data extraction type is already used in the handler.
Handler path: `main.py`
Handler name: `handler`
Attribute name: `path_data`
------------------------------
The alias
Attribute for PathParams
The alias
attribute does not work for PathParams()
.
@get('/{user_id}/{user_data}')
async def handler(
path_data: PathData = PathParams(alias='SomeName'), # <-- alias not working
) -> ...:
How Raw Data is Extracted
In Rapidy
, the match_info
method of the Request
object is used,
after which the obtained data is passed to pydantic
for validation.
How Extraction Works Inside Rapidy
Rapidy
uses built-in aiohttp
mechanisms.
For more information about the aiohttp.Request
object and data extraction methods, see
here.