-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(api): move request validation to pydantic models
Use Python builtin ContextVar and context manager together with FastAPI dependencies to provide request context such as path variables to pydantic model validation.
- Loading branch information
1 parent
300f766
commit de22d59
Showing
11 changed files
with
189 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from contextlib import asynccontextmanager | ||
from contextvars import ContextVar | ||
from dataclasses import dataclass | ||
|
||
from fastapi import Request | ||
|
||
|
||
@dataclass | ||
class RequestContext: | ||
path_parameters: dict | ||
|
||
|
||
request_context: ContextVar[RequestContext] = ContextVar("request_context") | ||
|
||
|
||
@asynccontextmanager | ||
async def request_context_manager(path_parameters: dict): | ||
token = request_context.set(RequestContext(path_parameters=path_parameters)) | ||
try: | ||
yield | ||
finally: | ||
request_context.reset(token) | ||
|
||
|
||
async def set_request_context(request: Request): | ||
"""Set request context for the duration of a request. | ||
After leaving the context manager (after the request is processed) | ||
the request context is resented again. | ||
""" | ||
async with request_context_manager(request.path_params): | ||
yield |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...ors_attribute_completeness_with_invalid_attribute_for_topic-headers0-schema0.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Value error, Invalid combination of attribute maxspeed and topic building-count. Topic building-count supports these attributes: height, house-number, address-street, address-city, address-postcode, address-country, address-state, address-suburb, address-district, address-housenumber, building-levels, roof-shape, roof-levels, building-material, roof-material, roof-colour, building-colour |
1 change: 1 addition & 0 deletions
1
...ors_attribute_completeness_with_invalid_attribute_for_topic-headers1-schema1.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Value error, Invalid combination of attribute maxspeed and topic building-count. Topic building-count supports these attributes: height, house-number, address-street, address-city, address-postcode, address-country, address-state, address-suburb, address-district, address-housenumber, building-levels, roof-shape, roof-levels, building-material, roof-material, roof-colour, building-colour |
Oops, something went wrong.