u-toolkit 0.1.9__py3-none-any.whl → 0.1.11__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- u_toolkit/fastapi/cbv.py +58 -22
- u_toolkit/fastapi/responses.py +2 -1
- {u_toolkit-0.1.9.dist-info → u_toolkit-0.1.11.dist-info}/METADATA +1 -1
- {u_toolkit-0.1.9.dist-info → u_toolkit-0.1.11.dist-info}/RECORD +6 -6
- {u_toolkit-0.1.9.dist-info → u_toolkit-0.1.11.dist-info}/WHEEL +0 -0
- {u_toolkit-0.1.9.dist-info → u_toolkit-0.1.11.dist-info}/entry_points.txt +0 -0
u_toolkit/fastapi/cbv.py
CHANGED
@@ -39,7 +39,7 @@ EndpointsClassInterfaceT = TypeVar(
|
|
39
39
|
)
|
40
40
|
|
41
41
|
|
42
|
-
|
42
|
+
LiteralUpperMethod = Literal[
|
43
43
|
"GET",
|
44
44
|
"POST",
|
45
45
|
"PATCH",
|
@@ -49,7 +49,7 @@ LiteralUpperMethods = Literal[
|
|
49
49
|
"HEAD",
|
50
50
|
"TRACE",
|
51
51
|
]
|
52
|
-
|
52
|
+
LiteralLowerMethod = Literal[
|
53
53
|
"get",
|
54
54
|
"post",
|
55
55
|
"patch",
|
@@ -72,23 +72,31 @@ class Methods(StrEnum):
|
|
72
72
|
TRACE = auto()
|
73
73
|
|
74
74
|
|
75
|
+
RequestMethod = Methods | LiteralLowerMethod | LiteralUpperMethod
|
76
|
+
|
77
|
+
|
75
78
|
METHOD_PATTERNS = {
|
76
79
|
method: re.compile(f"^({method}_|{method})", re.IGNORECASE)
|
77
80
|
for method in Methods
|
78
81
|
}
|
79
82
|
|
83
|
+
|
80
84
|
_FnName = str
|
81
85
|
|
82
86
|
|
83
87
|
class EndpointInfo(NamedTuple):
|
84
|
-
|
85
|
-
|
86
|
-
|
88
|
+
handle: Callable
|
89
|
+
original_handle_name: str
|
90
|
+
handle_name: str
|
91
|
+
method: RequestMethod
|
87
92
|
method_pattern: re.Pattern
|
88
93
|
path: str
|
89
94
|
|
90
95
|
|
91
|
-
|
96
|
+
_MethodInfo = tuple[RequestMethod, re.Pattern[str]]
|
97
|
+
|
98
|
+
|
99
|
+
def get_method(name: str) -> _MethodInfo | None:
|
92
100
|
for method, method_pattern in METHOD_PATTERNS.items():
|
93
101
|
if method_pattern.search(name):
|
94
102
|
return method, method_pattern
|
@@ -100,29 +108,41 @@ def valid_endpoint(name: str):
|
|
100
108
|
raise ValueError("Invalid endpoint function.")
|
101
109
|
|
102
110
|
|
103
|
-
def iter_endpoints(
|
104
|
-
|
111
|
+
def iter_endpoints(
|
112
|
+
cls: type[_T],
|
113
|
+
valid_method: Callable[[str, Callable], list[_MethodInfo] | None]
|
114
|
+
| None = None,
|
115
|
+
):
|
116
|
+
prefix = ""
|
105
117
|
|
106
118
|
if not cls.__name__.startswith("_"):
|
107
|
-
prefix += f"{to_snake(cls.__name__)}"
|
119
|
+
prefix += f"/{to_snake(cls.__name__)}"
|
108
120
|
|
109
|
-
for name,
|
121
|
+
for name, handle in inspect.getmembers(
|
110
122
|
cls,
|
111
123
|
lambda arg: inspect.ismethoddescriptor(arg) or inspect.isfunction(arg),
|
112
124
|
):
|
113
125
|
paths = [prefix]
|
114
126
|
|
127
|
+
methods: list[_MethodInfo] = []
|
115
128
|
if method := get_method(name):
|
116
|
-
|
129
|
+
methods.append(method)
|
130
|
+
elif valid_method:
|
131
|
+
methods.extend(valid_method(name, handle) or [])
|
132
|
+
|
133
|
+
for method, pattern in methods:
|
134
|
+
handle_name = pattern.sub("", name)
|
135
|
+
path = handle_name.replace("__", "/")
|
117
136
|
if path:
|
118
137
|
paths.append(path)
|
119
138
|
|
120
139
|
yield EndpointInfo(
|
121
|
-
|
122
|
-
|
140
|
+
handle=handle,
|
141
|
+
original_handle_name=name,
|
142
|
+
handle_name=handle_name,
|
123
143
|
path="/".join(paths),
|
124
|
-
method=method
|
125
|
-
method_pattern=
|
144
|
+
method=method,
|
145
|
+
method_pattern=pattern,
|
126
146
|
)
|
127
147
|
|
128
148
|
|
@@ -171,7 +191,7 @@ class CBV:
|
|
171
191
|
*,
|
172
192
|
cls: type[EndpointsClassInterfaceT],
|
173
193
|
path: str,
|
174
|
-
method:
|
194
|
+
method: RequestMethod,
|
175
195
|
method_name: str,
|
176
196
|
):
|
177
197
|
class_tags = list(cls.tags) if cls.tags else []
|
@@ -221,8 +241,7 @@ class CBV:
|
|
221
241
|
self,
|
222
242
|
*,
|
223
243
|
path: str | None = None,
|
224
|
-
methods: list[
|
225
|
-
| None = None,
|
244
|
+
methods: list[RequestMethod] | None = None,
|
226
245
|
tags: list[str | Enum] | None = None,
|
227
246
|
dependencies: list | None = None,
|
228
247
|
responses: list[Response] | None = None,
|
@@ -261,7 +280,7 @@ class CBV:
|
|
261
280
|
|
262
281
|
default_data = {}
|
263
282
|
for endpoint in iter_endpoints(n_cls):
|
264
|
-
default_data[endpoint.
|
283
|
+
default_data[endpoint.original_handle_name] = {}
|
265
284
|
|
266
285
|
self._state.setdefault(n_cls, default_data)
|
267
286
|
result = self._build_cls(n_cls)
|
@@ -361,14 +380,31 @@ class CBV:
|
|
361
380
|
|
362
381
|
decorator = self.__create_class_dependencies_injector(cls_)
|
363
382
|
|
364
|
-
|
383
|
+
def valid_method(
|
384
|
+
name: str, _handle: Callable
|
385
|
+
) -> None | list[_MethodInfo]:
|
386
|
+
if (cls_state := self._state.get(cls_)) and (
|
387
|
+
method_state := cls_state.get(name)
|
388
|
+
):
|
389
|
+
methods: list[RequestMethod] = (
|
390
|
+
method_state.get("methods") or []
|
391
|
+
)
|
392
|
+
result: list[_MethodInfo] = []
|
393
|
+
for i in methods:
|
394
|
+
method = Methods(i.lower())
|
395
|
+
result.append((method, METHOD_PATTERNS[method]))
|
396
|
+
return result
|
397
|
+
|
398
|
+
return None
|
399
|
+
|
400
|
+
for endpoint_info in iter_endpoints(cls, valid_method):
|
365
401
|
route = self.create_route(
|
366
402
|
cls=cast(type[EndpointsClassInterface], cls),
|
367
403
|
path=endpoint_info.path,
|
368
404
|
method=endpoint_info.method,
|
369
|
-
method_name=endpoint_info.
|
405
|
+
method_name=endpoint_info.original_handle_name,
|
370
406
|
)
|
371
|
-
method = getattr(instance, endpoint_info.
|
407
|
+
method = getattr(instance, endpoint_info.original_handle_name)
|
372
408
|
endpoint = decorator(method)
|
373
409
|
route(endpoint)
|
374
410
|
|
u_toolkit/fastapi/responses.py
CHANGED
@@ -13,7 +13,7 @@ def _merge_responses(
|
|
13
13
|
model_class = response.get("model")
|
14
14
|
if status in source:
|
15
15
|
source_model_class = source[status].get("model")
|
16
|
-
if source_model_class
|
16
|
+
if source_model_class and model_class:
|
17
17
|
target[status]["model"] = model_class | source_model_class
|
18
18
|
|
19
19
|
for status, response in source.items():
|
@@ -59,6 +59,7 @@ def build_responses(*responses: Response):
|
|
59
59
|
status = arg
|
60
60
|
else:
|
61
61
|
errors.append(arg)
|
62
|
+
continue
|
62
63
|
|
63
64
|
result[status] = {"model": response}
|
64
65
|
|
@@ -11,13 +11,13 @@ u_toolkit/object.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
11
11
|
u_toolkit/path.py,sha256=IkyIHcU9hKBCOZfF30FrKf4CfL-MH91fjeYF9EY7eos,128
|
12
12
|
u_toolkit/signature.py,sha256=-Q6n28PYBYYdd2OXBKESeVkL2rYpV6EaY3IVwQmzezQ,2161
|
13
13
|
u_toolkit/fastapi/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
14
|
-
u_toolkit/fastapi/cbv.py,sha256=
|
14
|
+
u_toolkit/fastapi/cbv.py,sha256=oxQFHWa48kZxxhL7UFeq9mUVqo9Q0QgQK_IllkyOFUk,11937
|
15
15
|
u_toolkit/fastapi/config.py,sha256=kGpokR9XXr1KxMA1GVKYkCdKwqIQAIwOJ-v6sGbqzAQ,267
|
16
16
|
u_toolkit/fastapi/exception.py,sha256=P9apEEQvbBMmffiHX57768gmemMeAy69-nb-1JQZAZE,4624
|
17
17
|
u_toolkit/fastapi/helpers.py,sha256=BCMMLxa1c6BMA_rKq-hCi0iyEjrR3Z5rPMeTvgaVJB0,447
|
18
18
|
u_toolkit/fastapi/lifespan.py,sha256=W1TwWymW7xtmntx59QBC4LQ6xQr3L7yuMMGj4U8hhTQ,1813
|
19
19
|
u_toolkit/fastapi/pagination.py,sha256=yOgEDUT04m_mZ0cPliuDbUHLFnmxGAmr5PyZlwfjT_s,1940
|
20
|
-
u_toolkit/fastapi/responses.py,sha256=
|
20
|
+
u_toolkit/fastapi/responses.py,sha256=alXUuFK9g1xwkw7V_gpeJFyWuGTHSh-fuuWTuaG8of8,1765
|
21
21
|
u_toolkit/pydantic/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
22
22
|
u_toolkit/pydantic/fields.py,sha256=9I8Jwek_oNn_niI98ZWaSPEUYIl5mw24YgbnpcEtgZk,839
|
23
23
|
u_toolkit/pydantic/models.py,sha256=Dqp3HnPlUU7ZpfBbYbERfcrLUb2CBJKHySRvM1Rv3HE,1101
|
@@ -30,7 +30,7 @@ u_toolkit/sqlalchemy/type_vars.py,sha256=m2VeV41CBIK_1QX3w2kgz-n556sILAGZ-Kaz3TD
|
|
30
30
|
u_toolkit/sqlalchemy/orm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
31
31
|
u_toolkit/sqlalchemy/orm/fields.py,sha256=3zoYil23I6YLtc_59aHDt9w5l1NBTkePT9AfXI3DMiY,593
|
32
32
|
u_toolkit/sqlalchemy/orm/models.py,sha256=V8vf4ps3phAmwxyaFYK7pw8Igz7h097o4QBjKB0gwC8,705
|
33
|
-
u_toolkit-0.1.
|
34
|
-
u_toolkit-0.1.
|
35
|
-
u_toolkit-0.1.
|
36
|
-
u_toolkit-0.1.
|
33
|
+
u_toolkit-0.1.11.dist-info/METADATA,sha256=XqvuAjDSFm1JLYjz-ag0CVE3gmJ8EKs8BbGRk92yb6o,366
|
34
|
+
u_toolkit-0.1.11.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
35
|
+
u_toolkit-0.1.11.dist-info/entry_points.txt,sha256=hTfAYCd5vvRiqgnJk2eBsoRIiIVB9pK8WZm3Q3jjKFU,45
|
36
|
+
u_toolkit-0.1.11.dist-info/RECORD,,
|
File without changes
|
File without changes
|