You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Current pydantic request/response handling decorator lacks consistency due to mixed usage of aiohttp's web.Request and Pydantic models. So we need to standardize and maintain consistency in API interfaces
Current decorator combines both request/response handling and error handling, creating a need for separation of concerns
Required Features
Implement Pydantic middleware to handle all request components (body, query params, path variables) through Pydantic models instead of web.Request
Deploy as subapp middleware to isolate changes within new vFolder API application without affecting other API endpoints
Implementation (Detail)
As-Is
# 1. Global Auth Middleware@web.Middlewareasyncdefauth_middleware(request, handler):
# 1) Initialize auth state in requestrequest.update({
"is_authorized": False,
"user": None,
"keypair": None
})
# 2) Check if handler requires authifnothandler.auth_required:
returnawaithandler(request)
# 3) Process authorization and stor info in request objifauthenticated:
request.update({
"is_authorized": True,
"user": user_info,
"keypair": keypair_info
})
# 2. Pydantic Param handlerdefpydantic_params_api_handler(param_model, query_model=None):
asyncdefwrapped(request):
try:
# 1) Validate body/query paramsifrequest.has_body:
params=param_model.validate(request.body)
ifquery_model:
query_model=query_model.validate(request.query)
else:
params=param_model.valdiate(request.query)
# 2) Exec handlerresult=awaithandler(request, params)
# 3) If validation failed make custom msg and raise InvalidAPIParameters error(Custom error)exceptValidationErrorase:
message=make_custom_msg(e)
raiseInvalidAPIParameters(message)
# 4) Convert Pydantic Response model into aiohttp web.Responsereturnweb.json_response(result.model_dump(mode="json"))
returnwrapped# 3. Usage Example@auth_required@pydantic_params_api_handler(RequestModel)asyncdefhandler(request, params):
user=request["user"] # Set from auth_middlewaredata=params# Validated by pydantic
To-Be
Impact
Consistent Request/Response processing logic across all API endpoints
Easy API documentation generation by using Pydantic
Testing Scenarios
Verify all request components (body, params, path vars) are correctly processed through Pydantic models and validate error handling for each component
The text was updated successfully, but these errors were encountered:
Motivation
Required Features
Implementation (Detail)
As-Is
To-Be
Impact
Testing Scenarios
The text was updated successfully, but these errors were encountered: