github.com/aavshr/aws-sdk-go@v1.41.3/private/protocol/ec2query/unmarshal.go (about)

     1  package ec2query
     2  
     3  //go:generate go run -tags codegen ../../../private/model/cli/gen-protocol-tests ../../../models/protocol_tests/output/ec2.json unmarshal_test.go
     4  
     5  import (
     6  	"encoding/xml"
     7  
     8  	"github.com/aavshr/aws-sdk-go/aws/awserr"
     9  	"github.com/aavshr/aws-sdk-go/aws/request"
    10  	"github.com/aavshr/aws-sdk-go/private/protocol/xml/xmlutil"
    11  )
    12  
    13  // UnmarshalHandler is a named request handler for unmarshaling ec2query protocol requests
    14  var UnmarshalHandler = request.NamedHandler{Name: "awssdk.ec2query.Unmarshal", Fn: Unmarshal}
    15  
    16  // UnmarshalMetaHandler is a named request handler for unmarshaling ec2query protocol request metadata
    17  var UnmarshalMetaHandler = request.NamedHandler{Name: "awssdk.ec2query.UnmarshalMeta", Fn: UnmarshalMeta}
    18  
    19  // UnmarshalErrorHandler is a named request handler for unmarshaling ec2query protocol request errors
    20  var UnmarshalErrorHandler = request.NamedHandler{Name: "awssdk.ec2query.UnmarshalError", Fn: UnmarshalError}
    21  
    22  // Unmarshal unmarshals a response body for the EC2 protocol.
    23  func Unmarshal(r *request.Request) {
    24  	defer r.HTTPResponse.Body.Close()
    25  	if r.DataFilled() {
    26  		decoder := xml.NewDecoder(r.HTTPResponse.Body)
    27  		err := xmlutil.UnmarshalXML(r.Data, decoder, "")
    28  		if err != nil {
    29  			r.Error = awserr.NewRequestFailure(
    30  				awserr.New(request.ErrCodeSerialization,
    31  					"failed decoding EC2 Query response", err),
    32  				r.HTTPResponse.StatusCode,
    33  				r.RequestID,
    34  			)
    35  			return
    36  		}
    37  	}
    38  }
    39  
    40  // UnmarshalMeta unmarshals response headers for the EC2 protocol.
    41  func UnmarshalMeta(r *request.Request) {
    42  	r.RequestID = r.HTTPResponse.Header.Get("X-Amzn-Requestid")
    43  	if r.RequestID == "" {
    44  		// Alternative version of request id in the header
    45  		r.RequestID = r.HTTPResponse.Header.Get("X-Amz-Request-Id")
    46  	}
    47  }
    48  
    49  type xmlErrorResponse struct {
    50  	XMLName   xml.Name `xml:"Response"`
    51  	Code      string   `xml:"Errors>Error>Code"`
    52  	Message   string   `xml:"Errors>Error>Message"`
    53  	RequestID string   `xml:"RequestID"`
    54  }
    55  
    56  // UnmarshalError unmarshals a response error for the EC2 protocol.
    57  func UnmarshalError(r *request.Request) {
    58  	defer r.HTTPResponse.Body.Close()
    59  
    60  	var respErr xmlErrorResponse
    61  	err := xmlutil.UnmarshalXMLError(&respErr, r.HTTPResponse.Body)
    62  	if err != nil {
    63  		r.Error = awserr.NewRequestFailure(
    64  			awserr.New(request.ErrCodeSerialization,
    65  				"failed to unmarshal error message", err),
    66  			r.HTTPResponse.StatusCode,
    67  			r.RequestID,
    68  		)
    69  		return
    70  	}
    71  
    72  	r.Error = awserr.NewRequestFailure(
    73  		awserr.New(respErr.Code, respErr.Message, nil),
    74  		r.HTTPResponse.StatusCode,
    75  		respErr.RequestID,
    76  	)
    77  }