def _serialize_error( self, error: ServiceException, code: str, sender_fault: bool, serialized: HttpResponse, shape: Shape, operation_model: OperationModel, ) -> None: # EC2 errors look like: # <Response> # <Errors> # <Error> # <Code>InvalidInstanceID.Malformed</Code> # <Message>Invalid id: "1343124"</Message> # </Error> # </Errors> # <RequestID>12345</RequestID> # </Response> # This is different from QueryParser in that it's RequestID, not RequestId # and that the Error tag is in an enclosing Errors tag. attr = ({ "xmlns": operation_model.metadata.get("xmlNamespace") } if "xmlNamespace" in operation_model.metadata else None) root = ETree.Element("Errors", attr) error_tag = ETree.SubElement(root, "Error") self._add_error_tags(code, error, error_tag, sender_fault) request_id = ETree.SubElement(root, "RequestID") request_id.text = gen_amzn_requestid_long() serialized.data = self._encode_payload( ETree.tostring(root, encoding=self.DEFAULT_ENCODING))
def _prepare_additional_traits_in_response( self, response: HttpResponse, operation_model: OperationModel): """Adds the request ID to the headers (in contrast to the body - as in the Query protocol).""" response = super()._prepare_additional_traits_in_response( response, operation_model) response.headers["x-amz-request-id"] = gen_amzn_requestid_long() return response
def _prepare_additional_traits_in_xml(self, root: Optional[ETree.Element]): # The EC2 protocol does not use the root output shape, therefore we need to remove the hierarchy level # below the root level output_node = root[0] for child in output_node: root.append(child) root.remove(output_node) # Add the requestId here (it's not defined in the specs) # For the ec2 and the query protocol, the root cannot be None at this time. request_id = ETree.SubElement(root, "requestId") request_id.text = gen_amzn_requestid_long()
def _serialize_error( self, error: ServiceException, code: str, sender_fault: bool, serialized: HttpResponse, shape: Shape, operation_model: OperationModel, ) -> None: # TODO handle error shapes with members # Check if we need to add a namespace attr = ({ "xmlns": operation_model.metadata.get("xmlNamespace") } if "xmlNamespace" in operation_model.metadata else {}) root = ETree.Element("ErrorResponse", attr) error_tag = ETree.SubElement(root, "Error") self._add_error_tags(code, error, error_tag, sender_fault) request_id = ETree.SubElement(root, "RequestId") request_id.text = gen_amzn_requestid_long() serialized.data = self._encode_payload( ETree.tostring(root, encoding=self.DEFAULT_ENCODING))
def _serialize_error( self, error: ServiceException, code: str, sender_fault: bool, serialized: HttpResponse, shape: Shape, operation_model: OperationModel, ) -> None: # It wouldn't be a spec if there wouldn't be any exceptions. # S3 errors look differently than other service's errors. if operation_model.name == "s3": attr = ({ "xmlns": operation_model.metadata.get("xmlNamespace") } if "xmlNamespace" in operation_model.metadata else None) root = ETree.Element("Error", attr) self._add_error_tags(code, error, root, sender_fault) request_id = ETree.SubElement(root, "RequestId") request_id.text = gen_amzn_requestid_long() serialized.data = self._encode_payload( ETree.tostring(root, encoding=self.DEFAULT_ENCODING)) else: super()._serialize_error(error, code, sender_fault, serialized, shape, operation_model)
def _prepare_additional_traits_in_response( self, response: HttpResponse, operation_model: OperationModel): response.headers["x-amzn-requestid"] = gen_amzn_requestid_long() response = super()._prepare_additional_traits_in_response( response, operation_model) return response
def _prepare_additional_traits_in_xml(self, root: Optional[ETree.Element]): # Add the response metadata here (it's not defined in the specs) # For the ec2 and the query protocol, the root cannot be None at this time. response_metadata = ETree.SubElement(root, "ResponseMetadata") request_id = ETree.SubElement(response_metadata, "RequestId") request_id.text = gen_amzn_requestid_long()