unmarshal.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package ec2query
  2. //go:generate go run ../../fixtures/protocol/generate.go ../../fixtures/protocol/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/internal/protocol/xml/xmlutil"
  9. )
  10. // Unmarshal unmarshals a response body for the EC2 protocol.
  11. func Unmarshal(r *request.Request) {
  12. defer r.HTTPResponse.Body.Close()
  13. if r.DataFilled() {
  14. decoder := xml.NewDecoder(r.HTTPResponse.Body)
  15. err := xmlutil.UnmarshalXML(r.Data, decoder, "")
  16. if err != nil {
  17. r.Error = awserr.New("SerializationError", "failed decoding EC2 Query response", err)
  18. return
  19. }
  20. }
  21. }
  22. // UnmarshalMeta unmarshals response headers for the EC2 protocol.
  23. func UnmarshalMeta(r *request.Request) {
  24. // TODO implement unmarshaling of request IDs
  25. }
  26. type xmlErrorResponse struct {
  27. XMLName xml.Name `xml:"Response"`
  28. Code string `xml:"Errors>Error>Code"`
  29. Message string `xml:"Errors>Error>Message"`
  30. RequestID string `xml:"RequestId"`
  31. }
  32. // UnmarshalError unmarshals a response error for the EC2 protocol.
  33. func UnmarshalError(r *request.Request) {
  34. defer r.HTTPResponse.Body.Close()
  35. resp := &xmlErrorResponse{}
  36. err := xml.NewDecoder(r.HTTPResponse.Body).Decode(resp)
  37. if err != nil && err != io.EOF {
  38. r.Error = awserr.New("SerializationError", "failed decoding EC2 Query error response", err)
  39. } else {
  40. r.Error = awserr.NewRequestFailure(
  41. awserr.New(resp.Code, resp.Message, nil),
  42. r.HTTPResponse.StatusCode,
  43. resp.RequestID,
  44. )
  45. }
  46. }