示例#1
0
    def create_integration_response(self, api_id, resource_id, http_method, status_code, selection_pattern=None):

        url = ("/restapis/{0}/resources/{1}/methods/{2}/" "integration/responses/{3}").format(
            api_id, resource_id, http_method.upper(), status_code
        )
        return sign_request(
            self.access_key,
            self.secret_key,
            method="put",
            canonical_uri=url,
            request_body={
                "selectionPattern": selection_pattern,
                "responseParameters": {},
                "responseTemplates": {"application/json": ""},
            },
        )
示例#2
0
    def create_integration(self, api_id, resource_id, http_method, uri, credentials, content_mapping_templates=None):
        if not content_mapping_templates:
            content_mapping_templates = {}

        url = "/restapis/{0}/resources/{1}/methods/{2}/integration".format(api_id, resource_id, http_method.upper())

        resp = sign_request(
            self.access_key,
            self.secret_key,
            canonical_uri=url,
            method="put",
            request_body={
                "type": "AWS",
                "httpMethod": "POST",
                "uri": uri,
                "credentials": credentials,
                "requestParameters": {},
                "requestTemplates": content_mapping_templates,
                "cacheNamespace": "none",
                "cacheKeyParameters": [],
            },
        )
        return resp
示例#3
0
    def create_and_setup_methods(self, api_id, resource_id, lambda_arn):
        """
        Sets up the methods, integration responses and method responses for a given API Gateway resource.

        Returns the given API's resource_id.

        """

        client = boto3.client('apigateway')

        for method in self.http_methods:

            response = client.put_method(restApiId=api_id,
                                         resourceId=resource_id,
                                         httpMethod=method,
                                         authorizationType='none',
                                         apiKeyRequired=False)

            # Gotta do this one dirty.. thanks Boto..
            template_mapping = TEMPLATE_MAPPING
            post_template_mapping = POST_TEMPLATE_MAPPING
            content_mapping_templates = {
                'application/json': template_mapping,
                'application/x-www-form-urlencoded': post_template_mapping
            }
            credentials = self.credentials_arn  # This must be a Role ARN
            uri = 'arn:aws:apigateway:' + self.aws_region + ':lambda:path/2015-03-31/functions/' + lambda_arn + '/invocations'
            url = "/restapis/{0}/resources/{1}/methods/{2}/integration".format(
                api_id, resource_id, method.upper())

            response = sign_request(
                self.access_key,
                self.secret_key,
                canonical_uri=url,
                method='put',
                request_body={
                    "type": "AWS",
                    "httpMethod": "POST",
                    "uri": uri,
                    "credentials": credentials,
                    "requestParameters": {},
                    "requestTemplates": content_mapping_templates,
                    "cacheNamespace": "none",
                    "cacheKeyParameters": []
                },
                host='apigateway.' + self.aws_region + '.amazonaws.com',
                region=self.aws_region,
            )

            ##
            # Method Response
            ##

            for response in self.method_response_codes:
                status_code = str(response)

                response_parameters = {
                    "method.response.header." + header_type: False
                    for header_type in self.method_header_types
                }
                response_models = {
                    content_type: 'Empty'
                    for content_type in self.method_content_types
                }

                method_response = client.put_method_response(
                    restApiId=api_id,
                    resourceId=resource_id,
                    httpMethod=method,
                    statusCode=status_code,
                    responseParameters=response_parameters,
                    responseModels=response_models)

            ##
            # Integration Response
            ##

            for response in self.integration_response_codes:
                status_code = str(response)

                response_parameters = {
                    "method.response.header." + header_type:
                    "integration.response.body." + header_type
                    for header_type in self.method_header_types
                }

                # Error code matching RegEx
                # Thanks to @KevinHornschemeier and @jayway
                # for the discussion on this.
                if status_code == '200':
                    selection_pattern = ''
                    response_templates = {
                        content_type: RESPONSE_TEMPLATE
                        for content_type in self.integration_content_types
                    }
                elif status_code in ['301', '302']:
                    selection_pattern = '\/.*'
                    response_templates = {
                        content_type: REDIRECT_RESPONSE_TEMPLATE
                        for content_type in self.integration_content_types
                    }
                    response_parameters[
                        "method.response.header.Location"] = "integration.response.body.errorMessage"
                else:
                    selection_pattern = base64.b64encode(
                        "<!DOCTYPE html>" + str(status_code)) + '.*'
                    selection_pattern = selection_pattern.replace('+', "\+")
                    response_templates = {
                        content_type: ERROR_RESPONSE_TEMPLATE
                        for content_type in self.integration_content_types
                    }

                integration_response = client.put_integration_response(
                    restApiId=api_id,
                    resourceId=resource_id,
                    httpMethod=method,
                    statusCode=status_code,
                    selectionPattern=selection_pattern,
                    responseParameters=response_parameters,
                    responseTemplates=response_templates)

        return resource_id
