| 1 |
<?php |
| 2 |
namespace Aws\Api\Serializer; |
| 3 |
|
| 4 |
use Aws\Api\ListShape; |
| 5 |
use Aws\Api\MapShape; |
| 6 |
use Aws\Api\Service; |
| 7 |
use Aws\Api\Operation; |
| 8 |
use Aws\Api\Shape; |
| 9 |
use Aws\Api\StructureShape; |
| 10 |
use Aws\Api\TimestampShape; |
| 11 |
use Aws\CommandInterface; |
| 12 |
use Aws\EndpointV2\EndpointV2SerializerTrait; |
| 13 |
use Aws\EndpointV2\Ruleset\RulesetEndpoint; |
| 14 |
use DateTimeInterface; |
| 15 |
use GuzzleHttp\Psr7; |
| 16 |
use GuzzleHttp\Psr7\Request; |
| 17 |
use GuzzleHttp\Psr7\Uri; |
| 18 |
use GuzzleHttp\Psr7\UriResolver; |
| 19 |
use Psr\Http\Message\RequestInterface; |
| 20 |
use Psr\Http\Message\UriInterface; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
abstract class RestSerializer |
| 27 |
{ |
| 28 |
private const TEMPLATE_STRING_REGEX = '/\{([^\}]+)\}/'; |
| 29 |
|
| 30 |
private static array $excludeContentType = [ |
| 31 |
's3' => true, |
| 32 |
'glacier' => true |
| 33 |
]; |
| 34 |
|
| 35 |
|
| 36 |
private Service $api; |
| 37 |
|
| 38 |
|
| 39 |
private $endpoint; |
| 40 |
|
| 41 |
|
| 42 |
private $isUseEndpointV2; |
| 43 |
|
| 44 |
use EndpointV2SerializerTrait; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function __construct(Service $api, $endpoint) |
| 51 |
{ |
| 52 |
$this->api = $api; |
| 53 |
$this->endpoint = Psr7\Utils::uriFor($endpoint); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function __invoke( |
| 62 |
CommandInterface $command, |
| 63 |
mixed $endpoint = null |
| 64 |
) |
| 65 |
{ |
| 66 |
$operation = $this->api->getOperation($command->getName()); |
| 67 |
$commandArgs = $command->toArray(); |
| 68 |
$opts = $this->serialize($operation, $commandArgs); |
| 69 |
$headers = $opts['headers'] ?? []; |
| 70 |
|
| 71 |
if ($endpoint instanceof RulesetEndpoint) { |
| 72 |
$this->isUseEndpointV2 = true; |
| 73 |
$this->setEndpointV2RequestOptions($endpoint, $headers); |
| 74 |
} |
| 75 |
|
| 76 |
$uri = $this->buildEndpoint($operation, $commandArgs, $opts); |
| 77 |
|
| 78 |
return new Request( |
| 79 |
$operation['http']['method'], |
| 80 |
$uri, |
| 81 |
$headers, |
| 82 |
$opts['body'] ?? null |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
abstract protected function payload( |
| 94 |
StructureShape $member, |
| 95 |
array $value, |
| 96 |
array &$opts |
| 97 |
); |
| 98 |
|
| 99 |
private function serialize(Operation $operation, array $args) |
| 100 |
{ |
| 101 |
$opts = []; |
| 102 |
$input = $operation->getInput(); |
| 103 |
|
| 104 |
|
| 105 |
if ($payload = $input['payload']) { |
| 106 |
$this->applyPayload($input, $payload, $args, $opts); |
| 107 |
} |
| 108 |
|
| 109 |
foreach ($args as $name => $value) { |
| 110 |
if ($input->hasMember($name)) { |
| 111 |
$member = $input->getMember($name); |
| 112 |
$location = $member['location']; |
| 113 |
if (!$payload && !$location) { |
| 114 |
$bodyMembers[$name] = $value; |
| 115 |
} elseif ($location === 'header') { |
| 116 |
$this->applyHeader($name, $member, $value, $opts); |
| 117 |
} elseif ($location === 'querystring') { |
| 118 |
$this->applyQuery($name, $member, $value, $opts); |
| 119 |
} elseif ($location === 'headers') { |
| 120 |
$this->applyHeaderMap($name, $member, $value, $opts); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
if (isset($bodyMembers)) { |
| 126 |
$this->payload($input, $bodyMembers, $opts); |
| 127 |
} else if (!isset($opts['body']) && $this->hasPayloadParam($input, $payload)) { |
| 128 |
$this->payload($input, [], $opts); |
| 129 |
} |
| 130 |
|
| 131 |
return $opts; |
| 132 |
} |
| 133 |
|
| 134 |
private function applyPayload(StructureShape $input, $name, array $args, array &$opts) |
| 135 |
{ |
| 136 |
if (!isset($args[$name])) { |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
$m = $input->getMember($name); |
| 141 |
|
| 142 |
$type = $m->getType(); |
| 143 |
if ($m['streaming'] || |
| 144 |
($type === 'string' || $type === 'blob') |
| 145 |
) { |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
if (!isset(self::$excludeContentType[$this->api->getServiceName() ?? ''])) { |
| 150 |
switch ($type) { |
| 151 |
case 'string': |
| 152 |
$opts['headers']['Content-Type'] = 'text/plain'; |
| 153 |
break; |
| 154 |
case 'blob': |
| 155 |
$opts['headers']['Content-Type'] = 'application/octet-stream'; |
| 156 |
break; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
$body = $args[$name]; |
| 161 |
if (!$m['streaming'] && is_string($body)) { |
| 162 |
$opts['headers']['Content-Length'] = (string) strlen($body); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
$opts['body'] = Psr7\Utils::streamFor($body); |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
$this->payload($m, $args[$name], $opts); |
| 172 |
} |
| 173 |
|
| 174 |
private function applyHeader($name, Shape $member, $value, array &$opts) |
| 175 |
{ |
| 176 |
if ($value === null) { |
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
if ($member instanceof ListShape) { |
| 182 |
if (!is_array($value)) { |
| 183 |
throw new \InvalidArgumentException('Header values must be scalar or an array of scalars.'); |
| 184 |
} |
| 185 |
|
| 186 |
$listMember = $member->getMember(); |
| 187 |
$headerValues = []; |
| 188 |
|
| 189 |
foreach ($value as $listValue) { |
| 190 |
if ($listValue === null) { |
| 191 |
throw new \InvalidArgumentException('Header values must be scalar or an array of scalars.'); |
| 192 |
} |
| 193 |
|
| 194 |
$tempOpts = ['headers' => []]; |
| 195 |
$this->applyHeader('temp', $listMember, $listValue, $tempOpts); |
| 196 |
if (!array_key_exists('temp', $tempOpts['headers'])) { |
| 197 |
throw new \InvalidArgumentException('Header values must be scalar or an array of scalars.'); |
| 198 |
} |
| 199 |
|
| 200 |
$convertedValue = $tempOpts['headers']['temp']; |
| 201 |
$headerValues[] = $convertedValue; |
| 202 |
} |
| 203 |
|
| 204 |
$value = $headerValues; |
| 205 |
} else { |
| 206 |
switch ($member->getType()) { |
| 207 |
case 'timestamp': |
| 208 |
$timestampFormat = $member['timestampFormat'] ?? 'rfc822'; |
| 209 |
$value = $this->formatTimestamp($value, $timestampFormat); |
| 210 |
break; |
| 211 |
case 'boolean': |
| 212 |
$value = $this->formatBoolean($value); |
| 213 |
break; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
if ($member['jsonvalue']) { |
| 218 |
$value = json_encode($value); |
| 219 |
if (empty($value) && JSON_ERROR_NONE !== json_last_error()) { |
| 220 |
throw new \InvalidArgumentException('Unable to encode the provided value' |
| 221 |
. ' with \'json_encode\'. ' . json_last_error_msg()); |
| 222 |
} |
| 223 |
|
| 224 |
$value = base64_encode($value); |
| 225 |
} |
| 226 |
|
| 227 |
$opts['headers'][$member['locationName'] ?: $name] = self::prepareHeaderValue($value); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
private function applyHeaderMap($name, Shape $member, array $value, array &$opts) |
| 234 |
{ |
| 235 |
$prefix = $member['locationName']; |
| 236 |
foreach ($value as $k => $v) { |
| 237 |
if ($v === null) { |
| 238 |
continue; |
| 239 |
} |
| 240 |
|
| 241 |
$opts['headers'][$prefix . $k] = self::prepareHeaderValue($v); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
private static function prepareHeaderValue($value) |
| 249 |
{ |
| 250 |
if (is_scalar($value)) { |
| 251 |
return (string) $value; |
| 252 |
} |
| 253 |
|
| 254 |
if (is_array($value)) { |
| 255 |
if ($value === []) { |
| 256 |
return ''; |
| 257 |
} |
| 258 |
|
| 259 |
foreach ($value as $key => $item) { |
| 260 |
if (!is_scalar($item)) { |
| 261 |
throw new \InvalidArgumentException('Header values must be scalar or an array of scalars.'); |
| 262 |
} |
| 263 |
|
| 264 |
$value[$key] = (string) $item; |
| 265 |
} |
| 266 |
|
| 267 |
return $value; |
| 268 |
} |
| 269 |
|
| 270 |
throw new \InvalidArgumentException('Header values must be scalar or an array of scalars.'); |
| 271 |
} |
| 272 |
|
| 273 |
private function applyQuery($name, Shape $member, $value, array &$opts) |
| 274 |
{ |
| 275 |
if ($member instanceof MapShape) { |
| 276 |
$opts['query'] = isset($opts['query']) && is_array($opts['query']) |
| 277 |
? $opts['query'] + $value |
| 278 |
: $value; |
| 279 |
} elseif ($member instanceof ListShape) { |
| 280 |
$listMember = $member->getMember(); |
| 281 |
$paramName = $member['locationName'] ?: $name; |
| 282 |
|
| 283 |
foreach ($value as $listValue) { |
| 284 |
|
| 285 |
$tempOpts = ['query' => []]; |
| 286 |
$this->applyQuery('temp', $listMember, $listValue, $tempOpts); |
| 287 |
$opts['query'][$paramName][] = $tempOpts['query']['temp']; |
| 288 |
} |
| 289 |
} elseif (!is_null($value)) { |
| 290 |
switch ($member->getType()) { |
| 291 |
case 'timestamp': |
| 292 |
$timestampFormat = $member['timestampFormat'] ?? 'iso8601'; |
| 293 |
$value = $this->formatTimestamp($value, $timestampFormat); |
| 294 |
break; |
| 295 |
case 'boolean': |
| 296 |
$value = $this->formatBoolean($value); |
| 297 |
break; |
| 298 |
} |
| 299 |
|
| 300 |
$opts['query'][$member['locationName'] ?: $name] = $value; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
private function buildEndpoint( |
| 305 |
Operation $operation, |
| 306 |
array $args, |
| 307 |
array $opts |
| 308 |
): UriInterface |
| 309 |
{ |
| 310 |
|
| 311 |
$relativeUri = $this->expandUriTemplate($operation, $args); |
| 312 |
|
| 313 |
|
| 314 |
if (!empty($opts['query'])) { |
| 315 |
$relativeUri = $this->appendQuery($opts['query'], $relativeUri); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
if ($this->api->getServiceName() === 's3' |
| 320 |
&& isset($args['Key']) |
| 321 |
&& $this->shouldPreservePath($args['Key']) |
| 322 |
) { |
| 323 |
return new Uri($this->endpoint . $relativeUri); |
| 324 |
} |
| 325 |
|
| 326 |
return $this->resolveUri($relativeUri, $opts); |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
private function expandUriTemplate(Operation $operation, array $args): string |
| 338 |
{ |
| 339 |
$varDefinitions = $this->getVarDefinitions($operation, $args); |
| 340 |
|
| 341 |
return preg_replace_callback( |
| 342 |
self::TEMPLATE_STRING_REGEX, |
| 343 |
static function (array $matches) use ($varDefinitions) { |
| 344 |
$isGreedy = str_ends_with($matches[1], '+'); |
| 345 |
$varName = $isGreedy ? substr($matches[1], 0, -1) : $matches[1]; |
| 346 |
|
| 347 |
if (!isset($varDefinitions[$varName])) { |
| 348 |
return ''; |
| 349 |
} |
| 350 |
|
| 351 |
$value = $varDefinitions[$varName]; |
| 352 |
|
| 353 |
if ($isGreedy) { |
| 354 |
return str_replace('%2F', '/', rawurlencode($value)); |
| 355 |
} |
| 356 |
|
| 357 |
return rawurlencode($value); |
| 358 |
}, |
| 359 |
$operation['http']['requestUri'] |
| 360 |
); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
private function shouldPreservePath(string $key): bool |
| 371 |
{ |
| 372 |
|
| 373 |
if (str_contains($key, '.')) { |
| 374 |
$segments = explode('/', $key); |
| 375 |
foreach ($segments as $segment) { |
| 376 |
if ($segment === '.' || $segment === '..') { |
| 377 |
return true; |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
if (str_starts_with($key, '/')) { |
| 384 |
return true; |
| 385 |
} |
| 386 |
|
| 387 |
return false; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
private function resolveUri(string $relativeUri, array $opts): UriInterface |
| 397 |
{ |
| 398 |
$basePath = $this->endpoint->getPath(); |
| 399 |
|
| 400 |
|
| 401 |
if (!empty($basePath) && $basePath !== '/') { |
| 402 |
|
| 403 |
if ($relativeUri === '/' || empty($relativeUri)) { |
| 404 |
|
| 405 |
return $this->endpoint->withPath(rtrim($basePath, '/')); |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
if (empty($opts['query']) |
| 411 |
&& str_starts_with($relativeUri, '/?') |
| 412 |
) { |
| 413 |
$query = substr($relativeUri, 2); |
| 414 |
return $this->endpoint->withQuery($query); |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
if (!str_ends_with($basePath, '/')) { |
| 419 |
$this->endpoint = $this->endpoint->withPath($basePath . '/'); |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
if (str_starts_with($relativeUri, '/')) { |
| 424 |
$relativeUri = substr($relativeUri, 1); |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
return UriResolver::resolve($this->endpoint, new Uri($relativeUri)); |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
private function hasPayloadParam(StructureShape $input, $payload) |
| 438 |
{ |
| 439 |
if ($payload) { |
| 440 |
$potentiallyEmptyTypes = ['blob','string']; |
| 441 |
if ($this->api->getProtocol() === 'rest-xml') { |
| 442 |
$potentiallyEmptyTypes[] = 'structure'; |
| 443 |
} |
| 444 |
|
| 445 |
$payloadMember = $input->getMember($payload); |
| 446 |
|
| 447 |
if (!empty($payloadMember['union']) |
| 448 |
|| in_array($payloadMember['type'], $potentiallyEmptyTypes) |
| 449 |
) { |
| 450 |
return false; |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
foreach ($input->getMembers() as $member) { |
| 455 |
if (!isset($member['location'])) { |
| 456 |
return true; |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
return false; |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
private function appendQuery($query, $relativeUri): string |
| 470 |
{ |
| 471 |
$append = Psr7\Query::build($query); |
| 472 |
return $relativeUri |
| 473 |
. (str_contains($relativeUri, '?') ? "&{$append}" : "?{$append}"); |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
private function getVarDefinitions( |
| 483 |
Operation $operation, |
| 484 |
array $args |
| 485 |
): array |
| 486 |
{ |
| 487 |
$varDefinitions = []; |
| 488 |
|
| 489 |
foreach ($operation->getInput()->getMembers() as $name => $member) { |
| 490 |
if ($member['location'] === 'uri') { |
| 491 |
$value = $args[$name] ?? null; |
| 492 |
if (!is_null($value)) { |
| 493 |
switch ($member->getType()) { |
| 494 |
case 'timestamp': |
| 495 |
$timestampFormat = $member['timestampFormat'] ?? 'iso8601'; |
| 496 |
$value = $this->formatTimestamp($value, $timestampFormat); |
| 497 |
break; |
| 498 |
case 'boolean': |
| 499 |
$value = $this->formatBoolean($value); |
| 500 |
break; |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
$varDefinitions[$member['locationName'] ?: $name] = $value; |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
return $varDefinitions; |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
private function formatTimestamp( |
| 518 |
DateTimeInterface|string|int $value, |
| 519 |
string $timestampFormat |
| 520 |
): string |
| 521 |
{ |
| 522 |
return TimestampShape::format($value, $timestampFormat); |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
private function formatBoolean($value): string |
| 531 |
{ |
| 532 |
return $value ? 'true' : 'false'; |
| 533 |
} |
| 534 |
} |