Map request data into pydantic models
Project description
Python request mapper
Map and validate incoming request data to Pydantic models. Request-mapper is framework agnostic with first-party support for Flask.
Examples
# Add this in your app initialization code.
setup_mapper(
integration=FlaskIntegration(
# Automatically decorate views to avoid adding @map_request to every view.
# Will not incur any performance overhead for views that don't use request-mapper.
# If map_views is enabled, setup_mapper must be called after
# all views have been registered!
map_views=True,
# Register an error handler that returns 422 alongside pydantic validation errors
# when the request cannot be mapped.
add_error_handler=True,
)
)
# Define models using Pydantic v1 or v2.
class PostCreateRequest(BaseModel):
title: str
content: str
class PostFilterQuery(BaseModel):
status: PostStatus | None = None
@app.get("/posts")
# Map data from the current request.
def post_list_all(query: FromQueryString[PostFilterQuery]) -> PaginatedResponse[Post]:
# "query" is a valid pydantic model at this point.
return PaginatedResponse(...)
@app.post("/posts")
def post_create(body: FromRequestBody[PostCreateRequest]) -> PostCreateResponse:
# "body" is a valid pydantic model at this point.
return PostCreateResponse(...)
Quickstart
pip install request-mapper
.- In your application setup, call
mapper.setup_mapper
with the integration of your choice. - Decorate targets with
@map_request
(Optional when using flask integration) - Map request data using one of the provided annotated types
FromQueryString[T]
orAnnotated[T, QueryStringMapping()]
FromRequestBody[T]
orAnnotated[T, RequestBodyMapping()]
FromFormData[T]
orAnnotated[T, FormDataMapping()]
Integrations
Flask
- Pull data from flask's request args, json and form data.
- Optional: Automatically map views without having to decorate them.
- Views which do not use request mapper will not incur any performance penalty.
- When using this feature the call to
setup_mapper
must be AFTER all views are registered.
- Optional: Set up an error handler for validation errors which makes the api respond with a 422.
Custom integrations
You can create your own integration by inheriting BaseIntegration
and supplying an instance of that
to the setup_mapper
call.
Note that you must decorate your views with @map_request
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
request_mapper-0.1.0.tar.gz
(5.5 kB
view hashes)
Built Distribution
Close
Hashes for request_mapper-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4aa75b3eafb48dfbeee733aac468fa18fcfe879610a9a6965ad6711c7203afa6 |
|
MD5 | 02ecaecb2fc8649ff33e47594ab8d1af |
|
BLAKE2b-256 | 1621133137a85b7b8801659b1709df414fafca7458948895354546aed980760f |