middleware and respnse models updated
This commit is contained in:
@@ -5,11 +5,19 @@ This module provides base request models that can be used across different endpo
|
||||
to ensure consistent request handling and validation.
|
||||
"""
|
||||
|
||||
from typing import Dict, Any, TypeVar
|
||||
from pydantic import BaseModel, Field, ConfigDict
|
||||
from typing import Union, Optional, Any
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from ApiLayers.ApiValidations.Custom.token_objects import (
|
||||
EmployeeTokenObject,
|
||||
OccupantTokenObject,
|
||||
)
|
||||
from ApiLayers.ApiValidations.Custom.wrapper_contexts import AuthContext, EventContext
|
||||
|
||||
|
||||
T = TypeVar("T")
|
||||
TokenDictType = Union[
|
||||
EmployeeTokenObject, OccupantTokenObject
|
||||
] # Type aliases for common types
|
||||
|
||||
|
||||
class EndpointBaseRequestModel(BaseModel):
|
||||
@@ -17,35 +25,63 @@ class EndpointBaseRequestModel(BaseModel):
|
||||
data: dict = Field(..., description="Data to be sent with the request")
|
||||
|
||||
class Config:
|
||||
json_schema_extra = {
|
||||
"data": {
|
||||
"key": "value",
|
||||
}
|
||||
}
|
||||
json_schema_extra = {"data": {"key": "value"}}
|
||||
|
||||
|
||||
class SuccessResponse(BaseModel):
|
||||
"""Standard success response model."""
|
||||
class ContextRetrievers:
|
||||
"""Utility class to retrieve context from functions."""
|
||||
|
||||
data: Dict[str, Any] = Field(
|
||||
...,
|
||||
example={
|
||||
"id": "123",
|
||||
"username": "john.doe",
|
||||
"email": "john@example.com",
|
||||
"role": "user",
|
||||
},
|
||||
)
|
||||
is_auth: bool = False
|
||||
is_event: bool = False
|
||||
key_: str = ""
|
||||
|
||||
model_config = ConfigDict(
|
||||
json_schema_extra={
|
||||
"example": {
|
||||
"data": {
|
||||
"id": "123",
|
||||
"username": "john.doe",
|
||||
"email": "john@example.com",
|
||||
"role": "user",
|
||||
},
|
||||
}
|
||||
}
|
||||
)
|
||||
def __init__(self, func):
|
||||
self.func = func
|
||||
if hasattr(self.func, "auth_context"):
|
||||
self.is_auth = True
|
||||
self.key_ = "auth_context"
|
||||
elif hasattr(self.func, "event_context"):
|
||||
self.is_event = hasattr(self.func, "event_context")
|
||||
self.key_ = "event_context"
|
||||
|
||||
@property
|
||||
def key(self) -> Union[str, None]:
|
||||
"""Retrieve key context from a function."""
|
||||
return self.key_
|
||||
|
||||
@property
|
||||
def context(self) -> Union[AuthContext, EventContext, None]:
|
||||
"""Retrieve authentication or event context from a function."""
|
||||
if self.is_auth:
|
||||
return getattr(self.func, "auth_context", None)
|
||||
elif self.is_event:
|
||||
return getattr(self.func, "event_context", None)
|
||||
return None
|
||||
|
||||
@property
|
||||
def request(self) -> Union[Any, None]:
|
||||
"""Retrieve request context from a function."""
|
||||
return getattr(self.context, "request", None)
|
||||
|
||||
@property
|
||||
def token(self) -> TokenDictType:
|
||||
"""Retrieve token context from a function."""
|
||||
if self.is_auth or self.is_event:
|
||||
return getattr(self.context, "auth", None)
|
||||
|
||||
@property
|
||||
def url(self) -> Union[str, None]:
|
||||
"""Retrieve URL context from a function."""
|
||||
return getattr(self.context, "url", None)
|
||||
|
||||
@property
|
||||
def code(self) -> Union[str, None]:
|
||||
"""Retrieve code context from a function."""
|
||||
if self.is_event:
|
||||
return getattr(self.context, "code", None)
|
||||
return None
|
||||
|
||||
@property
|
||||
def base(self) -> Optional[dict[str, Any]]:
|
||||
"""Retrieve base request model from a function."""
|
||||
return getattr(self.context, "base", None)
|
||||
|
||||
Reference in New Issue
Block a user