restjson.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Package restjson provides RESTful JSON serialization of AWS
  2. // requests and responses.
  3. package restjson
  4. //go:generate go run -tags codegen ../../../models/protocol_tests/generate.go ../../../models/protocol_tests/input/rest-json.json build_test.go
  5. //go:generate go run -tags codegen ../../../models/protocol_tests/generate.go ../../../models/protocol_tests/output/rest-json.json unmarshal_test.go
  6. import (
  7. "encoding/json"
  8. "io/ioutil"
  9. "strings"
  10. "github.com/aws/aws-sdk-go/aws/awserr"
  11. "github.com/aws/aws-sdk-go/aws/request"
  12. "github.com/aws/aws-sdk-go/private/protocol/jsonrpc"
  13. "github.com/aws/aws-sdk-go/private/protocol/rest"
  14. )
  15. // BuildHandler is a named request handler for building restjson protocol requests
  16. var BuildHandler = request.NamedHandler{Name: "awssdk.restjson.Build", Fn: Build}
  17. // UnmarshalHandler is a named request handler for unmarshaling restjson protocol requests
  18. var UnmarshalHandler = request.NamedHandler{Name: "awssdk.restjson.Unmarshal", Fn: Unmarshal}
  19. // UnmarshalMetaHandler is a named request handler for unmarshaling restjson protocol request metadata
  20. var UnmarshalMetaHandler = request.NamedHandler{Name: "awssdk.restjson.UnmarshalMeta", Fn: UnmarshalMeta}
  21. // UnmarshalErrorHandler is a named request handler for unmarshaling restjson protocol request errors
  22. var UnmarshalErrorHandler = request.NamedHandler{Name: "awssdk.restjson.UnmarshalError", Fn: UnmarshalError}
  23. // Build builds a request for the REST JSON protocol.
  24. func Build(r *request.Request) {
  25. rest.Build(r)
  26. if t := rest.PayloadType(r.Params); t == "structure" || t == "" {
  27. jsonrpc.Build(r)
  28. }
  29. }
  30. // Unmarshal unmarshals a response body for the REST JSON protocol.
  31. func Unmarshal(r *request.Request) {
  32. if t := rest.PayloadType(r.Data); t == "structure" || t == "" {
  33. jsonrpc.Unmarshal(r)
  34. } else {
  35. rest.Unmarshal(r)
  36. }
  37. }
  38. // UnmarshalMeta unmarshals response headers for the REST JSON protocol.
  39. func UnmarshalMeta(r *request.Request) {
  40. rest.UnmarshalMeta(r)
  41. }
  42. // UnmarshalError unmarshals a response error for the REST JSON protocol.
  43. func UnmarshalError(r *request.Request) {
  44. defer r.HTTPResponse.Body.Close()
  45. code := r.HTTPResponse.Header.Get("X-Amzn-Errortype")
  46. bodyBytes, err := ioutil.ReadAll(r.HTTPResponse.Body)
  47. if err != nil {
  48. r.Error = awserr.New("SerializationError", "failed reading REST JSON error response", err)
  49. return
  50. }
  51. if len(bodyBytes) == 0 {
  52. r.Error = awserr.NewRequestFailure(
  53. awserr.New("SerializationError", r.HTTPResponse.Status, nil),
  54. r.HTTPResponse.StatusCode,
  55. "",
  56. )
  57. return
  58. }
  59. var jsonErr jsonErrorResponse
  60. if err := json.Unmarshal(bodyBytes, &jsonErr); err != nil {
  61. r.Error = awserr.New("SerializationError", "failed decoding REST JSON error response", err)
  62. return
  63. }
  64. if code == "" {
  65. code = jsonErr.Code
  66. }
  67. code = strings.SplitN(code, ":", 2)[0]
  68. r.Error = awserr.NewRequestFailure(
  69. awserr.New(code, jsonErr.Message, nil),
  70. r.HTTPResponse.StatusCode,
  71. r.RequestID,
  72. )
  73. }
  74. type jsonErrorResponse struct {
  75. Code string `json:"code"`
  76. Message string `json:"message"`
  77. }