Service.php 12 KB

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