Skip to content

Commit 51c0dda

Browse files
author
Jean-Charles Bertin
committed
Allows the middleware to override the rest API requests.
1 parent e684710 commit 51c0dda

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

chalice/app.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,7 @@ def __init__(self, event_dict: Dict[str, Any],
419419
if query_params is None else MultiDict(query_params)
420420
self.headers: CaseInsensitiveMapping = \
421421
CaseInsensitiveMapping(event_dict['headers'])
422-
self.uri_params: Optional[Dict[str, str]] \
423-
= event_dict['pathParameters']
422+
self.uri_params: Dict[str, str] = event_dict['pathParameters']
424423
self.method: str = event_dict['requestContext']['httpMethod']
425424
self._is_base64_encoded = event_dict.get('isBase64Encoded', False)
426425
self._body: Any = event_dict['body']
@@ -1857,12 +1856,17 @@ def wrapped_event(request: Request) -> Response:
18571856
return response.to_dict(self.api.binary_types)
18581857

18591858
def _main_rest_api_handler(self, event: Any, context: Any) -> Response:
1860-
resource_path = event.get('requestContext', {}).get('resourcePath')
1861-
if resource_path is None:
1862-
return error_response(error_code='InternalServerError',
1863-
message='Unknown request.',
1864-
http_status_code=500)
1865-
http_method = event['requestContext']['httpMethod']
1859+
current_request: Optional[Request] = self.current_request
1860+
if current_request:
1861+
resource_path = current_request.path
1862+
http_method = current_request.method
1863+
else:
1864+
resource_path = event.get('requestContext', {}).get('resourcePath')
1865+
if resource_path is None:
1866+
return error_response(error_code='InternalServerError',
1867+
message='Unknown request.',
1868+
http_status_code=500)
1869+
http_method = event['requestContext']['httpMethod']
18661870
if http_method not in self.routes[resource_path]:
18671871
allowed_methods = ', '.join(self.routes[resource_path].keys())
18681872
return error_response(
@@ -1872,8 +1876,12 @@ def _main_rest_api_handler(self, event: Any, context: Any) -> Response:
18721876
headers={'Allow': allowed_methods})
18731877
route_entry = self.routes[resource_path][http_method]
18741878
view_function = route_entry.view_function
1875-
function_args = {name: event['pathParameters'][name]
1876-
for name in route_entry.view_args}
1879+
if current_request:
1880+
function_args = {name: current_request.uri_params[name]
1881+
for name in route_entry.view_args}
1882+
else:
1883+
function_args = {name: event['pathParameters'][name]
1884+
for name in route_entry.view_args}
18771885
self.lambda_context = context
18781886
# We're getting the CORS headers before validation to be able to
18791887
# output desired headers with
@@ -1883,8 +1891,8 @@ def _main_rest_api_handler(self, event: Any, context: Any) -> Response:
18831891
# We're doing the header validation after creating the request
18841892
# so can leverage the case insensitive dict that the Request class
18851893
# uses for headers.
1886-
if self.current_request and route_entry.content_types:
1887-
content_type = self.current_request.headers.get(
1894+
if current_request and route_entry.content_types:
1895+
content_type = current_request.headers.get(
18881896
'content-type', 'application/json')
18891897
if not _matches_content_type(content_type,
18901898
route_entry.content_types):
@@ -1900,8 +1908,8 @@ def _main_rest_api_handler(self, event: Any, context: Any) -> Response:
19001908
self._add_cors_headers(response, cors_headers)
19011909

19021910
response_headers = CaseInsensitiveMapping(response.headers)
1903-
if self.current_request and not self._validate_binary_response(
1904-
self.current_request.headers, response_headers):
1911+
if current_request and not self._validate_binary_response(
1912+
current_request.headers, response_headers):
19051913
content_type = response_headers.get('content-type', '')
19061914
return error_response(
19071915
error_code='BadRequest',

0 commit comments

Comments
 (0)