@@ -110,7 +110,8 @@ def _compress(self):
110
110
gzip = zlib .compressobj (9 , zlib .DEFLATED , zlib .MAX_WBITS | 16 )
111
111
self .response .body = gzip .compress (self .response .body ) + gzip .flush ()
112
112
113
- def _route (self , event : BaseProxyEvent , cors : CORSConfig = None ):
113
+ def _route (self , event : BaseProxyEvent , cors : Optional [CORSConfig ]):
114
+ """Optionally handle any of the route's configure response handling"""
114
115
if self .route is None :
115
116
return
116
117
if self .route .cors :
@@ -173,19 +174,25 @@ def resolve(self, event, context) -> Dict[str, Any]:
173
174
self .lambda_context = context
174
175
return self ._resolve_response ().build (self .current_event , self ._cors )
175
176
177
+ def __call__ (self , event , context ) -> Any :
178
+ return self .resolve (event , context )
179
+
176
180
@staticmethod
177
181
def _compile_regex (rule : str ):
182
+ """Precompile regex pattern"""
178
183
rule_regex : str = re .sub (r"(<\w+>)" , r"(?P\1.+)" , rule )
179
184
return re .compile ("^{}$" .format (rule_regex ))
180
185
181
186
def _to_data_class (self , event : Dict ) -> BaseProxyEvent :
187
+ """Convert the event dict to the corresponding data class"""
182
188
if self ._proxy_type == ProxyEventType .http_api_v1 :
183
189
return APIGatewayProxyEvent (event )
184
190
if self ._proxy_type == ProxyEventType .http_api_v2 :
185
191
return APIGatewayProxyEventV2 (event )
186
192
return ALBEvent (event )
187
193
188
194
def _resolve_response (self ) -> ResponseBuilder :
195
+ """Resolve the response or return the not found response"""
189
196
method = self .current_event .http_method .upper ()
190
197
path = self .current_event .path
191
198
for route in self ._routes :
@@ -195,9 +202,10 @@ def _resolve_response(self) -> ResponseBuilder:
195
202
if match :
196
203
return self ._call_route (route , match .groupdict ())
197
204
198
- return self .not_found (method , path )
205
+ return self ._not_found (method , path )
199
206
200
- def not_found (self , method : str , path : str ) -> ResponseBuilder :
207
+ def _not_found (self , method : str , path : str ) -> ResponseBuilder :
208
+ """No matching route was found, includes support for the cors preflight response"""
201
209
headers = {}
202
210
if self ._cors :
203
211
headers .update (self ._cors .to_dict ())
@@ -214,10 +222,12 @@ def not_found(self, method: str, path: str) -> ResponseBuilder:
214
222
)
215
223
216
224
def _call_route (self , route : Route , args : Dict [str , str ]) -> ResponseBuilder :
225
+ """Actually call the matching route with any provided keyword arguments."""
217
226
return ResponseBuilder (self ._to_response (route .func (** args )), route )
218
227
219
228
@staticmethod
220
229
def _to_response (result : Union [Tuple [int , str , Union [bytes , str ]], Dict , Response ]) -> Response :
230
+ """Convert the route result to a Response"""
221
231
if isinstance (result , Response ):
222
232
return result
223
233
elif isinstance (result , dict ):
@@ -228,6 +238,3 @@ def _to_response(result: Union[Tuple[int, str, Union[bytes, str]], Dict, Respons
228
238
)
229
239
else : # Tuple[int, str, Union[bytes, str]]
230
240
return Response (* result )
231
-
232
- def __call__ (self , event , context ) -> Any :
233
- return self .resolve (event , context )
0 commit comments