jsonrpc.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. if req.ClientInfo.TargetPrefix != "" || string(buf) != "{}" {
  45. req.SetBufferBody(buf)
  46. }
  47. if req.ClientInfo.TargetPrefix != "" {
  48. target := req.ClientInfo.TargetPrefix + "." + req.Operation.Name
  49. req.HTTPRequest.Header.Add("X-Amz-Target", target)
  50. }
  51. // Only set the content type if one is not already specified and an
  52. // JSONVersion is specified.
  53. if ct, v := req.HTTPRequest.Header.Get("Content-Type"), req.ClientInfo.JSONVersion; len(ct) == 0 && len(v) != 0 {
  54. jsonVersion := req.ClientInfo.JSONVersion
  55. req.HTTPRequest.Header.Set("Content-Type", "application/x-amz-json-"+jsonVersion)
  56. }
  57. }
  58. // Unmarshal unmarshals a response for a JSON RPC service.
  59. func Unmarshal(req *request.Request) {
  60. defer req.HTTPResponse.Body.Close()
  61. if req.DataFilled() {
  62. err := jsonutil.UnmarshalJSON(req.Data, req.HTTPResponse.Body)
  63. if err != nil {
  64. req.Error = awserr.NewRequestFailure(
  65. awserr.New(request.ErrCodeSerialization, "failed decoding JSON RPC response", err),
  66. req.HTTPResponse.StatusCode,
  67. req.RequestID,
  68. )
  69. }
  70. }
  71. return
  72. }
  73. // UnmarshalMeta unmarshals headers from a response for a JSON RPC service.
  74. func UnmarshalMeta(req *request.Request) {
  75. rest.UnmarshalMeta(req)
  76. }