2
0

restjson.go 2.9 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"
  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. var jsonErr jsonErrorResponse
  46. err := json.NewDecoder(r.HTTPResponse.Body).Decode(&jsonErr)
  47. if err == io.EOF {
  48. r.Error = awserr.NewRequestFailure(
  49. awserr.New("SerializationError", r.HTTPResponse.Status, nil),
  50. r.HTTPResponse.StatusCode,
  51. r.RequestID,
  52. )
  53. return
  54. } else if err != nil {
  55. r.Error = awserr.NewRequestFailure(
  56. awserr.New("SerializationError", "failed decoding REST JSON error response", err),
  57. r.HTTPResponse.StatusCode,
  58. r.RequestID,
  59. )
  60. return
  61. }
  62. code := r.HTTPResponse.Header.Get("X-Amzn-Errortype")
  63. if code == "" {
  64. code = jsonErr.Code
  65. }
  66. code = strings.SplitN(code, ":", 2)[0]
  67. r.Error = awserr.NewRequestFailure(
  68. awserr.New(code, jsonErr.Message, nil),
  69. r.HTTPResponse.StatusCode,
  70. r.RequestID,
  71. )
  72. }
  73. type jsonErrorResponse struct {
  74. Code string `json:"code"`
  75. Message string `json:"message"`
  76. }