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