restxml.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Package restxml provides RESTful XML serialisation of AWS
  2. // requests and responses.
  3. package restxml
  4. //go:generate go run ../../fixtures/protocol/generate.go ../../fixtures/protocol/input/rest-xml.json build_test.go
  5. //go:generate go run ../../fixtures/protocol/generate.go ../../fixtures/protocol/output/rest-xml.json unmarshal_test.go
  6. import (
  7. "bytes"
  8. "encoding/xml"
  9. "github.com/aws/aws-sdk-go/aws/awserr"
  10. "github.com/aws/aws-sdk-go/aws/request"
  11. "github.com/aws/aws-sdk-go/internal/protocol/query"
  12. "github.com/aws/aws-sdk-go/internal/protocol/rest"
  13. "github.com/aws/aws-sdk-go/internal/protocol/xml/xmlutil"
  14. )
  15. // Build builds a request payload for the REST XML protocol.
  16. func Build(r *request.Request) {
  17. rest.Build(r)
  18. if t := rest.PayloadType(r.Params); t == "structure" || t == "" {
  19. var buf bytes.Buffer
  20. err := xmlutil.BuildXML(r.Params, xml.NewEncoder(&buf))
  21. if err != nil {
  22. r.Error = awserr.New("SerializationError", "failed to encode rest XML request", err)
  23. return
  24. }
  25. r.SetBufferBody(buf.Bytes())
  26. }
  27. }
  28. // Unmarshal unmarshals a payload response for the REST XML protocol.
  29. func Unmarshal(r *request.Request) {
  30. if t := rest.PayloadType(r.Data); t == "structure" || t == "" {
  31. defer r.HTTPResponse.Body.Close()
  32. decoder := xml.NewDecoder(r.HTTPResponse.Body)
  33. err := xmlutil.UnmarshalXML(r.Data, decoder, "")
  34. if err != nil {
  35. r.Error = awserr.New("SerializationError", "failed to decode REST XML response", err)
  36. return
  37. }
  38. } else {
  39. rest.Unmarshal(r)
  40. }
  41. }
  42. // UnmarshalMeta unmarshals response headers for the REST XML protocol.
  43. func UnmarshalMeta(r *request.Request) {
  44. rest.UnmarshalMeta(r)
  45. }
  46. // UnmarshalError unmarshals a response error for the REST XML protocol.
  47. func UnmarshalError(r *request.Request) {
  48. query.UnmarshalError(r)
  49. }