From bd57233b10f8cca2b6e03586d5549974fac9a4f6 Mon Sep 17 00:00:00 2001 From: Steve Pulec Date: Fri, 4 Mar 2016 18:02:07 -0500 Subject: [PATCH] add integrations. --- moto/apigateway/models.py | 31 +++++++++++++++++ moto/apigateway/responses.py | 19 +++++++++++ moto/apigateway/urls.py | 1 + tests/test_apigateway/test_apigateway.py | 43 ++++++++++++++++++++---- 4 files changed, 88 insertions(+), 6 deletions(-) diff --git a/moto/apigateway/models.py b/moto/apigateway/models.py index 1146349fe..a26b3c0f8 100644 --- a/moto/apigateway/models.py +++ b/moto/apigateway/models.py @@ -6,6 +6,13 @@ from moto.core.utils import iso_8601_datetime_with_milliseconds from .utils import create_id +class Integration(dict): + def __init__(self, integration_type, uri): + super(Integration, self).__init__() + self['type'] = integration_type + self['uri'] = uri + + class MethodResponse(dict): def __init__(self, status_code): super(MethodResponse, self).__init__() @@ -65,6 +72,17 @@ class Resource(object): def get_method(self, method_type): return self.resource_methods[method_type] + def add_integration(self, method_type, integration_type, uri): + integration = Integration(integration_type, uri) + self.resource_methods[method_type]['methodIntegration'] = integration + return integration + + def get_integration(self, method_type): + return self.resource_methods[method_type]['methodIntegration'] + + def delete_integration(self, method_type): + return self.resource_methods[method_type].pop('methodIntegration') + class RestAPI(object): def __init__(self, id, name, description): @@ -165,6 +183,19 @@ class APIGatewayBackend(BaseBackend): method_response = method.delete_response(response_code) return method_response + def create_integration(self, function_id, resource_id, method_type, integration_type, uri): + resource = self.get_resource(function_id, resource_id) + integration = resource.add_integration(method_type, integration_type, uri) + return integration + + def get_integration(self, function_id, resource_id, method_type): + resource = self.get_resource(function_id, resource_id) + return resource.get_integration(method_type) + + def delete_integration(self, function_id, resource_id, method_type): + resource = self.get_resource(function_id, resource_id) + return resource.delete_integration(method_type) + apigateway_backends = {} for region_name in ['us-east-1', 'us-west-2', 'eu-west-1', 'ap-northeast-1']: # Not available in boto yet apigateway_backends[region_name] = APIGatewayBackend(region_name) diff --git a/moto/apigateway/responses.py b/moto/apigateway/responses.py index c6e11710d..e11cfac90 100644 --- a/moto/apigateway/responses.py +++ b/moto/apigateway/responses.py @@ -98,3 +98,22 @@ class APIGatewayResponse(BaseResponse): elif self.method == 'DELETE': method_response = self.backend.delete_method_response(function_id, resource_id, method_type, response_code) return 200, headers, json.dumps(method_response) + + def integrations(self, request, full_url, headers): + self.setup_class(request, full_url, headers) + url_path_parts = self.path.split("/") + function_id = url_path_parts[2] + resource_id = url_path_parts[4] + method_type = url_path_parts[6] + + if self.method == 'GET': + integration_response = self.backend.get_integration(function_id, resource_id, method_type) + return 200, headers, json.dumps(integration_response) + elif self.method == 'PUT': + integration_type = self._get_param('type') + uri = self._get_param('uri') + integration_response = self.backend.create_integration(function_id, resource_id, method_type, integration_type, uri) + return 200, headers, json.dumps(integration_response) + elif self.method == 'DELETE': + integration_response = self.backend.delete_integration(function_id, resource_id, method_type) + return 200, headers, json.dumps(integration_response) diff --git a/moto/apigateway/urls.py b/moto/apigateway/urls.py index d71a9e18c..a42c30cfd 100644 --- a/moto/apigateway/urls.py +++ b/moto/apigateway/urls.py @@ -12,4 +12,5 @@ url_paths = { '{0}/restapis/(?P[^/]+)/resources/(?P[^/]+)/?$': APIGatewayResponse().resource_individual, '{0}/restapis/(?P[^/]+)/resources/(?P[^/]+)/methods/(?P[^/]+)/?$': APIGatewayResponse().resource_methods, '{0}/restapis/(?P[^/]+)/resources/(?P[^/]+)/methods/(?P[^/]+)/responses/200$': APIGatewayResponse().resource_method_responses, + '{0}/restapis/(?P[^/]+)/resources/(?P[^/]+)/methods/(?P[^/]+)/integration/?$': APIGatewayResponse().integrations, } diff --git a/tests/test_apigateway/test_apigateway.py b/tests/test_apigateway/test_apigateway.py index 5fae936eb..cac502cc9 100644 --- a/tests/test_apigateway/test_apigateway.py +++ b/tests/test_apigateway/test_apigateway.py @@ -208,12 +208,6 @@ def test_integrations(): authorizationType='none', ) - client.get_method( - restApiId=api_id, - resourceId=root_id, - httpMethod='GET' - ) - client.put_method_response( restApiId=api_id, resourceId=root_id, @@ -228,3 +222,40 @@ def test_integrations(): type='HTTP', uri='http://httpbin.org/robots.txt', ) + response.should.equal({ + 'type': 'HTTP', + 'uri': 'http://httpbin.org/robots.txt', + 'ResponseMetadata': {'HTTPStatusCode': 200} + }) + + response = client.get_integration( + restApiId=api_id, + resourceId=root_id, + httpMethod='GET' + ) + response.should.equal({ + 'type': 'HTTP', + 'uri': 'http://httpbin.org/robots.txt', + 'ResponseMetadata': {'HTTPStatusCode': 200} + }) + + response = client.get_resource( + restApiId=api_id, + resourceId=root_id, + ) + response['resourceMethods']['GET']['methodIntegration'].should.equal({ + 'type': 'HTTP', + 'uri': 'http://httpbin.org/robots.txt' + }) + + client.delete_integration( + restApiId=api_id, + resourceId=root_id, + httpMethod='GET' + ) + + response = client.get_resource( + restApiId=api_id, + resourceId=root_id, + ) + response['resourceMethods']['GET'].shouldnt.contain("methodIntegration")