4
4
from types import FunctionType
5
5
6
6
from ellar .common .constants import (
7
- CONTROLLER_OPERATION_HANDLER_KEY ,
8
7
DELETE ,
9
8
GET ,
10
9
HEAD ,
13
12
PATCH ,
14
13
POST ,
15
14
PUT ,
15
+ ROUTE_OPERATION_PARAMETERS ,
16
16
TRACE ,
17
17
)
18
- from ellar .common .helper import class_base_function_regex
19
- from ellar .common .types import TCallable
20
- from ellar .reflect import reflect
21
18
22
- from .controller .route import ControllerRouteOperation
23
- from .controller .websocket .route import ControllerWebsocketRouteOperation
24
- from .route import RouteOperation
25
19
from .schema import RouteParameters , WsRouteParameters
26
- from .websocket import WebsocketRouteOperation
27
-
28
- TOperation = t .Union [RouteOperation , ControllerRouteOperation ]
29
- TWebsocketOperation = t .Union [
30
- WebsocketRouteOperation , ControllerWebsocketRouteOperation
31
- ]
32
20
33
21
34
22
def _websocket_connection_attributes (func : t .Callable ) -> t .Callable :
@@ -43,11 +31,11 @@ def _wrap(connect_handler: t.Callable) -> t.Callable:
43
31
"Invalid type. Please make sure you passed the websocket handler."
44
32
)
45
33
46
- _item : t .Optional [WebsocketRouteOperation ] = reflect . get_metadata (
47
- CONTROLLER_OPERATION_HANDLER_KEY , websocket_handler
34
+ _item : t .Optional [WsRouteParameters ] = getattr (
35
+ websocket_handler , ROUTE_OPERATION_PARAMETERS , None
48
36
)
49
37
50
- if not _item or not isinstance (_item , WebsocketRouteOperation ):
38
+ if not _item or not isinstance (_item , WsRouteParameters ):
51
39
raise Exception (
52
40
"Invalid type. Please make sure you passed the websocket handler."
53
41
)
@@ -74,41 +62,43 @@ def _wrap(connect_handler: t.Callable) -> t.Callable:
74
62
class OperationDefinitions :
75
63
__slots__ = ()
76
64
77
- def _get_http_operations_class (self , func : t .Callable ) -> t .Type [TOperation ]:
78
- if class_base_function_regex .match (repr (func )):
79
- return ControllerRouteOperation
80
- return RouteOperation
81
-
82
- def _get_ws_operations_class (self , func : t .Callable ) -> t .Type [TWebsocketOperation ]:
83
- if class_base_function_regex .match (repr (func )):
84
- return ControllerWebsocketRouteOperation
85
- return WebsocketRouteOperation
86
-
87
- def _get_operation (self , route_parameter : RouteParameters ) -> TOperation :
88
- _operation_class = self ._get_http_operations_class (route_parameter .endpoint )
89
- _operation = _operation_class (** route_parameter .dict ())
65
+ # def _get_http_operations_class(self, func: t.Callable) -> t.Type[TOperation]:
66
+ # if class_base_function_regex.match(repr(func)):
67
+ # return ControllerRouteOperation
68
+ # return RouteOperation
69
+ #
70
+ # def _get_ws_operations_class(self, func: t.Callable) -> t.Type[TWebsocketOperation]:
71
+ # if class_base_function_regex.match(repr(func)):
72
+ # return ControllerWebsocketRouteOperation
73
+ # return WebsocketRouteOperation
74
+
75
+ def _get_operation (self , route_parameter : RouteParameters ) -> t .Callable :
90
76
setattr (route_parameter .endpoint , OPERATION_ENDPOINT_KEY , True )
91
- return _operation
92
-
93
- def _get_ws_operation (
94
- self , ws_route_parameters : WsRouteParameters
95
- ) -> TWebsocketOperation :
96
- _ws_operation_class = self ._get_ws_operations_class (
97
- ws_route_parameters .endpoint
98
- )
99
- _operation = _ws_operation_class (** ws_route_parameters .dict ())
77
+ setattr (route_parameter .endpoint , ROUTE_OPERATION_PARAMETERS , route_parameter )
78
+ return route_parameter .endpoint
79
+
80
+ def _get_ws_operation (self , ws_route_parameters : WsRouteParameters ) -> t .Callable :
81
+ # _ws_operation_class = self._get_ws_operations_class(
82
+ # ws_route_parameters.endpoint
83
+ # )
84
+ # _operation = _ws_operation_class(**ws_route_parameters.dict())
100
85
setattr (ws_route_parameters .endpoint , OPERATION_ENDPOINT_KEY , True )
101
- return _operation
86
+ setattr (
87
+ ws_route_parameters .endpoint ,
88
+ ROUTE_OPERATION_PARAMETERS ,
89
+ ws_route_parameters ,
90
+ )
91
+ return ws_route_parameters .endpoint
102
92
103
93
def _get_decorator_or_operation (
104
94
self , path : t .Union [str , t .Callable ], endpoint_parameter_partial : t .Callable
105
- ) -> t .Callable [[ TCallable ], t . Union [ TOperation , TCallable ]] :
95
+ ) -> t .Callable :
106
96
if callable (path ):
107
97
route_parameter = endpoint_parameter_partial (endpoint = path , path = "/" )
108
98
self ._get_operation (route_parameter = route_parameter )
109
99
return path
110
100
111
- def _decorator (endpoint_handler : TCallable ) -> TCallable :
101
+ def _decorator (endpoint_handler : t . Callable ) -> t . Callable :
112
102
_route_parameter = endpoint_parameter_partial (
113
103
endpoint = endpoint_handler , path = path
114
104
)
@@ -126,7 +116,7 @@ def get(
126
116
response : t .Union [
127
117
t .Dict [int , t .Type ], t .List [t .Tuple [int , t .Type ]], t .Type , t .Any
128
118
] = None ,
129
- ) -> t .Callable [[ TCallable ], t . Union [ TOperation , TCallable ]] :
119
+ ) -> t .Callable :
130
120
methods = [GET ]
131
121
endpoint_parameter_partial = partial (
132
122
RouteParameters ,
@@ -146,7 +136,7 @@ def post(
146
136
response : t .Union [
147
137
t .Dict [int , t .Type ], t .List [t .Tuple [int , t .Type ]], t .Type , t .Any
148
138
] = None ,
149
- ) -> t .Callable [[ TCallable ], t . Union [ TOperation , TCallable ]] :
139
+ ) -> t .Callable :
150
140
methods = [POST ]
151
141
endpoint_parameter_partial = partial (
152
142
RouteParameters ,
@@ -166,7 +156,7 @@ def put(
166
156
response : t .Union [
167
157
t .Dict [int , t .Type ], t .List [t .Tuple [int , t .Type ]], t .Type , t .Any
168
158
] = None ,
169
- ) -> t .Callable [[ TCallable ], t . Union [ TOperation , TCallable ]] :
159
+ ) -> t .Callable :
170
160
methods = [PUT ]
171
161
endpoint_parameter_partial = partial (
172
162
RouteParameters ,
@@ -186,7 +176,7 @@ def patch(
186
176
response : t .Union [
187
177
t .Dict [int , t .Type ], t .List [t .Tuple [int , t .Type ]], t .Type , t .Any
188
178
] = None ,
189
- ) -> t .Callable [[ TCallable ], t . Union [ TOperation , TCallable ]] :
179
+ ) -> t .Callable :
190
180
methods = [PATCH ]
191
181
endpoint_parameter_partial = partial (
192
182
RouteParameters ,
@@ -206,7 +196,7 @@ def delete(
206
196
response : t .Union [
207
197
t .Dict [int , t .Type ], t .List [t .Tuple [int , t .Type ]], t .Type , t .Any
208
198
] = None ,
209
- ) -> t .Callable [[ TCallable ], t . Union [ TOperation , TCallable ]] :
199
+ ) -> t .Callable :
210
200
methods = [DELETE ]
211
201
endpoint_parameter_partial = partial (
212
202
RouteParameters ,
@@ -226,7 +216,7 @@ def head(
226
216
response : t .Union [
227
217
t .Dict [int , t .Type ], t .List [t .Tuple [int , t .Type ]], t .Type , t .Any
228
218
] = None ,
229
- ) -> t .Callable [[ TCallable ], t . Union [ TOperation , TCallable ]] :
219
+ ) -> t .Callable :
230
220
methods = [HEAD ]
231
221
endpoint_parameter_partial = partial (
232
222
RouteParameters ,
@@ -246,7 +236,7 @@ def options(
246
236
response : t .Union [
247
237
t .Dict [int , t .Type ], t .List [t .Tuple [int , t .Type ]], t .Type , t .Any
248
238
] = None ,
249
- ) -> t .Callable [[ TCallable ], t . Union [ TOperation , TCallable ]] :
239
+ ) -> t .Callable :
250
240
methods = [OPTIONS ]
251
241
endpoint_parameter_partial = partial (
252
242
RouteParameters ,
@@ -266,7 +256,7 @@ def trace(
266
256
response : t .Union [
267
257
t .Dict [int , t .Type ], t .List [t .Tuple [int , t .Type ]], t .Type , t .Any
268
258
] = None ,
269
- ) -> t .Callable [[ TCallable ], t . Union [ TOperation , TCallable ]] :
259
+ ) -> t .Callable :
270
260
methods = [TRACE ]
271
261
endpoint_parameter_partial = partial (
272
262
RouteParameters ,
@@ -287,8 +277,8 @@ def http_route(
287
277
response : t .Union [
288
278
t .Dict [int , t .Type ], t .List [t .Tuple [int , t .Type ]], t .Type , t .Any
289
279
] = None ,
290
- ) -> t .Callable [[ TCallable ], t . Union [ TOperation , TCallable ]] :
291
- def _decorator (endpoint_handler : TCallable ) -> TCallable :
280
+ ) -> t .Callable :
281
+ def _decorator (endpoint_handler : t . Callable ) -> t . Callable :
292
282
endpoint_parameter = RouteParameters (
293
283
name = name ,
294
284
path = path ,
@@ -311,10 +301,10 @@ def ws_route(
311
301
encoding : t .Optional [str ] = "json" ,
312
302
use_extra_handler : bool = False ,
313
303
extra_handler_type : t .Optional [t .Type ] = None ,
314
- ) -> t .Callable [[ TCallable ], t . Union [ TCallable , TWebsocketOperation ]] :
304
+ ) -> t .Callable :
315
305
def _decorator (
316
- endpoint_handler : TCallable ,
317
- ) -> t .Union [ TCallable , TWebsocketOperation ] :
306
+ endpoint_handler : t . Callable ,
307
+ ) -> t .Callable :
318
308
endpoint_parameter = WsRouteParameters (
319
309
name = name ,
320
310
path = path ,
0 commit comments