| 1 |
<?php |
| 2 |
namespace Aws\Api\Serializer; |
| 3 |
|
| 4 |
use Aws\Api\Service; |
| 5 |
use Aws\CommandInterface; |
| 6 |
use Aws\EndpointV2\EndpointV2SerializerTrait; |
| 7 |
use Aws\EndpointV2\Ruleset\RulesetEndpoint; |
| 8 |
use GuzzleHttp\Psr7\Request; |
| 9 |
use Psr\Http\Message\RequestInterface; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class JsonRpcSerializer |
| 16 |
{ |
| 17 |
use EndpointV2SerializerTrait; |
| 18 |
|
| 19 |
|
| 20 |
private $jsonFormatter; |
| 21 |
|
| 22 |
|
| 23 |
private $endpoint; |
| 24 |
|
| 25 |
|
| 26 |
private $api; |
| 27 |
|
| 28 |
|
| 29 |
private $contentType; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public function __construct( |
| 37 |
Service $api, |
| 38 |
$endpoint, |
| 39 |
?JsonBody $jsonFormatter = null |
| 40 |
) { |
| 41 |
$this->endpoint = $endpoint; |
| 42 |
$this->api = $api; |
| 43 |
$this->jsonFormatter = $jsonFormatter ?: new JsonBody($this->api); |
| 44 |
$this->contentType = JsonBody::getContentType($api); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function __invoke( |
| 58 |
CommandInterface $command, |
| 59 |
$endpoint = null |
| 60 |
) |
| 61 |
{ |
| 62 |
$operationName = $command->getName(); |
| 63 |
$operation = $this->api->getOperation($operationName); |
| 64 |
$commandArgs = $command->toArray(); |
| 65 |
$body = $this->jsonFormatter->build($operation->getInput(), $commandArgs); |
| 66 |
$headers = [ |
| 67 |
'X-Amz-Target' => $this->api->getMetadata('targetPrefix') . '.' . $operationName, |
| 68 |
'Content-Type' => $this->contentType, |
| 69 |
'Content-Length' => (string) strlen($body) |
| 70 |
]; |
| 71 |
|
| 72 |
if ($endpoint instanceof RulesetEndpoint) { |
| 73 |
$this->setEndpointV2RequestOptions($endpoint, $headers); |
| 74 |
} |
| 75 |
|
| 76 |
$requestUri = $operation['http']['requestUri'] ?? null; |
| 77 |
$absoluteUri = str_ends_with($this->endpoint, '/') |
| 78 |
? $this->endpoint : $this->endpoint . $requestUri; |
| 79 |
|
| 80 |
return new Request( |
| 81 |
$operation['http']['method'], |
| 82 |
$absoluteUri, |
| 83 |
$headers, |
| 84 |
$body |
| 85 |
); |
| 86 |
} |
| 87 |
} |