diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e1b33a852..f877025b3 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -10,6 +10,9 @@ Next Release (TBD) * Return 405 when method is not supported when running ``chalice local`` (`#159 `__) +* Add path params as requestParameters so they can be used + in generated SDKs as well as cache keys + (`#163 `__) 0.4.0 diff --git a/chalice/deployer.py b/chalice/deployer.py index 929853cb9..6dad974c4 100644 --- a/chalice/deployer.py +++ b/chalice/deployer.py @@ -14,7 +14,7 @@ import inspect import re -from typing import Any, Tuple, Callable, IO # noqa +from typing import Any, Tuple, Callable, IO, List # noqa import botocore.session # noqa import virtualenv @@ -341,6 +341,7 @@ def _configure_resource_route(self, node, http_method): self._apig_methods.create_method_request( resource_id, http_method, route_entry.authorization_type, route_entry.authorizer_id, route_entry.api_key_required, + route_entry.view_args ) self._apig_methods.create_lambda_method_integration( resource_id, http_method, route_entry.content_types, @@ -833,8 +834,9 @@ def __init__(self, apig_client, rest_api_id): def create_method_request(self, resource_id, http_method, authorization_type=None, authorizer_id=None, - api_key_required=False): - # type: (str, str, str, str, bool) -> None + api_key_required=False, + url_params=None): + # type: (str, str, str, str, bool, List[str]) -> None """Create an API Gateway method request. This defines the public API used by consumers of the API Gateway @@ -853,6 +855,12 @@ def create_method_request(self, resource_id, http_method, put_method_cfg['authorizerId'] = authorizer_id if api_key_required: put_method_cfg['apiKeyRequired'] = api_key_required + if url_params: + request_params = { + 'method.request.path.%s' % param: True + for param in url_params + } + put_method_cfg['requestParameters'] = request_params self._apig_client.put_method(**put_method_cfg) def create_lambda_method_integration(self, resource_id, http_method, diff --git a/tests/unit/test_deployer.py b/tests/unit/test_deployer.py index cb61896b9..2416424e4 100644 --- a/tests/unit/test_deployer.py +++ b/tests/unit/test_deployer.py @@ -437,3 +437,20 @@ def badview(): with pytest.raises(ValueError): validate_routes(sample_app.routes) + + +def test_apig_methods(stubbed_session): + gateway_stub = stubbed_session.stub('apigateway') + gateway_stub.put_method( + authorizationType='NONE', + httpMethod='GET', + resourceId='resource_id', + restApiId='rest_api_id', + requestParameters={'method.request.path.name': True}, + ).returns({}) + apig = APIGatewayMethods( + stubbed_session.create_client('apigateway'), 'rest_api_id') + + stubbed_session.activate_stubs() + apig.create_method_request('resource_id', 'GET', url_params=['name']) + stubbed_session.verify_stubs()