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