wag-managment-api-service-v.../ApiEvents/base_request_model.py

78 lines
1.9 KiB
Python

"""
Base request models for API endpoints.
This module provides base request models that can be used across different endpoints
to ensure consistent request handling and validation.
"""
from typing import Dict, Any, Generic, TypeVar, Optional, Union, get_args
from pydantic import BaseModel, Field, ConfigDict, RootModel
T = TypeVar("T")
class EndpointBaseRequestModel(BaseModel):
data: dict = Field(..., description="Data to be sent with the request")
class Config:
json_schema_extra = {
"data": {
"key": "value",
}
}
class BaseRequestModel(RootModel[T], Generic[T]):
"""Base model for all API requests."""
model_config = ConfigDict(
json_schema_extra={
"example": {"base": "example"}
} # Will be populated by subclasses
)
class DictRequestModel(RootModel[Dict[str, Any]]):
"""Request model for endpoints that accept dictionary data."""
model_config = ConfigDict(
json_schema_extra={
"example": {
"key1": "value1",
"key2": "value2",
"nested": {"inner_key": "inner_value"},
}
}
)
class SuccessResponse(BaseModel):
"""Standard success response model."""
token: str = Field(..., example="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...")
user_info: Dict[str, Any] = Field(
...,
example={
"id": "123",
"username": "john.doe",
"email": "john@example.com",
"role": "user",
},
)
model_config = ConfigDict(
json_schema_extra={
"example": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user_info": {
"id": "123",
"username": "john.doe",
"email": "john@example.com",
"role": "user",
},
}
}
)