Skip to content

Commit 1b33f0f

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

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

chalice/app.py

+21-12
Original file line numberDiff line numberDiff line change
@@ -1857,12 +1857,17 @@ def wrapped_event(request: Request) -> Response:
18571857
return response.to_dict(self.api.binary_types)
18581858

18591859
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']
1860+
current_request: Optional[Request] = self.current_request
1861+
if current_request:
1862+
resource_path = current_request.path
1863+
http_method = current_request.method
1864+
else:
1865+
resource_path = event.get('requestContext', {}).get('resourcePath')
1866+
if resource_path is None:
1867+
return error_response(error_code='InternalServerError',
1868+
message='Unknown request.',
1869+
http_status_code=500)
1870+
http_method = event['requestContext']['httpMethod']
18661871
if http_method not in self.routes[resource_path]:
18671872
allowed_methods = ', '.join(self.routes[resource_path].keys())
18681873
return error_response(
@@ -1872,8 +1877,12 @@ def _main_rest_api_handler(self, event: Any, context: Any) -> Response:
18721877
headers={'Allow': allowed_methods})
18731878
route_entry = self.routes[resource_path][http_method]
18741879
view_function = route_entry.view_function
1875-
function_args = {name: event['pathParameters'][name]
1876-
for name in route_entry.view_args}
1880+
if current_request:
1881+
function_args = {name: current_request.uri_params[name]
1882+
for name in route_entry.view_args}
1883+
else:
1884+
function_args = {name: event['pathParameters'][name]
1885+
for name in route_entry.view_args}
18771886
self.lambda_context = context
18781887
# We're getting the CORS headers before validation to be able to
18791888
# output desired headers with
@@ -1883,8 +1892,8 @@ def _main_rest_api_handler(self, event: Any, context: Any) -> Response:
18831892
# We're doing the header validation after creating the request
18841893
# so can leverage the case insensitive dict that the Request class
18851894
# uses for headers.
1886-
if self.current_request and route_entry.content_types:
1887-
content_type = self.current_request.headers.get(
1895+
if current_request and route_entry.content_types:
1896+
content_type = current_request.headers.get(
18881897
'content-type', 'application/json')
18891898
if not _matches_content_type(content_type,
18901899
route_entry.content_types):
@@ -1900,8 +1909,8 @@ def _main_rest_api_handler(self, event: Any, context: Any) -> Response:
19001909
self._add_cors_headers(response, cors_headers)
19011910

19021911
response_headers = CaseInsensitiveMapping(response.headers)
1903-
if self.current_request and not self._validate_binary_response(
1904-
self.current_request.headers, response_headers):
1912+
if current_request and not self._validate_binary_response(
1913+
current_request.headers, response_headers):
19051914
content_type = response_headers.get('content-type', '')
19061915
return error_response(
19071916
error_code='BadRequest',

0 commit comments

Comments
 (0)