← All changes
|
includes/sdk/s3/Aws/Api/Serializer/JsonRpcSerializer.php
+19
-6
1.2.2
→
1.4.2
View file →
| @@ -3,8 +3,10 @@ | ||
| 3 | 3 | namespace Dudlewebs\WPMCS\s3\Aws\Api\Serializer; |
| 4 | 4 | |
| 5 | 5 | use Dudlewebs\WPMCS\s3\Aws\Api\Service; |
| 6 | 6 | use Dudlewebs\WPMCS\s3\Aws\CommandInterface; |
| 7 | +use Dudlewebs\WPMCS\s3\Aws\EndpointV2\EndpointV2SerializerTrait; | |
| 8 | +use Dudlewebs\WPMCS\s3\Aws\EndpointV2\Ruleset\RulesetEndpoint; | |
| 7 | 9 | use Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7\Request; |
| 8 | 10 | use Dudlewebs\WPMCS\s3\Psr\Http\Message\RequestInterface; |
| 9 | 11 | /** |
| 10 | 12 | * Prepares a JSON-RPC request for transfer. |
| @@ -11,8 +13,9 @@ | ||
| 11 | 13 | * @internal |
| 12 | 14 | */ |
| 13 | 15 | class JsonRpcSerializer |
| 14 | 16 | { |
| 17 | + use EndpointV2SerializerTrait; | |
| 15 | 18 | /** @var JsonBody */ |
| 16 | 19 | private $jsonFormatter; |
| 17 | 20 | /** @var string */ |
| 18 | 21 | private $endpoint; |
| @@ -24,9 +27,9 @@ | ||
| 24 | 27 | * @param Service $api Service description |
| 25 | 28 | * @param string $endpoint Endpoint to connect to |
| 26 | 29 | * @param JsonBody $jsonFormatter Optional JSON formatter to use |
| 27 | 30 | */ |
| 28 | - public function __construct(Service $api, $endpoint, JsonBody $jsonFormatter = null) | |
| 31 | + public function __construct(Service $api, $endpoint, ?JsonBody $jsonFormatter = null) | |
| 29 | 32 | { |
| 30 | 33 | $this->endpoint = $endpoint; |
| 31 | 34 | $this->api = $api; |
| 32 | 35 | $this->jsonFormatter = $jsonFormatter ?: new JsonBody($this->api); |
| @@ -35,15 +38,25 @@ | ||
| 35 | 38 | /** |
| 36 | 39 | * When invoked with an AWS command, returns a serialization array |
| 37 | 40 | * containing "method", "uri", "headers", and "body" key value pairs. |
| 38 | 41 | * |
| 39 | - * @param CommandInterface $command | |
| 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. | |
| 40 | 45 | * |
| 41 | 46 | * @return RequestInterface |
| 42 | 47 | */ |
| 43 | - public function __invoke(CommandInterface $command) | |
| 48 | + public function __invoke(CommandInterface $command, $endpoint = null) | |
| 44 | 49 | { |
| 45 | - $name = $command->getName(); | |
| 46 | - $operation = $this->api->getOperation($name); | |
| 47 | - return new Request($operation['http']['method'], $this->endpoint, ['X-Amz-Target' => $this->api->getMetadata('targetPrefix') . '.' . $name, 'Content-Type' => $this->contentType], $this->jsonFormatter->build($operation->getInput(), $command->toArray())); | |
| 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); | |
| 48 | 61 | } |
| 49 | 62 | } |