jsonrpc.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Package jsonrpc provides JSON RPC utilities for serialization of AWS
  2. // requests and responses.
  3. package jsonrpc
  4. //go:generate go run -tags codegen ../../../private/model/cli/gen-protocol-tests ../../../models/protocol_tests/input/json.json build_test.go
  5. //go:generate go run -tags codegen ../../../private/model/cli/gen-protocol-tests ../../../models/protocol_tests/output/json.json unmarshal_test.go
  6. import (
  7. "github.com/aws/aws-sdk-go/aws/awserr"
  8. "github.com/aws/aws-sdk-go/aws/request"
  9. "github.com/aws/aws-sdk-go/private/protocol/json/jsonutil"
  10. "github.com/aws/aws-sdk-go/private/protocol/rest"
  11. )
  12. var emptyJSON = []byte("{}")
  13. // BuildHandler is a named request handler for building jsonrpc protocol
  14. // requests
  15. var BuildHandler = request.NamedHandler{
  16. Name: "awssdk.jsonrpc.Build",
  17. Fn: Build,
  18. }
  19. // UnmarshalHandler is a named request handler for unmarshaling jsonrpc
  20. // protocol requests
  21. var UnmarshalHandler = request.NamedHandler{
  22. Name: "awssdk.jsonrpc.Unmarshal",
  23. Fn: Unmarshal,
  24. }
  25. // UnmarshalMetaHandler is a named request handler for unmarshaling jsonrpc
  26. // protocol request metadata
  27. var UnmarshalMetaHandler = request.NamedHandler{
  28. Name: "awssdk.jsonrpc.UnmarshalMeta",
  29. Fn: UnmarshalMeta,
  30. }
  31. // Build builds a JSON payload for a JSON RPC request.
  32. func Build(req *request.Request) {
  33. var buf []byte
  34. var err error
  35. if req.ParamsFilled() {
  36. buf, err = jsonutil.BuildJSON(req.Params)
  37. if err != nil {
  38. req.Error = awserr.New(request.ErrCodeSerialization, "failed encoding JSON RPC request", err)
  39. return
  40. }
  41. } else {
  42. buf = emptyJSON
  43. }
  44. // Always serialize the body, don't suppress it.
  45. req.SetBufferBody(buf)
  46. if req.ClientInfo.TargetPrefix != "" {
  47. target := req.ClientInfo.TargetPrefix + "." + req.Operation.Name
  48. req.HTTPRequest.Header.Add("X-Amz-Target", target)
  49. }
  50. // Only set the content type if one is not already specified and an
  51. // JSONVersion is specified.
  52. if ct, v := req.HTTPRequest.Header.Get("Content-Type"), req.ClientInfo.JSONVersion; len(ct) == 0 && len(v) != 0 {
  53. jsonVersion := req.ClientInfo.JSONVersion
  54. req.HTTPRequest.Header.Set("Content-Type", "application/x-amz-json-"+jsonVersion)
  55. }
  56. }
  57. // Unmarshal unmarshals a response for a JSON RPC service.
  58. func Unmarshal(req *request.Request) {
  59. defer req.HTTPResponse.Body.Close()
  60. if req.DataFilled() {
  61. err := jsonutil.UnmarshalJSON(req.Data, req.HTTPResponse.Body)
  62. if err != nil {
  63. req.Error = awserr.NewRequestFailure(
  64. awserr.New(request.ErrCodeSerialization, "failed decoding JSON RPC response", err),
  65. req.HTTPResponse.StatusCode,
  66. req.RequestID,
  67. )
  68. }
  69. }
  70. return
  71. }
  72. // UnmarshalMeta unmarshals headers from a response for a JSON RPC service.
  73. func UnmarshalMeta(req *request.Request) {
  74. rest.UnmarshalMeta(req)
  75. }