JsonRpcSerializer.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace Aws\Api\Serializer;
  3. use Aws\Api\Service;
  4. use Aws\CommandInterface;
  5. use Aws\EndpointV2\EndpointV2SerializerTrait;
  6. use Aws\EndpointV2\Ruleset\RulesetEndpoint;
  7. use GuzzleHttp\Psr7\Request;
  8. use Psr\Http\Message\RequestInterface;
  9. /**
  10. * Prepares a JSON-RPC request for transfer.
  11. * @internal
  12. */
  13. class JsonRpcSerializer
  14. {
  15. use EndpointV2SerializerTrait;
  16. /** @var JsonBody */
  17. private $jsonFormatter;
  18. /** @var string */
  19. private $endpoint;
  20. /** @var Service */
  21. private $api;
  22. /** @var string */
  23. private $contentType;
  24. /**
  25. * @param Service $api Service description
  26. * @param string $endpoint Endpoint to connect to
  27. * @param JsonBody $jsonFormatter Optional JSON formatter to use
  28. */
  29. public function __construct(
  30. Service $api,
  31. $endpoint,
  32. JsonBody $jsonFormatter = null
  33. ) {
  34. $this->endpoint = $endpoint;
  35. $this->api = $api;
  36. $this->jsonFormatter = $jsonFormatter ?: new JsonBody($this->api);
  37. $this->contentType = JsonBody::getContentType($api);
  38. }
  39. /**
  40. * When invoked with an AWS command, returns a serialization array
  41. * containing "method", "uri", "headers", and "body" key value pairs.
  42. *
  43. * @param CommandInterface $command Command to serialize into a request.
  44. * @param $endpointProvider Provider used for dynamic endpoint resolution.
  45. * @param $clientArgs Client arguments used for dynamic endpoint resolution.
  46. *
  47. * @return RequestInterface
  48. */
  49. public function __invoke(
  50. CommandInterface $command,
  51. $endpoint = null
  52. )
  53. {
  54. $operationName = $command->getName();
  55. $operation = $this->api->getOperation($operationName);
  56. $commandArgs = $command->toArray();
  57. $headers = [
  58. 'X-Amz-Target' => $this->api->getMetadata('targetPrefix') . '.' . $operationName,
  59. 'Content-Type' => $this->contentType
  60. ];
  61. if ($endpoint instanceof RulesetEndpoint) {
  62. $this->setEndpointV2RequestOptions($endpoint, $headers);
  63. }
  64. return new Request(
  65. $operation['http']['method'],
  66. $this->endpoint,
  67. $headers,
  68. $this->jsonFormatter->build(
  69. $operation->getInput(),
  70. $commandArgs
  71. )
  72. );
  73. }
  74. }