vendor/aws/aws-sdk-php/src/Api/Service.php line 82

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