| @@ -1,11 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace Dudlewebs\WPMCS\s3\Aws\Api; |
| 4 | 4 | |
| 5 | -use Dudlewebs\WPMCS\s3\Aws\Api\Serializer\QuerySerializer; | |
| 6 | -use Dudlewebs\WPMCS\s3\Aws\Api\Serializer\Ec2ParamBuilder; | |
| 7 | -use Dudlewebs\WPMCS\s3\Aws\Api\Parser\QueryParser; | |
| 8 | 5 | /** |
| 9 | 6 | * Represents a web service API model. |
| 10 | 7 | */ |
| 11 | 8 | class Service extends AbstractModel |
| @@ -15,8 +12,10 @@ | ||
| 15 | 12 | /** @var string */ |
| 16 | 13 | private $serviceName; |
| 17 | 14 | /** @var string */ |
| 18 | 15 | private $apiVersion; |
| 16 | + /** @var array */ | |
| 17 | + private $clientContextParams = []; | |
| 19 | 18 | /** @var Operation[] */ |
| 20 | 19 | private $operations = []; |
| 21 | 20 | /** @var array */ |
| 22 | 21 | private $paginators = null; |
| @@ -21,8 +20,12 @@ | ||
| 21 | 20 | /** @var array */ |
| 22 | 21 | private $paginators = null; |
| 23 | 22 | /** @var array */ |
| 24 | 23 | private $waiters = null; |
| 24 | + /** @var boolean */ | |
| 25 | + private $modifiedModel = \false; | |
| 26 | + /** @var string */ | |
| 27 | + private $protocol; | |
| 25 | 28 | /** |
| 26 | 29 | * @param array $definition |
| 27 | 30 | * @param callable $provider |
| 28 | 31 | * |
| @@ -29,9 +32,9 @@ | ||
| 29 | 32 | * @internal param array $definition Service description |
| 30 | 33 | */ |
| 31 | 34 | public function __construct(array $definition, callable $provider) |
| 32 | 35 | { |
| 33 | - static $defaults = ['operations' => [], 'shapes' => [], 'metadata' => []], $defaultMeta = ['apiVersion' => null, 'serviceFullName' => null, 'serviceId' => null, 'endpointPrefix' => null, 'signingName' => null, 'signatureVersion' => null, 'protocol' => null, 'uid' => null]; | |
| 36 | + static $defaults = ['operations' => [], 'shapes' => [], 'metadata' => [], 'clientContextParams' => []], $defaultMeta = ['apiVersion' => null, 'serviceFullName' => null, 'serviceId' => null, 'endpointPrefix' => null, 'signingName' => null, 'signatureVersion' => null, 'protocol' => null, 'uid' => null]; | |
| 34 | 37 | $definition += $defaults; |
| 35 | 38 | $definition['metadata'] += $defaultMeta; |
| 36 | 39 | $this->definition = $definition; |
| 37 | 40 | $this->apiProvider = $provider; |
| @@ -41,8 +44,12 @@ | ||
| 41 | 44 | } else { |
| 42 | 45 | $this->serviceName = $this->getEndpointPrefix(); |
| 43 | 46 | } |
| 44 | 47 | $this->apiVersion = $this->getApiVersion(); |
| 48 | + if (isset($definition['clientContextParams'])) { | |
| 49 | + $this->clientContextParams = $definition['clientContextParams']; | |
| 50 | + } | |
| 51 | + $this->protocol = $this->selectProtocol($definition); | |
| 45 | 52 | } |
| 46 | 53 | /** |
| 47 | 54 | * Creates a request serializer for the provided API object. |
| 48 | 55 | * |
| @@ -53,15 +60,15 @@ | ||
| 53 | 60 | * @throws \UnexpectedValueException |
| 54 | 61 | */ |
| 55 | 62 | public static function createSerializer(Service $api, $endpoint) |
| 56 | 63 | { |
| 57 | - static $mapping = ['json' => 'Dudlewebs\\WPMCS\\s3\\Aws\\Api\\Serializer\\JsonRpcSerializer', 'query' => 'Dudlewebs\\WPMCS\\s3\\Aws\\Api\\Serializer\\QuerySerializer', 'rest-json' => 'Dudlewebs\\WPMCS\\s3\\Aws\\Api\\Serializer\\RestJsonSerializer', 'rest-xml' => 'Dudlewebs\\WPMCS\\s3\\Aws\\Api\\Serializer\\RestXmlSerializer']; | |
| 64 | + static $mapping = ['json' => Serializer\JsonRpcSerializer::class, 'query' => Serializer\QuerySerializer::class, 'rest-json' => Serializer\RestJsonSerializer::class, 'rest-xml' => Serializer\RestXmlSerializer::class]; | |
| 58 | 65 | $proto = $api->getProtocol(); |
| 59 | 66 | if (isset($mapping[$proto])) { |
| 60 | 67 | return new $mapping[$proto]($api, $endpoint); |
| 61 | 68 | } |
| 62 | 69 | if ($proto == 'ec2') { |
| 63 | - return new QuerySerializer($api, $endpoint, new Ec2ParamBuilder()); | |
| 70 | + return new Serializer\QuerySerializer($api, $endpoint, new Serializer\Ec2ParamBuilder()); | |
| 64 | 71 | } |
| 65 | 72 | throw new \UnexpectedValueException('Unknown protocol: ' . $api->getProtocol()); |
| 66 | 73 | } |
| 67 | 74 | /** |
| @@ -73,11 +80,11 @@ | ||
| 73 | 80 | * |
| 74 | 81 | * @return callable |
| 75 | 82 | * @throws \UnexpectedValueException |
| 76 | 83 | */ |
| 77 | - public static function createErrorParser($protocol, Service $api = null) | |
| 84 | + public static function createErrorParser($protocol, ?Service $api = null) | |
| 78 | 85 | { |
| 79 | - static $mapping = ['json' => 'Dudlewebs\\WPMCS\\s3\\Aws\\Api\\ErrorParser\\JsonRpcErrorParser', 'query' => 'Dudlewebs\\WPMCS\\s3\\Aws\\Api\\ErrorParser\\XmlErrorParser', 'rest-json' => 'Dudlewebs\\WPMCS\\s3\\Aws\\Api\\ErrorParser\\RestJsonErrorParser', 'rest-xml' => 'Dudlewebs\\WPMCS\\s3\\Aws\\Api\\ErrorParser\\XmlErrorParser', 'ec2' => 'Dudlewebs\\WPMCS\\s3\\Aws\\Api\\ErrorParser\\XmlErrorParser']; | |
| 86 | + static $mapping = ['json' => ErrorParser\JsonRpcErrorParser::class, 'query' => ErrorParser\XmlErrorParser::class, 'rest-json' => ErrorParser\RestJsonErrorParser::class, 'rest-xml' => ErrorParser\XmlErrorParser::class, 'ec2' => ErrorParser\XmlErrorParser::class]; | |
| 80 | 87 | if (isset($mapping[$protocol])) { |
| 81 | 88 | return new $mapping[$protocol]($api); |
| 82 | 89 | } |
| 83 | 90 | throw new \UnexpectedValueException("Unknown protocol: {$protocol}"); |
| @@ -90,15 +97,15 @@ | ||
| 90 | 97 | * @throws \UnexpectedValueException |
| 91 | 98 | */ |
| 92 | 99 | public static function createParser(Service $api) |
| 93 | 100 | { |
| 94 | - static $mapping = ['json' => 'Dudlewebs\\WPMCS\\s3\\Aws\\Api\\Parser\\JsonRpcParser', 'query' => 'Dudlewebs\\WPMCS\\s3\\Aws\\Api\\Parser\\QueryParser', 'rest-json' => 'Dudlewebs\\WPMCS\\s3\\Aws\\Api\\Parser\\RestJsonParser', 'rest-xml' => 'Dudlewebs\\WPMCS\\s3\\Aws\\Api\\Parser\\RestXmlParser']; | |
| 101 | + static $mapping = ['json' => Parser\JsonRpcParser::class, 'query' => Parser\QueryParser::class, 'rest-json' => Parser\RestJsonParser::class, 'rest-xml' => Parser\RestXmlParser::class]; | |
| 95 | 102 | $proto = $api->getProtocol(); |
| 96 | 103 | if (isset($mapping[$proto])) { |
| 97 | 104 | return new $mapping[$proto]($api); |
| 98 | 105 | } |
| 99 | 106 | if ($proto == 'ec2') { |
| 100 | - return new QueryParser($api, null, \false); | |
| 107 | + return new Parser\QueryParser($api, null, \false); | |
| 101 | 108 | } |
| 102 | 109 | throw new \UnexpectedValueException('Unknown protocol: ' . $api->getProtocol()); |
| 103 | 110 | } |
| 104 | 111 | /** |
| @@ -152,9 +159,9 @@ | ||
| 152 | 159 | * @return string |
| 153 | 160 | */ |
| 154 | 161 | public function getServiceName() |
| 155 | 162 | { |
| 156 | - return $this->definition['metadata']['serviceIdentifier']; | |
| 163 | + return $this->definition['metadata']['serviceIdentifier'] ?? null; | |
| 157 | 164 | } |
| 158 | 165 | /** |
| 159 | 166 | * Get the default signature version of the service. |
| 160 | 167 | * |
| @@ -172,9 +179,9 @@ | ||
| 172 | 179 | * @return string |
| 173 | 180 | */ |
| 174 | 181 | public function getProtocol() |
| 175 | 182 | { |
| 176 | - return $this->definition['metadata']['protocol']; | |
| 183 | + return $this->protocol; | |
| 177 | 184 | } |
| 178 | 185 | /** |
| 179 | 186 | * Get the uid string used by the service |
| 180 | 187 | * |
| @@ -209,8 +216,10 @@ | ||
| 209 | 216 | if (!isset($this->definition['operations'][$name])) { |
| 210 | 217 | throw new \InvalidArgumentException("Unknown operation: {$name}"); |
| 211 | 218 | } |
| 212 | 219 | $this->operations[$name] = new Operation($this->definition['operations'][$name], $this->shapeMap); |
| 220 | + } elseif ($this->modifiedModel) { | |
| 221 | + $this->operations[$name] = new Operation($this->definition['operations'][$name], $this->shapeMap); | |
| 213 | 222 | } |
| 214 | 223 | return $this->operations[$name]; |
| 215 | 224 | } |
| 216 | 225 | /** |
| @@ -352,6 +361,80 @@ | ||
| 352 | 361 | */ |
| 353 | 362 | public function getShapeMap() |
| 354 | 363 | { |
| 355 | 364 | return $this->shapeMap; |
| 365 | + } | |
| 366 | + /** | |
| 367 | + * Get all the context params of the description. | |
| 368 | + * | |
| 369 | + * @return array | |
| 370 | + */ | |
| 371 | + public function getClientContextParams() | |
| 372 | + { | |
| 373 | + return $this->clientContextParams; | |
| 374 | + } | |
| 375 | + /** | |
| 376 | + * Get the service's api provider. | |
| 377 | + * | |
| 378 | + * @return callable | |
| 379 | + */ | |
| 380 | + public function getProvider() | |
| 381 | + { | |
| 382 | + return $this->apiProvider; | |
| 383 | + } | |
| 384 | + /** | |
| 385 | + * Get the service's definition. | |
| 386 | + * | |
| 387 | + * @return callable | |
| 388 | + */ | |
| 389 | + public function getDefinition() | |
| 390 | + { | |
| 391 | + return $this->definition; | |
| 392 | + } | |
| 393 | + /** | |
| 394 | + * Sets the service's api definition. | |
| 395 | + * Intended for internal use only. | |
| 396 | + * | |
| 397 | + * @return void | |
| 398 | + * | |
| 399 | + * @internal | |
| 400 | + */ | |
| 401 | + public function setDefinition($definition) | |
| 402 | + { | |
| 403 | + $this->definition = $definition; | |
| 404 | + $this->shapeMap = new ShapeMap($definition['shapes']); | |
| 405 | + $this->modifiedModel = \true; | |
| 406 | + } | |
| 407 | + /** | |
| 408 | + * Denotes whether or not a service's definition has | |
| 409 | + * been modified. Intended for internal use only. | |
| 410 | + * | |
| 411 | + * @return bool | |
| 412 | + * | |
| 413 | + * @internal | |
| 414 | + */ | |
| 415 | + public function isModifiedModel() | |
| 416 | + { | |
| 417 | + return $this->modifiedModel; | |
| 418 | + } | |
| 419 | + /** | |
| 420 | + * Accepts a list of protocols derived from the service model. | |
| 421 | + * Returns the highest priority compatible auth scheme if the `protocols` trait is present. | |
| 422 | + * Otherwise, returns the value of the `protocol` field, if set, or null. | |
| 423 | + * | |
| 424 | + * @param array $definition | |
| 425 | + * | |
| 426 | + * @return string|null | |
| 427 | + */ | |
| 428 | + private function selectProtocol(array $definition) : string|null | |
| 429 | + { | |
| 430 | + $modeledProtocols = $definition['metadata']['protocols'] ?? null; | |
| 431 | + if (!empty($modeledProtocols)) { | |
| 432 | + foreach (SupportedProtocols::cases() as $protocol) { | |
| 433 | + if (\in_array($protocol->value, $modeledProtocols)) { | |
| 434 | + return $protocol->value; | |
| 435 | + } | |
| 436 | + } | |
| 437 | + } | |
| 438 | + return $definition['metadata']['protocol'] ?? null; | |
| 356 | 439 | } |
| 357 | 440 | } |