restxml.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Package restxml provides RESTful XML serialization of AWS
  2. // requests and responses.
  3. package restxml
  4. //go:generate go run -tags codegen ../../../models/protocol_tests/generate.go ../../../models/protocol_tests/input/rest-xml.json build_test.go
  5. //go:generate go run -tags codegen ../../../models/protocol_tests/generate.go ../../../models/protocol_tests/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/private/protocol/query"
  12. "github.com/aws/aws-sdk-go/private/protocol/rest"
  13. "github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil"
  14. )
  15. // BuildHandler is a named request handler for building restxml protocol requests
  16. var BuildHandler = request.NamedHandler{Name: "awssdk.restxml.Build", Fn: Build}
  17. // UnmarshalHandler is a named request handler for unmarshaling restxml protocol requests
  18. var UnmarshalHandler = request.NamedHandler{Name: "awssdk.restxml.Unmarshal", Fn: Unmarshal}
  19. // UnmarshalMetaHandler is a named request handler for unmarshaling restxml protocol request metadata
  20. var UnmarshalMetaHandler = request.NamedHandler{Name: "awssdk.restxml.UnmarshalMeta", Fn: UnmarshalMeta}
  21. // UnmarshalErrorHandler is a named request handler for unmarshaling restxml protocol request errors
  22. var UnmarshalErrorHandler = request.NamedHandler{Name: "awssdk.restxml.UnmarshalError", Fn: UnmarshalError}
  23. // Build builds a request payload for the REST XML protocol.
  24. func Build(r *request.Request) {
  25. rest.Build(r)
  26. if t := rest.PayloadType(r.Params); t == "structure" || t == "" {
  27. var buf bytes.Buffer
  28. err := xmlutil.BuildXML(r.Params, xml.NewEncoder(&buf))
  29. if err != nil {
  30. r.Error = awserr.New("SerializationError", "failed to encode rest XML request", err)
  31. return
  32. }
  33. r.SetBufferBody(buf.Bytes())
  34. }
  35. }
  36. // Unmarshal unmarshals a payload response for the REST XML protocol.
  37. func Unmarshal(r *request.Request) {
  38. if t := rest.PayloadType(r.Data); t == "structure" || t == "" {
  39. defer r.HTTPResponse.Body.Close()
  40. decoder := xml.NewDecoder(r.HTTPResponse.Body)
  41. err := xmlutil.UnmarshalXML(r.Data, decoder, "")
  42. if err != nil {
  43. r.Error = awserr.New("SerializationError", "failed to decode REST XML response", err)
  44. return
  45. }
  46. } else {
  47. rest.Unmarshal(r)
  48. }
  49. }
  50. // UnmarshalMeta unmarshals response headers for the REST XML protocol.
  51. func UnmarshalMeta(r *request.Request) {
  52. rest.UnmarshalMeta(r)
  53. }
  54. // UnmarshalError unmarshals a response error for the REST XML protocol.
  55. func UnmarshalError(r *request.Request) {
  56. query.UnmarshalError(r)
  57. }