| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\Api; |
| 4 |
|
| 5 |
/** |
| 6 |
* Represents a web service API model. |
| 7 |
*/ |
| 8 |
class Service extends AbstractModel |
| 9 |
{ |
| 10 |
/** @var callable */ |
| 11 |
private $apiProvider; |
| 12 |
/** @var string */ |
| 13 |
private $serviceName; |
| 14 |
/** @var string */ |
| 15 |
private $apiVersion; |
| 16 |
/** @var array */ |
| 17 |
private $clientContextParams = []; |
| 18 |
/** @var Operation[] */ |
| 19 |
private $operations = []; |
| 20 |
/** @var array */ |
| 21 |
private $paginators = null; |
| 22 |
/** @var array */ |
| 23 |
private $waiters = null; |
| 24 |
/** @var boolean */ |
| 25 |
private $modifiedModel = \false; |
| 26 |
/** |
| 27 |
* @param array $definition |
| 28 |
* @param callable $provider |
| 29 |
* |
| 30 |
* @internal param array $definition Service description |
| 31 |
*/ |
| 32 |
public function __construct(array $definition, callable $provider) |
| 33 |
{ |
| 34 |
static $defaults = ['operations' => [], 'shapes' => [], 'metadata' => [], 'clientContextParams' => []], $defaultMeta = ['apiVersion' => null, 'serviceFullName' => null, 'serviceId' => null, 'endpointPrefix' => null, 'signingName' => null, 'signatureVersion' => null, 'protocol' => null, 'uid' => null]; |
| 35 |
$definition += $defaults; |
| 36 |
$definition['metadata'] += $defaultMeta; |
| 37 |
$this->definition = $definition; |
| 38 |
$this->apiProvider = $provider; |
| 39 |
parent::__construct($definition, new ShapeMap($definition['shapes'])); |
| 40 |
if (isset($definition['metadata']['serviceIdentifier'])) { |
| 41 |
$this->serviceName = $this->getServiceName(); |
| 42 |
} else { |
| 43 |
$this->serviceName = $this->getEndpointPrefix(); |
| 44 |
} |
| 45 |
$this->apiVersion = $this->getApiVersion(); |
| 46 |
if (isset($definition['clientContextParams'])) { |
| 47 |
$this->clientContextParams = $definition['clientContextParams']; |
| 48 |
} |
| 49 |
} |
| 50 |
/** |
| 51 |
* Creates a request serializer for the provided API object. |
| 52 |
* |
| 53 |
* @param Service $api API that contains a protocol. |
| 54 |
* @param string $endpoint Endpoint to send requests to. |
| 55 |
* |
| 56 |
* @return callable |
| 57 |
* @throws \UnexpectedValueException |
| 58 |
*/ |
| 59 |
public static function createSerializer(Service $api, $endpoint) |
| 60 |
{ |
| 61 |
static $mapping = ['json' => Serializer\JsonRpcSerializer::class, 'query' => Serializer\QuerySerializer::class, 'rest-json' => Serializer\RestJsonSerializer::class, 'rest-xml' => Serializer\RestXmlSerializer::class]; |
| 62 |
$proto = $api->getProtocol(); |
| 63 |
if (isset($mapping[$proto])) { |
| 64 |
return new $mapping[$proto]($api, $endpoint); |
| 65 |
} |
| 66 |
if ($proto == 'ec2') { |
| 67 |
return new Serializer\QuerySerializer($api, $endpoint, new Serializer\Ec2ParamBuilder()); |
| 68 |
} |
| 69 |
throw new \UnexpectedValueException('Unknown protocol: ' . $api->getProtocol()); |
| 70 |
} |
| 71 |
/** |
| 72 |
* Creates an error parser for the given protocol. |
| 73 |
* |
| 74 |
* Redundant method signature to preserve backwards compatibility. |
| 75 |
* |
| 76 |
* @param string $protocol Protocol to parse (e.g., query, json, etc.) |
| 77 |
* |
| 78 |
* @return callable |
| 79 |
* @throws \UnexpectedValueException |
| 80 |
*/ |
| 81 |
public static function createErrorParser($protocol, Service $api = null) |
| 82 |
{ |
| 83 |
static $mapping = ['json' => ErrorParser\JsonRpcErrorParser::class, 'query' => ErrorParser\XmlErrorParser::class, 'rest-json' => ErrorParser\RestJsonErrorParser::class, 'rest-xml' => ErrorParser\XmlErrorParser::class, 'ec2' => ErrorParser\XmlErrorParser::class]; |
| 84 |
if (isset($mapping[$protocol])) { |
| 85 |
return new $mapping[$protocol]($api); |
| 86 |
} |
| 87 |
throw new \UnexpectedValueException("Unknown protocol: {$protocol}"); |
| 88 |
} |
| 89 |
/** |
| 90 |
* Applies the listeners needed to parse client models. |
| 91 |
* |
| 92 |
* @param Service $api API to create a parser for |
| 93 |
* @return callable |
| 94 |
* @throws \UnexpectedValueException |
| 95 |
*/ |
| 96 |
public static function createParser(Service $api) |
| 97 |
{ |
| 98 |
static $mapping = ['json' => Parser\JsonRpcParser::class, 'query' => Parser\QueryParser::class, 'rest-json' => Parser\RestJsonParser::class, 'rest-xml' => Parser\RestXmlParser::class]; |
| 99 |
$proto = $api->getProtocol(); |
| 100 |
if (isset($mapping[$proto])) { |
| 101 |
return new $mapping[$proto]($api); |
| 102 |
} |
| 103 |
if ($proto == 'ec2') { |
| 104 |
return new Parser\QueryParser($api, null, \false); |
| 105 |
} |
| 106 |
throw new \UnexpectedValueException('Unknown protocol: ' . $api->getProtocol()); |
| 107 |
} |
| 108 |
/** |
| 109 |
* Get the full name of the service |
| 110 |
* |
| 111 |
* @return string |
| 112 |
*/ |
| 113 |
public function getServiceFullName() |
| 114 |
{ |
| 115 |
return $this->definition['metadata']['serviceFullName']; |
| 116 |
} |
| 117 |
/** |
| 118 |
* Get the service id |
| 119 |
* |
| 120 |
* @return string |
| 121 |
*/ |
| 122 |
public function getServiceId() |
| 123 |
{ |
| 124 |
return $this->definition['metadata']['serviceId']; |
| 125 |
} |
| 126 |
/** |
| 127 |
* Get the API version of the service |
| 128 |
* |
| 129 |
* @return string |
| 130 |
*/ |
| 131 |
public function getApiVersion() |
| 132 |
{ |
| 133 |
return $this->definition['metadata']['apiVersion']; |
| 134 |
} |
| 135 |
/** |
| 136 |
* Get the API version of the service |
| 137 |
* |
| 138 |
* @return string |
| 139 |
*/ |
| 140 |
public function getEndpointPrefix() |
| 141 |
{ |
| 142 |
return $this->definition['metadata']['endpointPrefix']; |
| 143 |
} |
| 144 |
/** |
| 145 |
* Get the signing name used by the service. |
| 146 |
* |
| 147 |
* @return string |
| 148 |
*/ |
| 149 |
public function getSigningName() |
| 150 |
{ |
| 151 |
return $this->definition['metadata']['signingName'] ?: $this->definition['metadata']['endpointPrefix']; |
| 152 |
} |
| 153 |
/** |
| 154 |
* Get the service name. |
| 155 |
* |
| 156 |
* @return string |
| 157 |
*/ |
| 158 |
public function getServiceName() |
| 159 |
{ |
| 160 |
return $this->definition['metadata']['serviceIdentifier']; |
| 161 |
} |
| 162 |
/** |
| 163 |
* Get the default signature version of the service. |
| 164 |
* |
| 165 |
* Note: this method assumes "v4" when not specified in the model. |
| 166 |
* |
| 167 |
* @return string |
| 168 |
*/ |
| 169 |
public function getSignatureVersion() |
| 170 |
{ |
| 171 |
return $this->definition['metadata']['signatureVersion'] ?: 'v4'; |
| 172 |
} |
| 173 |
/** |
| 174 |
* Get the protocol used by the service. |
| 175 |
* |
| 176 |
* @return string |
| 177 |
*/ |
| 178 |
public function getProtocol() |
| 179 |
{ |
| 180 |
return $this->definition['metadata']['protocol']; |
| 181 |
} |
| 182 |
/** |
| 183 |
* Get the uid string used by the service |
| 184 |
* |
| 185 |
* @return string |
| 186 |
*/ |
| 187 |
public function getUid() |
| 188 |
{ |
| 189 |
return $this->definition['metadata']['uid']; |
| 190 |
} |
| 191 |
/** |
| 192 |
* Check if the description has a specific operation by name. |
| 193 |
* |
| 194 |
* @param string $name Operation to check by name |
| 195 |
* |
| 196 |
* @return bool |
| 197 |
*/ |
| 198 |
public function hasOperation($name) |
| 199 |
{ |
| 200 |
return isset($this['operations'][$name]); |
| 201 |
} |
| 202 |
/** |
| 203 |
* Get an operation by name. |
| 204 |
* |
| 205 |
* @param string $name Operation to retrieve by name |
| 206 |
* |
| 207 |
* @return Operation |
| 208 |
* @throws \InvalidArgumentException If the operation is not found |
| 209 |
*/ |
| 210 |
public function getOperation($name) |
| 211 |
{ |
| 212 |
if (!isset($this->operations[$name])) { |
| 213 |
if (!isset($this->definition['operations'][$name])) { |
| 214 |
throw new \InvalidArgumentException("Unknown operation: {$name}"); |
| 215 |
} |
| 216 |
$this->operations[$name] = new Operation($this->definition['operations'][$name], $this->shapeMap); |
| 217 |
} elseif ($this->modifiedModel) { |
| 218 |
$this->operations[$name] = new Operation($this->definition['operations'][$name], $this->shapeMap); |
| 219 |
} |
| 220 |
return $this->operations[$name]; |
| 221 |
} |
| 222 |
/** |
| 223 |
* Get all of the operations of the description. |
| 224 |
* |
| 225 |
* @return Operation[] |
| 226 |
*/ |
| 227 |
public function getOperations() |
| 228 |
{ |
| 229 |
$result = []; |
| 230 |
foreach ($this->definition['operations'] as $name => $definition) { |
| 231 |
$result[$name] = $this->getOperation($name); |
| 232 |
} |
| 233 |
return $result; |
| 234 |
} |
| 235 |
/** |
| 236 |
* Get all of the error shapes of the service |
| 237 |
* |
| 238 |
* @return array |
| 239 |
*/ |
| 240 |
public function getErrorShapes() |
| 241 |
{ |
| 242 |
$result = []; |
| 243 |
foreach ($this->definition['shapes'] as $name => $definition) { |
| 244 |
if (!empty($definition['exception'])) { |
| 245 |
$definition['name'] = $name; |
| 246 |
$result[] = new StructureShape($definition, $this->getShapeMap()); |
| 247 |
} |
| 248 |
} |
| 249 |
return $result; |
| 250 |
} |
| 251 |
/** |
| 252 |
* Get all of the service metadata or a specific metadata key value. |
| 253 |
* |
| 254 |
* @param string|null $key Key to retrieve or null to retrieve all metadata |
| 255 |
* |
| 256 |
* @return mixed Returns the result or null if the key is not found |
| 257 |
*/ |
| 258 |
public function getMetadata($key = null) |
| 259 |
{ |
| 260 |
if (!$key) { |
| 261 |
return $this['metadata']; |
| 262 |
} |
| 263 |
if (isset($this->definition['metadata'][$key])) { |
| 264 |
return $this->definition['metadata'][$key]; |
| 265 |
} |
| 266 |
return null; |
| 267 |
} |
| 268 |
/** |
| 269 |
* Gets an associative array of available paginator configurations where |
| 270 |
* the key is the name of the paginator, and the value is the paginator |
| 271 |
* configuration. |
| 272 |
* |
| 273 |
* @return array |
| 274 |
* @unstable The configuration format of paginators may change in the future |
| 275 |
*/ |
| 276 |
public function getPaginators() |
| 277 |
{ |
| 278 |
if (!isset($this->paginators)) { |
| 279 |
$res = \call_user_func($this->apiProvider, 'paginator', $this->serviceName, $this->apiVersion); |
| 280 |
$this->paginators = isset($res['pagination']) ? $res['pagination'] : []; |
| 281 |
} |
| 282 |
return $this->paginators; |
| 283 |
} |
| 284 |
/** |
| 285 |
* Determines if the service has a paginator by name. |
| 286 |
* |
| 287 |
* @param string $name Name of the paginator. |
| 288 |
* |
| 289 |
* @return bool |
| 290 |
*/ |
| 291 |
public function hasPaginator($name) |
| 292 |
{ |
| 293 |
return isset($this->getPaginators()[$name]); |
| 294 |
} |
| 295 |
/** |
| 296 |
* Retrieve a paginator by name. |
| 297 |
* |
| 298 |
* @param string $name Paginator to retrieve by name. This argument is |
| 299 |
* typically the operation name. |
| 300 |
* @return array |
| 301 |
* @throws \UnexpectedValueException if the paginator does not exist. |
| 302 |
* @unstable The configuration format of paginators may change in the future |
| 303 |
*/ |
| 304 |
public function getPaginatorConfig($name) |
| 305 |
{ |
| 306 |
static $defaults = ['input_token' => null, 'output_token' => null, 'limit_key' => null, 'result_key' => null, 'more_results' => null]; |
| 307 |
if ($this->hasPaginator($name)) { |
| 308 |
return $this->paginators[$name] + $defaults; |
| 309 |
} |
| 310 |
throw new \UnexpectedValueException("There is no {$name} " . "paginator defined for the {$this->serviceName} service."); |
| 311 |
} |
| 312 |
/** |
| 313 |
* Gets an associative array of available waiter configurations where the |
| 314 |
* key is the name of the waiter, and the value is the waiter |
| 315 |
* configuration. |
| 316 |
* |
| 317 |
* @return array |
| 318 |
*/ |
| 319 |
public function getWaiters() |
| 320 |
{ |
| 321 |
if (!isset($this->waiters)) { |
| 322 |
$res = \call_user_func($this->apiProvider, 'waiter', $this->serviceName, $this->apiVersion); |
| 323 |
$this->waiters = isset($res['waiters']) ? $res['waiters'] : []; |
| 324 |
} |
| 325 |
return $this->waiters; |
| 326 |
} |
| 327 |
/** |
| 328 |
* Determines if the service has a waiter by name. |
| 329 |
* |
| 330 |
* @param string $name Name of the waiter. |
| 331 |
* |
| 332 |
* @return bool |
| 333 |
*/ |
| 334 |
public function hasWaiter($name) |
| 335 |
{ |
| 336 |
return isset($this->getWaiters()[$name]); |
| 337 |
} |
| 338 |
/** |
| 339 |
* Get a waiter configuration by name. |
| 340 |
* |
| 341 |
* @param string $name Name of the waiter by name. |
| 342 |
* |
| 343 |
* @return array |
| 344 |
* @throws \UnexpectedValueException if the waiter does not exist. |
| 345 |
*/ |
| 346 |
public function getWaiterConfig($name) |
| 347 |
{ |
| 348 |
// Error if the waiter is not defined |
| 349 |
if ($this->hasWaiter($name)) { |
| 350 |
return $this->waiters[$name]; |
| 351 |
} |
| 352 |
throw new \UnexpectedValueException("There is no {$name} waiter " . "defined for the {$this->serviceName} service."); |
| 353 |
} |
| 354 |
/** |
| 355 |
* Get the shape map used by the API. |
| 356 |
* |
| 357 |
* @return ShapeMap |
| 358 |
*/ |
| 359 |
public function getShapeMap() |
| 360 |
{ |
| 361 |
return $this->shapeMap; |
| 362 |
} |
| 363 |
/** |
| 364 |
* Get all the context params of the description. |
| 365 |
* |
| 366 |
* @return array |
| 367 |
*/ |
| 368 |
public function getClientContextParams() |
| 369 |
{ |
| 370 |
return $this->clientContextParams; |
| 371 |
} |
| 372 |
/** |
| 373 |
* Get the service's api provider. |
| 374 |
* |
| 375 |
* @return callable |
| 376 |
*/ |
| 377 |
public function getProvider() |
| 378 |
{ |
| 379 |
return $this->apiProvider; |
| 380 |
} |
| 381 |
/** |
| 382 |
* Get the service's definition. |
| 383 |
* |
| 384 |
* @return callable |
| 385 |
*/ |
| 386 |
public function getDefinition() |
| 387 |
{ |
| 388 |
return $this->definition; |
| 389 |
} |
| 390 |
/** |
| 391 |
* Sets the service's api definition. |
| 392 |
* Intended for internal use only. |
| 393 |
* |
| 394 |
* @return void |
| 395 |
* |
| 396 |
* @internal |
| 397 |
*/ |
| 398 |
public function setDefinition($definition) |
| 399 |
{ |
| 400 |
$this->definition = $definition; |
| 401 |
$this->shapeMap = new ShapeMap($definition['shapes']); |
| 402 |
$this->modifiedModel = \true; |
| 403 |
} |
| 404 |
/** |
| 405 |
* Denotes whether or not a service's definition has |
| 406 |
* been modified. Intended for internal use only. |
| 407 |
* |
| 408 |
* @return bool |
| 409 |
* |
| 410 |
* @internal |
| 411 |
*/ |
| 412 |
public function isModifiedModel() |
| 413 |
{ |
| 414 |
return $this->modifiedModel; |
| 415 |
} |
| 416 |
} |
| 417 |
|