|
5 | 5 | import sys |
6 | 6 | from collections.abc import Callable |
7 | 7 | from pathlib import Path |
8 | | -from typing import Annotated, NamedTuple, Optional, Union, get_args, get_origin |
| 8 | +from typing import ( |
| 9 | + Annotated, |
| 10 | + Any, |
| 11 | + Generic, |
| 12 | + NamedTuple, |
| 13 | + Optional, |
| 14 | + TypeVar, |
| 15 | + Union, |
| 16 | + get_args, |
| 17 | + get_origin, |
| 18 | +) |
9 | 19 |
|
10 | 20 | from common_library.json_serialization import json_dumps |
11 | 21 | from common_library.pydantic_fields_extension import get_type |
12 | 22 | from fastapi import Query |
13 | | -from models_library.basic_types import LogLevel |
14 | | -from pydantic import BaseModel, ConfigDict, Field, Json, create_model |
| 23 | +from pydantic import BaseModel, Json, create_model |
15 | 24 | from pydantic.fields import FieldInfo |
16 | 25 |
|
17 | 26 | CURRENT_DIR = Path(sys.argv[0] if __name__ == "__main__" else __file__).resolve().parent |
@@ -78,43 +87,12 @@ def as_query(model_class: type[BaseModel]) -> type[BaseModel]: |
78 | 87 | return create_model(new_model_name, **fields) |
79 | 88 |
|
80 | 89 |
|
81 | | -class Log(BaseModel): |
82 | | - level: LogLevel | None = Field("INFO", description="log level") |
83 | | - message: str = Field( |
84 | | - ..., |
85 | | - description="log message. If logger is USER, then it MUST be human readable", |
86 | | - ) |
87 | | - logger: str | None = Field( |
88 | | - None, description="name of the logger receiving this message" |
89 | | - ) |
90 | | - |
91 | | - model_config = ConfigDict( |
92 | | - json_schema_extra={ |
93 | | - "example": { |
94 | | - "message": "Hi there, Mr user", |
95 | | - "level": "INFO", |
96 | | - "logger": "user-logger", |
97 | | - } |
98 | | - } |
99 | | - ) |
100 | | - |
101 | | - |
102 | | -class ErrorItem(BaseModel): |
103 | | - code: str = Field( |
104 | | - ..., |
105 | | - description="Typically the name of the exception that produced it otherwise some known error code", |
106 | | - ) |
107 | | - message: str = Field(..., description="Error message specific to this item") |
108 | | - resource: str | None = Field( |
109 | | - None, description="API resource affected by this error" |
110 | | - ) |
111 | | - field: str | None = Field(None, description="Specific field within the resource") |
| 90 | +ErrorT = TypeVar("ErrorT") |
112 | 91 |
|
113 | 92 |
|
114 | | -class Error(BaseModel): |
115 | | - logs: list[Log] | None = Field(None, description="log messages") |
116 | | - errors: list[ErrorItem] | None = Field(None, description="errors metadata") |
117 | | - status: int | None = Field(None, description="HTTP error code") |
| 93 | +class EnvelopeE(BaseModel, Generic[ErrorT]): |
| 94 | + error: ErrorT | None = None |
| 95 | + data: Any | None = None |
118 | 96 |
|
119 | 97 |
|
120 | 98 | class ParamSpec(NamedTuple): |
|
0 commit comments