61 lines
1.6 KiB
Python
61 lines
1.6 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 BaseRequestModel(RootModel[T], Generic[T]):
|
|
"""Base model for all API requests."""
|
|
model_config = ConfigDict(
|
|
json_schema_extra={"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",
|
|
},
|
|
}
|
|
}
|
|
)
|