Declare and validate HTTP query and path parameters in aiohttp
Project description
aiohttp-parameter-parser
Declare and validate HTTP query and path parameters in aiohttp
views.
Currently only path and URL query parameter location are supported.
Basic usage:
from aiohttp import web
from aiohttp_parameter_parser import ParameterView
class ExampleView(ParameterView):
async def get(self) -> web.Response:
my_list_of_ints = self.query_parameter(
"parameter_name_in_request",
required=True,
is_array=True,
max_items=6, # len() restriction for list
is_int=True,
max_value=1337, # maximum allowed item value
)
# If provided parameter is of wrong type or missing, a default
# HTTP 400 response is returned to client.
my_str = self.path_parameter( # default type for parsed parameter is str
"a_string_parameter_name",
choices=["foo", "bar", "baz"], # enum
)
return web.json_response({
"received_array_of_ints": my_list_of_ints,
"received_str": my_str,
})
Sometimes you want to return custom error response instead of default HTTP 400. Here's an example how to raise custom exception if validation fails:
from aiohttp import web
from aiohttp_parameter_parser import ParameterView
class CustomErrorResponseView(ParameterView):
def validation_error_handler(self, msg: str) -> web.Response:
# just override this method of base class
# 'msg' is a human-readable explanation of validation error
j = {
"ok": False,
"data": None,
"error": {
"description": msg,
},
}
# you can use raise or return here
return web.json_response(status=418, data=j)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for aiohttp-parameter-parser-0.1.5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | cbc0547e3ffea2a98af75fcd560df5c3fb479b98c7e4584820eaf75a2123883d |
|
MD5 | de3ae6f271492351f1bb105c4a99b420 |
|
BLAKE2b-256 | 53971a81ec70c64a80b1416fd71518226ee8ab96dd85feab0831eab9029d41b6 |
Close
Hashes for aiohttp_parameter_parser-0.1.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54b76c0f4484127d7ff2db5a8b2568be6356c095339a9e26055fc66a452a84e2 |
|
MD5 | ae4f43931e67d66909ee76f7e423b3d6 |
|
BLAKE2b-256 | d0d908245afdddd74e75d91cd44b2447b7705b6062f0ccbbaccf5c6b4a247622 |