def _botocore_parser_integration_test(service: str, action: str, method: str = None, request_uri: str = None, headers: dict = None, expected: dict = None, **kwargs): # Load the appropriate service service = load_service(service) # Use the serializer from botocore to serialize the request params serializer = create_serializer(service.protocol) serialized_request = serializer.serialize_to_request( kwargs, service.operation_model(action)) serialized_request["path"] = request_uri serialized_request["method"] = method serialized_request["headers"] = headers if service.protocol in ["query", "ec2"]: # Serialize the body as query parameter serialized_request["body"] = urlencode(serialized_request["body"]) # Use our parser to parse the serialized body parser = create_parser(service) operation_model, parsed_request = parser.parse(serialized_request) # Check if the result is equal to the given "expected" dict or the kwargs (if "expected" has not been set) assert parsed_request == (expected or kwargs)
def test_instantiate_with_validation(self): request_serializer = serialize.create_serializer( self.service_model.metadata['protocol'], True) try: self.assert_serialize_valid_parameter(request_serializer) except ParamValidationError as e: self.fail("Shouldn't fail serializing valid parameter with validation") with self.assertRaises(ParamValidationError): self.assert_serialize_invalid_parameter(request_serializer)
def test_instantiate_with_validation(self): request_serializer = serialize.create_serializer( self.service_model.metadata['protocol'], True) try: self.assert_serialize_valid_parameter(request_serializer) except ParamValidationError as e: self.fail( "Shouldn't fail serializing valid parameter with validation") with self.assertRaises(ParamValidationError): self.assert_serialize_invalid_parameter(request_serializer)
def _botocore_parser_integration_test(service: str, action: str, headers: dict = None, expected: dict = None, **kwargs): # Load the appropriate service service = load_service(service) # Use the serializer from botocore to serialize the request params serializer = create_serializer(service.protocol) operation_model = service.operation_model(action) serialized_request = serializer.serialize_to_request( kwargs, operation_model) prepare_request_dict(serialized_request, "") split_url = urlsplit(serialized_request.get("url")) path = split_url.path query_string = split_url.query body = serialized_request["body"] # use custom headers (if provided), or headers from serialized request as default headers = serialized_request.get("headers") if headers is None else headers if service.protocol in ["query", "ec2"]: # Serialize the body as query parameter body = urlencode(serialized_request["body"]) # Use our parser to parse the serialized body parser = create_parser(service) parsed_operation_model, parsed_request = parser.parse( HttpRequest( method=serialized_request.get("method") or "GET", path=unquote(path), query_string=to_str(query_string), headers=headers, body=body, raw_path=path, )) # Check if the determined operation_model is correct assert parsed_operation_model == operation_model # Check if the result is equal to the given "expected" dict or the kwargs (if "expected" has not been set) expected = expected or kwargs # The parser adds None for none-existing members on purpose. Remove those for the assert expected = { key: value for key, value in expected.items() if value is not None } parsed_request = { key: value for key, value in parsed_request.items() if value is not None } assert parsed_request == expected
def test_instantiate_without_validation(self): request_serializer = serialize.create_serializer( self.service_model.metadata['protocol'], False) try: self.assert_serialize_valid_parameter(request_serializer) except ParamValidationError as e: self.fail( "Shouldn't fail serializing valid parameter without validation".format(e)) try: self.assert_serialize_invalid_parameter(request_serializer) except ParamValidationError as e: self.fail( "Shouldn't fail serializing invalid parameter without validation".format(e))
def build_parameters(self, **kwargs): """ Returns a dictionary containing the kwargs for the given operation formatted as required to pass to the service in a request. """ protocol = self._model.metadata['protocol'] input_shape = self._model.input_shape if input_shape is not None: self._convert_kwargs_to_correct_casing(kwargs) validator = ParamValidator() errors = validator.validate(kwargs, self._model.input_shape) if errors.has_errors(): raise ParamValidationError(report=errors.generate_report()) serializer = serialize.create_serializer(protocol) request_dict = serializer.serialize_to_request(kwargs, self._model) return request_dict
def _botocore_parser_integration_test(service: str, action: str, headers: dict = None, expected: dict = None, **kwargs): # Load the appropriate service service = load_service(service) # Use the serializer from botocore to serialize the request params serializer = create_serializer(service.protocol) operation_model = service.operation_model(action) serialized_request = serializer.serialize_to_request( kwargs, operation_model) body = serialized_request["body"] query_string = urlencode(serialized_request.get("query_string") or "", doseq=False) # use custom headers (if provided), or headers from serialized request as default headers = serialized_request.get("headers") if headers is None else headers if service.protocol in ["query", "ec2"]: # Serialize the body as query parameter body = urlencode(serialized_request["body"]) # Use our parser to parse the serialized body parser = create_parser(service) parsed_operation_model, parsed_request = parser.parse( HttpRequest( method=serialized_request.get("method") or "GET", path=serialized_request.get("url_path") or "", query_string=query_string, headers=headers, body=body, )) # Check if the determined operation_model is correct assert parsed_operation_model == operation_model # Check if the result is equal to the given "expected" dict or the kwargs (if "expected" has not been set) assert parsed_request == (expected or kwargs)
def serialize_to_request(self, input_params): service_model = ServiceModel(self.model) request_serializer = serialize.create_serializer( service_model.metadata['protocol']) return request_serializer.serialize_to_request( input_params, service_model.operation_model('TestOperation'))
def handle_method(fragment): if fragment["Type"] != "AWS::ApiGateway::Method": response_string = "Macro only supports \"AWS::ApiGateway::Method\", user supplied {}" raise InvalidTypeException(response_string.format(fragment["Type"])) service_name = fragment["Properties"]["Integration"].pop("Service").lower() action = fragment["Properties"]["Integration"].pop("Action") response_maps = fragment["Properties"]["Integration"].pop("ResponseMaps") try: fragment.pop("Fn::Transform") except: pass loader = Loader() service_description = loader.load_service_model(service_name=service_name, type_name='service-2') service_model = ServiceModel(service_description) protocol = service_model.protocol op_model = service_model.operation_model(action["Name"]) request_parameters = action.get("Parameters", {}) params = dict(ChainMap(*request_parameters)) print("params: {}".format(params)) serializer = create_serializer(protocol) response_parser = create_parser(protocol) print(service_model.protocol) request = serializer.serialize_to_request(params, op_model) request_object = AWSRequest( method=request['method'], url=get_endpoint(service_model.service_name), data=request['body'], headers=request['headers']) X = request_object.prepare() print("Raw request: {}".format(request)) print("Prepared request: {}".format(X)) integration = fragment["Properties"]["Integration"] new_integration = integration_template() # Copy the existing values to the new template for entry in integration.keys(): new_integration[entry] = integration[entry] # Add headers to cfn template if X.headers is not None and callable(getattr(X.headers, "keys", None)): for header in X.headers.keys(): if header.lower() != 'Content-Length'.lower(): new_integration["RequestParameters"].update({"integration.request.header.{}".format(header): "'{}'".format(X.headers[header])}) # Add Query Strings to cfn template if 'query_string' in request and callable(getattr(request['query_string'], "keys", None)): for query in request['query_string'].keys(): new_integration["RequestParameters"].update({"integration.request.querystring.{}".format(query): "'{}'".format(request['query_string'][query])}) # Set the body if isinstance(X.body, str): new_integration["RequestTemplates"]["application/json"] = X.body else: new_integration["RequestTemplates"]["application/json"] = str(X.body, "utf-8") if X.body else '' new_integration["Uri"] = ":".join([ "arn", "aws", "apigateway", REGION, service_model.endpoint_prefix, "path/" + request["url_path"] ]) new_integration["IntegrationHttpMethod"] = X.method fragment["Properties"]["Integration"] = new_integration print(fragment) return fragment