| 1 |
<?php |
| 2 |
namespace Aws\Api\Serializer; |
| 3 |
|
| 4 |
use Aws\Api\Service; |
| 5 |
|
| 6 |
use Aws\Api\Shape; |
| 7 |
use Aws\Api\StructureShape; |
| 8 |
use Aws\CommandInterface; |
| 9 |
use Aws\EndpointV2\EndpointV2SerializerTrait; |
| 10 |
use Aws\EndpointV2\Ruleset\RulesetEndpoint; |
| 11 |
use DateTimeInterface; |
| 12 |
use GuzzleHttp\Psr7; |
| 13 |
use GuzzleHttp\Psr7\Request; |
| 14 |
use GuzzleHttp\Psr7\Uri; |
| 15 |
use Psr\Http\Message\RequestInterface; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
abstract class AbstractRpcV2Serializer |
| 32 |
{ |
| 33 |
protected const HEADER_SMITHY_PROTOCOL = 'Smithy-Protocol'; |
| 34 |
protected const HEADER_CONTENT_TYPE = 'Content-Type'; |
| 35 |
protected const HEADER_ACCEPT = 'Accept'; |
| 36 |
|
| 37 |
|
| 38 |
protected static array $defaultHeaders; |
| 39 |
|
| 40 |
|
| 41 |
private Service $api; |
| 42 |
|
| 43 |
|
| 44 |
private string|Uri $endpoint; |
| 45 |
|
| 46 |
|
| 47 |
private bool $isUseEndpointV2; |
| 48 |
|
| 49 |
use EndpointV2SerializerTrait; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function __construct(Service $api, string|Uri $endpoint) |
| 56 |
{ |
| 57 |
$this->api = $api; |
| 58 |
$this->endpoint = Psr7\Utils::uriFor($endpoint); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function __invoke( |
| 68 |
CommandInterface $command, |
| 69 |
mixed $endpoint = null |
| 70 |
) |
| 71 |
{ |
| 72 |
$commandArgs = $command->toArray(); |
| 73 |
$commandName = $command->getName(); |
| 74 |
$operation = $this->api->getOperation($commandName); |
| 75 |
$headers = static::$defaultHeaders; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
if ($operation['input'] !== null) { |
| 80 |
$body = $this->serialize($operation->getInput(), $commandArgs); |
| 81 |
$headers['Content-Length'] = (string) strlen($body); |
| 82 |
} else { |
| 83 |
unset($headers['Content-Type']); |
| 84 |
} |
| 85 |
|
| 86 |
if ($endpoint instanceof RulesetEndpoint) { |
| 87 |
$this->isUseEndpointV2 = true; |
| 88 |
$this->setEndpointV2RequestOptions($endpoint, $headers); |
| 89 |
$this->endpoint = $endpoint->getUrl(); |
| 90 |
} |
| 91 |
|
| 92 |
$requestTarget = $this->buildRequestTarget( |
| 93 |
$commandName, |
| 94 |
$operation['http']['requestUri'] ?? '' |
| 95 |
); |
| 96 |
$uri = new Uri($this->endpoint . $requestTarget); |
| 97 |
|
| 98 |
return new Request( |
| 99 |
$operation['http']['method'], |
| 100 |
$uri, |
| 101 |
$headers, |
| 102 |
$body ?? null |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
abstract public function serialize( |
| 113 |
StructureShape $inputShape, |
| 114 |
array $commandArgs |
| 115 |
): string; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
abstract protected function resolveBlob(mixed $value): array; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
abstract protected function resolveTimestamp( |
| 136 |
int|float|string|DateTimeInterface $value |
| 137 |
): array; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
protected function resolveInputShape(Shape $shape, mixed $value): mixed |
| 148 |
{ |
| 149 |
switch ($shape->getType()) { |
| 150 |
case 'structure': |
| 151 |
$data = []; |
| 152 |
foreach ($value as $k => $v) { |
| 153 |
if ($v !== null && $shape->hasMember($k)) { |
| 154 |
$valueShape = $shape->getMember($k); |
| 155 |
$data[$valueShape['locationName'] ?: $k] |
| 156 |
= $this->resolveInputShape($valueShape, $v); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
return $data; |
| 161 |
|
| 162 |
case 'list': |
| 163 |
$items = $shape->getMember(); |
| 164 |
foreach ($value as $k => $v) { |
| 165 |
$value[$k] = $this->resolveInputShape($items, $v); |
| 166 |
} |
| 167 |
|
| 168 |
return $value; |
| 169 |
|
| 170 |
case 'map': |
| 171 |
$values = $shape->getValue(); |
| 172 |
foreach ($value as $k => $v) { |
| 173 |
$value[$k] = $this->resolveInputShape($values, $v); |
| 174 |
} |
| 175 |
|
| 176 |
return $value; |
| 177 |
|
| 178 |
case 'timestamp': |
| 179 |
return $this->resolveTimestamp($value); |
| 180 |
|
| 181 |
case 'string': |
| 182 |
return (string) $value; |
| 183 |
|
| 184 |
case 'integer': |
| 185 |
case 'long': |
| 186 |
return (int) $value; |
| 187 |
|
| 188 |
case 'double': |
| 189 |
case 'float': |
| 190 |
return (float) $value; |
| 191 |
|
| 192 |
case 'blob': |
| 193 |
return $this->resolveBlob($value); |
| 194 |
|
| 195 |
default: |
| 196 |
return $value; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
private function buildRequestTarget( |
| 209 |
string $commandName, |
| 210 |
string $requestUri |
| 211 |
): string |
| 212 |
{ |
| 213 |
$requestUri = str_ends_with($requestUri, '/') |
| 214 |
? $requestUri |
| 215 |
: $requestUri . '/'; |
| 216 |
$targetPrefix = $this->api->getMetadata('targetPrefix'); |
| 217 |
|
| 218 |
return "{$requestUri}service/{$targetPrefix}/operation/{$commandName}"; |
| 219 |
} |
| 220 |
} |