unmarshal.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package ec2query
  2. //go:generate go run -tags codegen ../../../models/protocol_tests/generate.go ../../../models/protocol_tests/output/ec2.json unmarshal_test.go
  3. import (
  4. "encoding/xml"
  5. "io"
  6. "github.com/aws/aws-sdk-go/aws/awserr"
  7. "github.com/aws/aws-sdk-go/aws/request"
  8. "github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil"
  9. )
  10. // UnmarshalHandler is a named request handler for unmarshaling ec2query protocol requests
  11. var UnmarshalHandler = request.NamedHandler{Name: "awssdk.ec2query.Unmarshal", Fn: Unmarshal}
  12. // UnmarshalMetaHandler is a named request handler for unmarshaling ec2query protocol request metadata
  13. var UnmarshalMetaHandler = request.NamedHandler{Name: "awssdk.ec2query.UnmarshalMeta", Fn: UnmarshalMeta}
  14. // UnmarshalErrorHandler is a named request handler for unmarshaling ec2query protocol request errors
  15. var UnmarshalErrorHandler = request.NamedHandler{Name: "awssdk.ec2query.UnmarshalError", Fn: UnmarshalError}
  16. // Unmarshal unmarshals a response body for the EC2 protocol.
  17. func Unmarshal(r *request.Request) {
  18. defer r.HTTPResponse.Body.Close()
  19. if r.DataFilled() {
  20. decoder := xml.NewDecoder(r.HTTPResponse.Body)
  21. err := xmlutil.UnmarshalXML(r.Data, decoder, "")
  22. if err != nil {
  23. r.Error = awserr.New("SerializationError", "failed decoding EC2 Query response", err)
  24. return
  25. }
  26. }
  27. }
  28. // UnmarshalMeta unmarshals response headers for the EC2 protocol.
  29. func UnmarshalMeta(r *request.Request) {
  30. // TODO implement unmarshaling of request IDs
  31. }
  32. type xmlErrorResponse struct {
  33. XMLName xml.Name `xml:"Response"`
  34. Code string `xml:"Errors>Error>Code"`
  35. Message string `xml:"Errors>Error>Message"`
  36. RequestID string `xml:"RequestID"`
  37. }
  38. // UnmarshalError unmarshals a response error for the EC2 protocol.
  39. func UnmarshalError(r *request.Request) {
  40. defer r.HTTPResponse.Body.Close()
  41. resp := &xmlErrorResponse{}
  42. err := xml.NewDecoder(r.HTTPResponse.Body).Decode(resp)
  43. if err != nil && err != io.EOF {
  44. r.Error = awserr.New("SerializationError", "failed decoding EC2 Query error response", err)
  45. } else {
  46. r.Error = awserr.NewRequestFailure(
  47. awserr.New(resp.Code, resp.Message, nil),
  48. r.HTTPResponse.StatusCode,
  49. resp.RequestID,
  50. )
  51. }
  52. }