Skip to content

Commit

Permalink
Add path params as requestParam in put_method
Browse files Browse the repository at this point in the history
This fixes an issue where you can't leverage certain
API gateway features unless you specify the request parameters to
include the params in the URL,
e.g /users/{name} -> method.request.path.name: True

The specific problems I ran into:

* Generated javascript SDK does not include these params
* Can't use the params as cache keys.
  • Loading branch information
jamesls committed Nov 10, 2016
1 parent 1dba858 commit 6bdb174
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Next Release (TBD)

* Add default application logger
(`#149 <https://github.com/awslabs/chalice/issues/149>`__)
* Add path params as requestParameters so they can be used
in generated SDKs as well as cache keys
(`#163 <https://github.com/awslabs/chalice/issues/163>`__)


0.4.0
Expand Down
14 changes: 11 additions & 3 deletions chalice/deployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/test_deployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 6bdb174

Please sign in to comment.