Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Api/Service.php

raw symbols SCIP
1 <?php
2 namespace Aws\Api;
3
4 /**
5 * Represents a web service API model.
6 */
7 class Service extends AbstractModel
8 {
9 /** @var callable */
10 private $apiProvider;
11
12 /** @var string */
13 private $serviceName;
14
15 /** @var string */
16 private $apiVersion;
17
18 /** @var array */
19 private $clientContextParams = [];
20
21 /** @var Operation[] */
22 private $operations = [];
23
24 /** @var array */
25 private $paginators = null;
26
27 /** @var array */
28 private $waiters = null;
29
30 /** @var boolean */
31 private $modifiedModel = false;
32
33 /** @var string */
34 private $protocol;
35
36 /**
37 * @param array $definition
38 * @param callable $provider
39 *
40 * @internal param array $definition Service description
41 */
42 public function __construct(array $definition, callable $provider)
43 {
44 static $defaults = [
45 'operations' => [],
46 'shapes' => [],
47 'metadata' => [],
48 'clientContextParams' => []
49 ], $defaultMeta = [
50 'apiVersion' => null,
51 'serviceFullName' => null,
52 'serviceId' => null,
53 'endpointPrefix' => null,
54 'signingName' => null,
55 'signatureVersion' => null,
56 'protocol' => null,
57 'uid' => null
58 ];
59
60 $definition += $defaults;
61 $definition['metadata'] += $defaultMeta;
62 $this->definition = $definition;
63 $this->apiProvider = $provider;
64 parent::__construct($definition, new ShapeMap($definition['shapes']));
65
66 if (isset($definition['metadata']['serviceIdentifier'])) {
67 $this->serviceName = $this->getServiceName();
68 } else {
69 $this->serviceName = $this->getEndpointPrefix();
70 }
71 $this->apiVersion = $this->getApiVersion();
72 if (isset($definition['clientContextParams'])) {
73 $this->clientContextParams = $definition['clientContextParams'];
74 }
75
76 $this->protocol = $this->selectProtocol($definition);
77 }
78
79 /**
80 * Creates a request serializer for the provided API object.
81 *
82 * @param Service $api API that contains a protocol.
83 * @param string $endpoint Endpoint to send requests to.
84 *
85 * @return callable
86 * @throws \UnexpectedValueException
87 */
88 public static function createSerializer(Service $api, $endpoint)
89 {
90 static $mapping = [
91 'json' => Serializer\JsonRpcSerializer::class,
92 'query' => Serializer\QuerySerializer::class,
93 'rest-json' => Serializer\RestJsonSerializer::class,
94 'rest-xml' => Serializer\RestXmlSerializer::class,
95 'smithy-rpc-v2-cbor' => Serializer\RpcV2CborSerializer::class
96 ];
97
98 $proto = $api->getProtocol();
99
100 if (isset($mapping[$proto])) {
101 return new $mapping[$proto]($api, $endpoint);
102 }
103
104 if ($proto == 'ec2') {
105 return new Serializer\QuerySerializer($api, $endpoint, new Serializer\Ec2ParamBuilder());
106 }
107
108 throw new \UnexpectedValueException(
109 'Unknown protocol: ' . $api->getProtocol()
110 );
111 }
112
113 /**
114 * Creates an error parser for the given protocol.
115 *
116 * Redundant method signature to preserve backwards compatibility.
117 *
118 * @param string $protocol Protocol to parse (e.g., query, json, etc.)
119 *
120 * @return callable
121 * @throws \UnexpectedValueException
122 */
123 public static function createErrorParser($protocol, ?Service $api = null)
124 {
125 static $mapping = [
126 'json' => ErrorParser\JsonRpcErrorParser::class,
127 'query' => ErrorParser\XmlErrorParser::class,
128 'rest-json' => ErrorParser\RestJsonErrorParser::class,
129 'rest-xml' => ErrorParser\XmlErrorParser::class,
130 'ec2' => ErrorParser\XmlErrorParser::class,
131 'smithy-rpc-v2-cbor' => ErrorParser\RpcV2CborErrorParser::class
132 ];
133
134 if (isset($mapping[$protocol])) {
135 return new $mapping[$protocol]($api);
136 }
137
138 throw new \UnexpectedValueException("Unknown protocol: $protocol");
139 }
140
141 /**
142 * Applies the listeners needed to parse client models.
143 *
144 * @param Service $api API to create a parser for
145 * @return callable
146 * @throws \UnexpectedValueException
147 */
148 public static function createParser(Service $api)
149 {
150 static $mapping = [
151 'json' => Parser\JsonRpcParser::class,
152 'query' => Parser\QueryParser::class,
153 'rest-json' => Parser\RestJsonParser::class,
154 'rest-xml' => Parser\RestXmlParser::class,
155 'smithy-rpc-v2-cbor' => Parser\RpcV2CborParser::class
156 ];
157
158 $proto = $api->getProtocol();
159 if (isset($mapping[$proto])) {
160 return new $mapping[$proto]($api);
161 }
162
163 if ($proto == 'ec2') {
164 return new Parser\QueryParser($api, null, false);
165 }
166
167 throw new \UnexpectedValueException(
168 'Unknown protocol: ' . $api->getProtocol()
169 );
170 }
171
172 /**
173 * Get the full name of the service
174 *
175 * @return string
176 */
177 public function getServiceFullName()
178 {
179 return $this->definition['metadata']['serviceFullName'];
180 }
181
182 /**
183 * Get the service id
184 *
185 * @return string
186 */
187 public function getServiceId()
188 {
189 return $this->definition['metadata']['serviceId'];
190 }
191
192 /**
193 * Get the API version of the service
194 *
195 * @return string
196 */
197 public function getApiVersion()
198 {
199 return $this->definition['metadata']['apiVersion'];
200 }
201
202 /**
203 * Get the API version of the service
204 *
205 * @return string
206 */
207 public function getEndpointPrefix()
208 {
209 return $this->definition['metadata']['endpointPrefix'];
210 }
211
212 /**
213 * Get the signing name used by the service.
214 *
215 * @return string
216 */
217 public function getSigningName()
218 {
219 return $this->definition['metadata']['signingName']
220 ?: $this->definition['metadata']['endpointPrefix'];
221 }
222
223 /**
224 * Get the service name.
225 *
226 * @return string
227 */
228 public function getServiceName()
229 {
230 return $this->definition['metadata']['serviceIdentifier'] ?? null;
231 }
232
233 /**
234 * Get the default signature version of the service.
235 *
236 * Note: this method assumes "v4" when not specified in the model.
237 *
238 * @return string
239 */
240 public function getSignatureVersion()
241 {
242 return $this->definition['metadata']['signatureVersion'] ?: 'v4';
243 }
244
245 /**
246 * Get the protocol used by the service.
247 *
248 * @return string
249 */
250 public function getProtocol()
251 {
252 return $this->protocol;
253 }
254
255 /**
256 * Get the uid string used by the service
257 *
258 * @return string
259 */
260 public function getUid()
261 {
262 return $this->definition['metadata']['uid'];
263 }
264
265 /**
266 * Check if the description has a specific operation by name.
267 *
268 * @param string $name Operation to check by name
269 *
270 * @return bool
271 */
272 public function hasOperation($name)
273 {
274 return isset($this['operations'][$name]);
275 }
276
277 /**
278 * Get an operation by name.
279 *
280 * @param string $name Operation to retrieve by name
281 *
282 * @return Operation
283 * @throws \InvalidArgumentException If the operation is not found
284 */
285 public function getOperation($name)
286 {
287 if (!isset($this->operations[$name])) {
288 if (!isset($this->definition['operations'][$name])) {
289 throw new \InvalidArgumentException("Unknown operation: $name");
290 }
291 $this->operations[$name] = new Operation(
292 $this->definition['operations'][$name],
293 $this->shapeMap
294 );
295 } elseif ($this->modifiedModel) {
296 $this->operations[$name] = new Operation(
297 $this->definition['operations'][$name],
298 $this->shapeMap
299 );
300 }
301
302 return $this->operations[$name];
303 }
304
305 /**
306 * Get all of the operations of the description.
307 *
308 * @return Operation[]
309 */
310 public function getOperations()
311 {
312 $result = [];
313 foreach ($this->definition['operations'] as $name => $definition) {
314 $result[$name] = $this->getOperation($name);
315 }
316
317 return $result;
318 }
319
320 /**
321 * Get all of the error shapes of the service
322 *
323 * @return array
324 */
325 public function getErrorShapes()
326 {
327 $result = [];
328 foreach ($this->definition['shapes'] as $name => $definition) {
329 if (!empty($definition['exception'])) {
330 $definition['name'] = $name;
331 $result[] = new StructureShape($definition, $this->getShapeMap());
332 }
333 }
334
335 return $result;
336 }
337
338 /**
339 * Get all of the service metadata or a specific metadata key value.
340 *
341 * @param string|null $key Key to retrieve or null to retrieve all metadata
342 *
343 * @return mixed Returns the result or null if the key is not found
344 */
345 public function getMetadata($key = null)
346 {
347 if (!$key) {
348 return $this['metadata'];
349 }
350
351 if (isset($this->definition['metadata'][$key])) {
352 return $this->definition['metadata'][$key];
353 }
354
355 return null;
356 }
357
358 /**
359 * Gets an associative array of available paginator configurations where
360 * the key is the name of the paginator, and the value is the paginator
361 * configuration.
362 *
363 * @return array
364 * @unstable The configuration format of paginators may change in the future
365 */
366 public function getPaginators()
367 {
368 if (!isset($this->paginators)) {
369 $res = call_user_func(
370 $this->apiProvider,
371 'paginator',
372 $this->serviceName,
373 $this->apiVersion
374 );
375 $this->paginators = isset($res['pagination'])
376 ? $res['pagination']
377 : [];
378 }
379
380 return $this->paginators;
381 }
382
383 /**
384 * Determines if the service has a paginator by name.
385 *
386 * @param string $name Name of the paginator.
387 *
388 * @return bool
389 */
390 public function hasPaginator($name)
391 {
392 return isset($this->getPaginators()[$name]);
393 }
394
395 /**
396 * Retrieve a paginator by name.
397 *
398 * @param string $name Paginator to retrieve by name. This argument is
399 * typically the operation name.
400 * @return array
401 * @throws \UnexpectedValueException if the paginator does not exist.
402 * @unstable The configuration format of paginators may change in the future
403 */
404 public function getPaginatorConfig($name)
405 {
406 static $defaults = [
407 'input_token' => null,
408 'output_token' => null,
409 'limit_key' => null,
410 'result_key' => null,
411 'more_results' => null,
412 ];
413
414 if ($this->hasPaginator($name)) {
415 return $this->paginators[$name] + $defaults;
416 }
417
418 throw new \UnexpectedValueException("There is no {$name} "
419 . "paginator defined for the {$this->serviceName} service.");
420 }
421
422 /**
423 * Gets an associative array of available waiter configurations where the
424 * key is the name of the waiter, and the value is the waiter
425 * configuration.
426 *
427 * @return array
428 */
429 public function getWaiters()
430 {
431 if (!isset($this->waiters)) {
432 $res = call_user_func(
433 $this->apiProvider,
434 'waiter',
435 $this->serviceName,
436 $this->apiVersion
437 );
438 $this->waiters = isset($res['waiters'])
439 ? $res['waiters']
440 : [];
441 }
442
443 return $this->waiters;
444 }
445
446 /**
447 * Determines if the service has a waiter by name.
448 *
449 * @param string $name Name of the waiter.
450 *
451 * @return bool
452 */
453 public function hasWaiter($name)
454 {
455 return isset($this->getWaiters()[$name]);
456 }
457
458 /**
459 * Get a waiter configuration by name.
460 *
461 * @param string $name Name of the waiter by name.
462 *
463 * @return array
464 * @throws \UnexpectedValueException if the waiter does not exist.
465 */
466 public function getWaiterConfig($name)
467 {
468 // Error if the waiter is not defined
469 if ($this->hasWaiter($name)) {
470 return $this->waiters[$name];
471 }
472
473 throw new \UnexpectedValueException("There is no {$name} waiter "
474 . "defined for the {$this->serviceName} service.");
475 }
476
477 /**
478 * Get the shape map used by the API.
479 *
480 * @return ShapeMap
481 */
482 public function getShapeMap()
483 {
484 return $this->shapeMap;
485 }
486
487 /**
488 * Get all the context params of the description.
489 *
490 * @return array
491 */
492 public function getClientContextParams()
493 {
494 return $this->clientContextParams;
495 }
496
497 /**
498 * Get the service's api provider.
499 *
500 * @return callable
501 */
502 public function getProvider()
503 {
504 return $this->apiProvider;
505 }
506
507 /**
508 * Get the service's definition.
509 *
510 * @return callable
511 */
512 public function getDefinition()
513 {
514 return $this->definition;
515 }
516
517 /**
518 * Sets the service's api definition.
519 * Intended for internal use only.
520 *
521 * @return void
522 *
523 * @internal
524 */
525 public function setDefinition($definition)
526 {
527 $this->definition = $definition;
528 $this->shapeMap = new ShapeMap($definition['shapes']);
529 $this->modifiedModel = true;
530 }
531
532 /**
533 * Denotes whether or not a service's definition has
534 * been modified. Intended for internal use only.
535 *
536 * @return bool
537 *
538 * @internal
539 */
540 public function isModifiedModel()
541 {
542 return $this->modifiedModel;
543 }
544
545 /**
546 * Accepts a list of protocols derived from the service model.
547 * Returns the highest priority compatible auth scheme if the `protocols` trait is present.
548 * Otherwise, returns the value of the `protocol` field, if set, or null.
549 *
550 * @param array $definition
551 *
552 * @return string|null
553 */
554 private function selectProtocol(array $definition): string | null
555 {
556 $modeledProtocols = $definition['metadata']['protocols'] ?? null;
557 if (!empty($modeledProtocols)) {
558 foreach(SupportedProtocols::cases() as $protocol) {
559 if (in_array($protocol->value, $modeledProtocols)) {
560 return $protocol->value;
561 }
562 }
563 }
564
565 return $definition['metadata']['protocol'] ?? null;
566 }
567 }