-
-
Notifications
You must be signed in to change notification settings - Fork 470
/
Copy pathrouter.py
450 lines (422 loc) · 15.3 KB
/
router.py
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
from typing import (
TYPE_CHECKING,
Any,
Callable,
Dict,
Iterator,
List,
Optional,
Tuple,
Union,
)
from django.urls import URLPattern
from django.urls import path as django_path
from django.utils.module_loading import import_string
from ninja.constants import NOT_SET, NOT_SET_TYPE
from ninja.errors import ConfigError
from ninja.operation import PathView
from ninja.throttling import BaseThrottle
from ninja.parser import Parser
from ninja.types import TCallable
from ninja.utils import normalize_path, replace_path_param_notation
if TYPE_CHECKING:
from ninja import NinjaAPI # pragma: no cover
__all__ = ["Router"]
class Router:
def __init__(
self,
*,
auth: Any = NOT_SET,
throttle: Union[BaseThrottle, List[BaseThrottle], NOT_SET_TYPE] = NOT_SET,
tags: Optional[List[str]] = None,
by_alias: Optional[bool] = None,
exclude_unset: Optional[bool] = None,
exclude_defaults: Optional[bool] = None,
exclude_none: Optional[bool] = None,
) -> None:
self.api: Optional[NinjaAPI] = None
self.auth = auth
self.throttle = throttle
self.tags = tags
self.by_alias = by_alias
self.exclude_unset = exclude_unset
self.exclude_defaults = exclude_defaults
self.exclude_none = exclude_none
self.path_operations: Dict[str, PathView] = {}
self._routers: List[Tuple[str, Router]] = []
def get(
self,
path: str,
*,
auth: Any = NOT_SET,
throttle: Union[BaseThrottle, List[BaseThrottle], NOT_SET_TYPE] = NOT_SET,
response: Any = NOT_SET,
operation_id: Optional[str] = None,
summary: Optional[str] = None,
description: Optional[str] = None,
tags: Optional[List[str]] = None,
deprecated: Optional[bool] = None,
by_alias: Optional[bool] = None,
exclude_unset: Optional[bool] = None,
exclude_defaults: Optional[bool] = None,
exclude_none: Optional[bool] = None,
url_name: Optional[str] = None,
include_in_schema: bool = True,
openapi_extra: Optional[Dict[str, Any]] = None,
parser: Optional[Parser] = None,
) -> Callable[[TCallable], TCallable]:
return self.api_operation(
["GET"],
path,
auth=auth,
throttle=throttle,
response=response,
operation_id=operation_id,
summary=summary,
description=description,
tags=tags,
deprecated=deprecated,
by_alias=by_alias,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
url_name=url_name,
include_in_schema=include_in_schema,
openapi_extra=openapi_extra,
parser=parser,
)
def post(
self,
path: str,
*,
auth: Any = NOT_SET,
throttle: Union[BaseThrottle, List[BaseThrottle], NOT_SET_TYPE] = NOT_SET,
response: Any = NOT_SET,
operation_id: Optional[str] = None,
summary: Optional[str] = None,
description: Optional[str] = None,
tags: Optional[List[str]] = None,
deprecated: Optional[bool] = None,
by_alias: Optional[bool] = None,
exclude_unset: Optional[bool] = None,
exclude_defaults: Optional[bool] = None,
exclude_none: Optional[bool] = None,
url_name: Optional[str] = None,
include_in_schema: bool = True,
openapi_extra: Optional[Dict[str, Any]] = None,
parser: Optional[Parser] = None,
) -> Callable[[TCallable], TCallable]:
return self.api_operation(
["POST"],
path,
auth=auth,
throttle=throttle,
response=response,
operation_id=operation_id,
summary=summary,
description=description,
tags=tags,
deprecated=deprecated,
by_alias=by_alias,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
url_name=url_name,
include_in_schema=include_in_schema,
openapi_extra=openapi_extra,
parser=parser,
)
def delete(
self,
path: str,
*,
auth: Any = NOT_SET,
throttle: Union[BaseThrottle, List[BaseThrottle], NOT_SET_TYPE] = NOT_SET,
response: Any = NOT_SET,
operation_id: Optional[str] = None,
summary: Optional[str] = None,
description: Optional[str] = None,
tags: Optional[List[str]] = None,
deprecated: Optional[bool] = None,
by_alias: Optional[bool] = None,
exclude_unset: Optional[bool] = None,
exclude_defaults: Optional[bool] = None,
exclude_none: Optional[bool] = None,
url_name: Optional[str] = None,
include_in_schema: bool = True,
openapi_extra: Optional[Dict[str, Any]] = None,
parser: Optional[Parser] = None,
) -> Callable[[TCallable], TCallable]:
return self.api_operation(
["DELETE"],
path,
auth=auth,
throttle=throttle,
response=response,
operation_id=operation_id,
summary=summary,
description=description,
tags=tags,
deprecated=deprecated,
by_alias=by_alias,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
url_name=url_name,
include_in_schema=include_in_schema,
openapi_extra=openapi_extra,
parser=parser,
)
def patch(
self,
path: str,
*,
auth: Any = NOT_SET,
throttle: Union[BaseThrottle, List[BaseThrottle], NOT_SET_TYPE] = NOT_SET,
response: Any = NOT_SET,
operation_id: Optional[str] = None,
summary: Optional[str] = None,
description: Optional[str] = None,
tags: Optional[List[str]] = None,
deprecated: Optional[bool] = None,
by_alias: Optional[bool] = None,
exclude_unset: Optional[bool] = None,
exclude_defaults: Optional[bool] = None,
exclude_none: Optional[bool] = None,
url_name: Optional[str] = None,
include_in_schema: bool = True,
openapi_extra: Optional[Dict[str, Any]] = None,
parser: Optional[Parser] = None,
) -> Callable[[TCallable], TCallable]:
return self.api_operation(
["PATCH"],
path,
auth=auth,
throttle=throttle,
response=response,
operation_id=operation_id,
summary=summary,
description=description,
tags=tags,
deprecated=deprecated,
by_alias=by_alias,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
url_name=url_name,
include_in_schema=include_in_schema,
openapi_extra=openapi_extra,
parser=parser,
)
def put(
self,
path: str,
*,
auth: Any = NOT_SET,
throttle: Union[BaseThrottle, List[BaseThrottle], NOT_SET_TYPE] = NOT_SET,
response: Any = NOT_SET,
operation_id: Optional[str] = None,
summary: Optional[str] = None,
description: Optional[str] = None,
tags: Optional[List[str]] = None,
deprecated: Optional[bool] = None,
by_alias: Optional[bool] = None,
exclude_unset: Optional[bool] = None,
exclude_defaults: Optional[bool] = None,
exclude_none: Optional[bool] = None,
url_name: Optional[str] = None,
include_in_schema: bool = True,
openapi_extra: Optional[Dict[str, Any]] = None,
parser: Optional[Parser] = None,
) -> Callable[[TCallable], TCallable]:
return self.api_operation(
["PUT"],
path,
auth=auth,
throttle=throttle,
response=response,
operation_id=operation_id,
summary=summary,
description=description,
tags=tags,
deprecated=deprecated,
by_alias=by_alias,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
url_name=url_name,
include_in_schema=include_in_schema,
openapi_extra=openapi_extra,
parser=parser,
)
def api_operation(
self,
methods: List[str],
path: str,
*,
auth: Any = NOT_SET,
throttle: Union[BaseThrottle, List[BaseThrottle], NOT_SET_TYPE] = NOT_SET,
response: Any = NOT_SET,
operation_id: Optional[str] = None,
summary: Optional[str] = None,
description: Optional[str] = None,
tags: Optional[List[str]] = None,
deprecated: Optional[bool] = None,
by_alias: Optional[bool] = None,
exclude_unset: Optional[bool] = None,
exclude_defaults: Optional[bool] = None,
exclude_none: Optional[bool] = None,
url_name: Optional[str] = None,
include_in_schema: bool = True,
openapi_extra: Optional[Dict[str, Any]] = None,
parser: Optional[Parser] = None,
) -> Callable[[TCallable], TCallable]:
def decorator(view_func: TCallable) -> TCallable:
self.add_api_operation(
path,
methods,
view_func,
auth=auth,
throttle=throttle,
response=response,
operation_id=operation_id,
summary=summary,
description=description,
tags=tags,
deprecated=deprecated,
by_alias=by_alias,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
url_name=url_name,
include_in_schema=include_in_schema,
openapi_extra=openapi_extra,
parser=parser,
)
return view_func
return decorator
def add_api_operation(
self,
path: str,
methods: List[str],
view_func: Callable,
*,
auth: Any = NOT_SET,
throttle: Union[BaseThrottle, List[BaseThrottle], NOT_SET_TYPE] = NOT_SET,
response: Any = NOT_SET,
operation_id: Optional[str] = None,
summary: Optional[str] = None,
description: Optional[str] = None,
tags: Optional[List[str]] = None,
deprecated: Optional[bool] = None,
by_alias: Optional[bool] = None,
exclude_unset: Optional[bool] = None,
exclude_defaults: Optional[bool] = None,
exclude_none: Optional[bool] = None,
url_name: Optional[str] = None,
include_in_schema: bool = True,
openapi_extra: Optional[Dict[str, Any]] = None,
parser: Optional[Parser] = None,
) -> None:
if path not in self.path_operations:
path_view = PathView()
self.path_operations[path] = path_view
else:
path_view = self.path_operations[path]
by_alias = by_alias is None and self.by_alias or by_alias
exclude_unset = exclude_unset is None and self.exclude_unset or exclude_unset
exclude_defaults = (
exclude_defaults is None and self.exclude_defaults or exclude_defaults
)
exclude_none = exclude_none is None and self.exclude_none or exclude_none
path_view.add_operation(
path=path,
methods=methods,
view_func=view_func,
auth=auth,
throttle=throttle,
response=response,
operation_id=operation_id,
summary=summary,
description=description,
tags=tags,
deprecated=deprecated,
by_alias=by_alias,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
url_name=url_name,
include_in_schema=include_in_schema,
openapi_extra=openapi_extra,
parser=parser,
)
if self.api:
path_view.set_api_instance(self.api, self)
return None
def set_api_instance(
self, api: "NinjaAPI", parent_router: Optional["Router"] = None
) -> None:
# TODO: check - parent_router seems not used
if self.auth is NOT_SET and parent_router and parent_router.auth:
self.auth = parent_router.auth
self.api = api
for path_view in self.path_operations.values():
path_view.set_api_instance(self.api, self)
for _, router in self._routers:
router.set_api_instance(api, self)
def urls_paths(self, prefix: str) -> Iterator[URLPattern]:
prefix = replace_path_param_notation(prefix)
for path, path_view in self.path_operations.items():
for operation in path_view.operations:
path = replace_path_param_notation(path)
route = "/".join([i for i in (prefix, path) if i])
# to skip lot of checks we simply treat double slash as a mistake:
route = normalize_path(route)
route = route.lstrip("/")
url_name = getattr(operation, "url_name", "")
if not url_name and self.api:
url_name = self.api.get_operation_url_name(operation, router=self)
yield django_path(route, path_view.get_view(), name=url_name)
def add_router(
self,
prefix: str,
router: Union["Router", str],
*,
auth: Any = NOT_SET,
throttle: Union[BaseThrottle, List[BaseThrottle], NOT_SET_TYPE] = NOT_SET,
tags: Optional[List[str]] = None,
) -> None:
if isinstance(router, str):
router = import_string(router)
assert isinstance(router, Router)
if self.api:
# we are already attached to an api
self.api.add_router(
prefix=prefix,
router=router,
auth=auth,
throttle=throttle,
tags=tags,
parent_router=self,
)
else:
# we are not attached to an api
if auth != NOT_SET:
router.auth = auth
# TODO: throttle
if tags is not None:
router.tags = tags
self._routers.append((prefix, router))
def build_routers(self, prefix: str) -> List[Tuple[str, "Router"]]:
if self.api is not None:
from ninja.main import debug_server_url_reimport
if not debug_server_url_reimport():
raise ConfigError(
f"Router@'{prefix}' has already been attached to API"
f" {self.api.title}:{self.api.version} "
)
internal_routes = []
for inter_prefix, inter_router in self._routers:
_route = normalize_path("/".join((prefix, inter_prefix))).lstrip("/")
internal_routes.extend(inter_router.build_routers(_route))
return [(prefix, self), *internal_routes]