示例#4
0
    def create_and_setup_methods(self, api_id, resource_id, lambda_arn, delay=1):
        """
        Sets up the methods, integration responses and method responses for a given API Gateway resource.

        Returns the given API's resource_id.

        """

        client = boto3.client('apigateway')

        for method in self.http_methods:

            response = client.put_method(
                    restApiId=api_id,
                    resourceId=resource_id,
                    httpMethod=method,
                    authorizationType='none',
                    apiKeyRequired=False
            )

            # Gotta do this one dirty.. thanks Boto..
            template_mapping = TEMPLATE_MAPPING
            post_template_mapping = POST_TEMPLATE_MAPPING
            content_mapping_templates = {'application/json': template_mapping, 'application/x-www-form-urlencoded': post_template_mapping}
            credentials = self.credentials_arn  # This must be a Role ARN
            uri = 'arn:aws:apigateway:' + self.aws_region + ':lambda:path/2015-03-31/functions/' + lambda_arn + '/invocations'
            url = "/restapis/{0}/resources/{1}/methods/{2}/integration".format(
                    api_id,
                    resource_id,
                    method.upper()
            )

            response = sign_request(
                    self.access_key,
                    self.secret_key,
                    canonical_uri=url,
                    method='put',
                    request_body={
                        "type": "AWS",
                        "httpMethod": "POST",
                        "uri": uri,
                        "credentials": credentials,
                        "requestParameters": {
                        },
                        "requestTemplates": content_mapping_templates,
                        "cacheNamespace": "none",
                        "cacheKeyParameters": []
                    },
                    host='apigateway.' + self.aws_region + '.amazonaws.com',
                    region=self.aws_region,
            )

            time.sleep(delay)

            ##
            # Method Response
            ##

            for response in self.method_response_codes:
                status_code = str(response)

                response_parameters = {"method.response.header." + header_type: False for header_type in self.method_header_types}
                response_models = {content_type: 'Empty' for content_type in self.method_content_types}

                method_response = client.put_method_response(
                        restApiId=api_id,
                        resourceId=resource_id,
                        httpMethod=method,
                        statusCode=status_code,
                        responseParameters=response_parameters,
                        responseModels=response_models
                )

                time.sleep(delay)

            ##
            # Integration Response
            ##

            for response in self.integration_response_codes:
                status_code = str(response)

                response_parameters = {"method.response.header." + header_type: "integration.response.body." + header_type for header_type in self.method_header_types}

                # Error code matching RegEx
                # Thanks to @KevinHornschemeier and @jayway
                # for the discussion on this.
                if status_code == '200':
                    selection_pattern = ''
                    response_templates = {content_type: RESPONSE_TEMPLATE for content_type in self.integration_content_types}
                elif status_code in ['301', '302']:
                    selection_pattern = '\/.*'
                    response_templates = {content_type: REDIRECT_RESPONSE_TEMPLATE for content_type in self.integration_content_types}
                    response_parameters["method.response.header.Location"] = "integration.response.body.errorMessage"
                else:
                    selection_pattern = base64.b64encode("<!DOCTYPE html>" + str(status_code)) + '.*'
                    selection_pattern = selection_pattern.replace('+', "\+")
                    response_templates = {content_type: ERROR_RESPONSE_TEMPLATE for content_type in self.integration_content_types}

                integration_response = client.put_integration_response(
                        restApiId=api_id,
                        resourceId=resource_id,
                        httpMethod=method,
                        statusCode=status_code,
                        selectionPattern=selection_pattern,
                        responseParameters=response_parameters,
                        responseTemplates=response_templates
                )

                time.sleep(delay)

        return resource_id