JsonRpcSerializer.php 1.8 KB

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