payload.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package protocol
  2. import (
  3. "io"
  4. "io/ioutil"
  5. "net/http"
  6. "github.com/aws/aws-sdk-go/aws"
  7. "github.com/aws/aws-sdk-go/aws/client/metadata"
  8. "github.com/aws/aws-sdk-go/aws/request"
  9. )
  10. // PayloadUnmarshaler provides the interface for unmarshaling a payload's
  11. // reader into a SDK shape.
  12. type PayloadUnmarshaler interface {
  13. UnmarshalPayload(io.Reader, interface{}) error
  14. }
  15. // HandlerPayloadUnmarshal implements the PayloadUnmarshaler from a
  16. // HandlerList. This provides the support for unmarshaling a payload reader to
  17. // a shape without needing a SDK request first.
  18. type HandlerPayloadUnmarshal struct {
  19. Unmarshalers request.HandlerList
  20. }
  21. // UnmarshalPayload unmarshals the io.Reader payload into the SDK shape using
  22. // the Unmarshalers HandlerList provided. Returns an error if unable
  23. // unmarshaling fails.
  24. func (h HandlerPayloadUnmarshal) UnmarshalPayload(r io.Reader, v interface{}) error {
  25. req := &request.Request{
  26. HTTPRequest: &http.Request{},
  27. HTTPResponse: &http.Response{
  28. StatusCode: 200,
  29. Header: http.Header{},
  30. Body: ioutil.NopCloser(r),
  31. },
  32. Data: v,
  33. }
  34. h.Unmarshalers.Run(req)
  35. return req.Error
  36. }
  37. // PayloadMarshaler provides the interface for marshaling a SDK shape into and
  38. // io.Writer.
  39. type PayloadMarshaler interface {
  40. MarshalPayload(io.Writer, interface{}) error
  41. }
  42. // HandlerPayloadMarshal implements the PayloadMarshaler from a HandlerList.
  43. // This provides support for marshaling a SDK shape into an io.Writer without
  44. // needing a SDK request first.
  45. type HandlerPayloadMarshal struct {
  46. Marshalers request.HandlerList
  47. }
  48. // MarshalPayload marshals the SDK shape into the io.Writer using the
  49. // Marshalers HandlerList provided. Returns an error if unable if marshal
  50. // fails.
  51. func (h HandlerPayloadMarshal) MarshalPayload(w io.Writer, v interface{}) error {
  52. req := request.New(
  53. aws.Config{},
  54. metadata.ClientInfo{},
  55. request.Handlers{},
  56. nil,
  57. &request.Operation{HTTPMethod: "PUT"},
  58. v,
  59. nil,
  60. )
  61. h.Marshalers.Run(req)
  62. if req.Error != nil {
  63. return req.Error
  64. }
  65. io.Copy(w, req.GetBody())
  66. return nil
  67. }