-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
283 lines (228 loc) · 8.57 KB
/
Copy path__init__.py
File metadata and controls
283 lines (228 loc) · 8.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
"""Tiny FastAPI-compatible façade for unit tests."""
from __future__ import annotations
import inspect
from dataclasses import dataclass
from typing import (
Any,
Callable,
Dict,
List,
Mapping,
Optional,
Tuple,
Type,
get_type_hints,
)
from types import SimpleNamespace
try: # pragma: no cover - optional dependency for typing checks
from pydantic import BaseModel, ValidationError
except ModuleNotFoundError: # pragma: no cover - the stub ships alongside
from pydantic import BaseModel, ValidationError # type: ignore
class Request:
def __init__(self, headers: Optional[Mapping[str, str]] | None = None, body: bytes | None = None) -> None:
self.headers = dict(headers or {})
self._body = body or b""
async def body(self) -> bytes:
return self._body
class HTTPException(Exception):
def __init__(self, status_code: int, detail: Any) -> None:
super().__init__(str(detail))
self.status_code = status_code
self.detail = detail
def Depends(dependency: Callable[..., Any] | None = None) -> Callable[..., Any] | None:
return dependency
def Query(default: Any = None, description: str | None = None) -> Any:
return default
def File(default: Any = None, **_: Any) -> Any:
return default
def Form(default: Any = None, **_: Any) -> Any:
return default
class UploadFile:
def __init__(
self, filename: str | None = None, content_type: str | None = None
) -> None:
self.filename = filename or ""
self.content_type = content_type
self._buffer = bytearray()
async def read(self, size: int = -1) -> bytes: # pragma: no cover - simple stub
if size is None or size < 0:
size = len(self._buffer)
data = self._buffer[:size]
if size >= len(self._buffer):
self._buffer.clear()
else:
del self._buffer[:size]
return bytes(data)
class RequestValidationError(Exception):
def __init__(self, errors: List[Dict[str, Any]]) -> None:
super().__init__("Validation failed")
self.errors = errors
@dataclass
class _Route:
method: str
path: str
endpoint: Callable[..., Any]
def __post_init__(self) -> None:
self.signature = inspect.signature(self.endpoint)
raw_segments = [
segment for segment in self.path.strip("/").split("/") if segment
]
self._segments: List[Tuple[str, Optional[str]]] = []
for segment in raw_segments:
if segment.startswith("{") and segment.endswith("}"):
self._segments.append(("param", segment[1:-1]))
else:
self._segments.append(("literal", segment))
try:
self._type_hints = get_type_hints(self.endpoint)
except Exception: # pragma: no cover - fallback for dynamic globals
self._type_hints = {}
def match(self, method: str, path: str) -> Optional[Mapping[str, str]]:
if method != self.method:
return None
segments = [segment for segment in path.strip("/").split("/") if segment]
if len(segments) != len(self._segments):
return None
params: Dict[str, str] = {}
for (kind, value), segment in zip(self._segments, segments):
if kind == "literal" and value != segment:
return None
if kind == "param":
params[value] = segment
return params
def invoke(self, params: Mapping[str, str], body: Optional[Dict[str, Any]]) -> Any:
kwargs: Dict[str, Any] = {}
for name, parameter in self.signature.parameters.items():
annotation = self._type_hints.get(name, parameter.annotation)
if name in params:
kwargs[name] = params[name]
continue
if isinstance(annotation, type) and issubclass(annotation, BaseModel):
model_data = body or {}
try:
kwargs[name] = annotation(**model_data)
except ValidationError as exc:
raise RequestValidationError(exc.errors()) from exc
continue
if name == "body":
kwargs[name] = body
elif parameter.default is not inspect._empty:
kwargs[name] = parameter.default
else:
kwargs[name] = None
result = self.endpoint(**kwargs)
if inspect.iscoroutine(result):
import asyncio
return asyncio.run(result)
return result
class APIRouter:
def __init__(
self, prefix: str = "", tags: Optional[List[str]] | None = None
) -> None:
self.prefix = prefix or ""
self.tags = tags or []
self._routes: List[_Route] = []
def _register(
self, method: str, path: str
) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
full_path = f"{self.prefix}{path}"
def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
self._routes.append(_Route(method, full_path, func))
return func
return decorator
def post(
self, path: str, **_: Any
) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
return self._register("POST", path)
def get(
self, path: str, **_: Any
) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
return self._register("GET", path)
def include_router(self, router: "APIRouter", prefix: str = "") -> None:
for route in router._routes:
combined_path = f"{prefix}{route.path}" if prefix else route.path
self._routes.append(_Route(route.method, combined_path, route.endpoint))
def add_api_route(
self,
path: str,
endpoint: Callable[..., Any],
methods: Optional[List[str]] = None,
**_: Any,
) -> None:
for method in methods or ["GET"]:
self._routes.append(_Route(method, f"{self.prefix}{path}", endpoint))
class FastAPI:
def __init__(
self,
title: str | None = None,
version: str | None = None,
lifespan: Any | None = None,
) -> None:
self.title = title
self.version = version
self._routes: List[_Route] = []
self._middleware: List[tuple[Any, Dict[str, Any]]] = []
self.user_middleware: List[SimpleNamespace] = []
self._lifespan = lifespan
def post(
self, path: str, summary: str | None = None, **_: Any
) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
return self._register("POST", path)
def get(
self, path: str, summary: str | None = None, **_: Any
) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
return self._register("GET", path)
def add_middleware(self, middleware_class: Any, **options: Any) -> None:
self._middleware.append((middleware_class, options))
self.user_middleware.append(
SimpleNamespace(cls=middleware_class, options=options)
)
def _register(
self, method: str, path: str
) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
self._routes.append(_Route(method, path, func))
return func
return decorator
def include_router(self, router: APIRouter, prefix: str = "") -> None:
for route in router._routes:
combined_path = f"{prefix}{route.path}" if prefix else route.path
self._routes.append(_Route(route.method, combined_path, route.endpoint))
# Internal helpers for the TestClient
def _handle(
self,
method: str,
path: str,
body: Optional[Dict[str, Any]],
headers: Optional[Dict[str, Any]] = None,
) -> Any:
for route in self._routes:
params = route.match(method, path)
if params is not None:
return route.invoke(params, body)
raise HTTPException(status_code=404, detail="Not Found")
class _StatusCodes:
HTTP_201_CREATED = 201
HTTP_200_OK = 200
HTTP_400_BAD_REQUEST = 400
HTTP_401_UNAUTHORIZED = 401
HTTP_403_FORBIDDEN = 403
HTTP_404_NOT_FOUND = 404
HTTP_413_REQUEST_ENTITY_TOO_LARGE = 413
HTTP_415_UNSUPPORTED_MEDIA_TYPE = 415
HTTP_422_UNPROCESSABLE_ENTITY = 422
status = _StatusCodes()
from .testclient import TestClient # noqa: E402 (import after FastAPI definition)
__all__ = [
"FastAPI",
"APIRouter",
"HTTPException",
"Request",
"Depends",
"Query",
"File",
"UploadFile",
"RequestValidationError",
"status",
"TestClient",
]