CHANGELOG.md000064400000001625146412204310006356 0ustar00# CHANGELOG ## 3.0.0 - 2020-02-14 - Rename package name. ## 2.0.3 - 2020-02-14 - Improved Exception. ## 2.0.2 - 2019-09-11 - Supported `String`. ## 2.0.1 - 2019-08-15 - Supported `IteratorAggregate`. ## 2.0.0 - 2019-08-14 - Design `Request` as a standard `PsrRequest`. ## 1.0.10 - 2019-08-12 - Added `__toString` for `Response`. ## 1.0.9 - 2019-08-01 - Updated `Middleware`. ## 1.0.8 - 2019-07-29 - Supported `TransferStats`. ## 1.0.7 - 2019-07-27 - Improved request. ## 1.0.6 - 2019-07-23 - Trim key for parameter. ## 1.0.5 - 2019-07-23 - Supported default protocol. ## 1.0.4 - 2019-07-22 - Added `toArray()`. ## 1.0.3 - 2019-05-02 - Improved `Request`. ## 1.0.2 - 2019-05-02 - Added getHeader/getHeaders. ## 1.0.1 - 2019-04-02 - Improved design. ## 1.0.0 - 2019-05-02 - Initial release of the AlibabaCloud Tea Version 1.0.0 on Packagist See for more information. LICENSE.md000064400000001115146412204310006143 0ustar00Copyright (c) 2009-present, Alibaba Cloud All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. README.md000064400000000611146412204310006016 0ustar00 ## Installation ``` composer require alibabacloud/tea --optimize-autoloader ``` > Some users may not be able to install due to network problems, you can try to switch the Composer mirror. ## Changelog Detailed changes for each release are documented in the [release notes](CHANGELOG.md). ## License [Apache-2.0](LICENSE.md) Copyright (c) 2009-present, Alibaba Cloud All rights reserved. composer.json000064400000004273146412204310007271 0ustar00{ "name": "alibabacloud/tea", "homepage": "https://www.alibabacloud.com/", "description": "Client of Tea for PHP", "keywords": [ "tea", "client", "alibabacloud", "cloud" ], "type": "library", "license": "Apache-2.0", "support": { "source": "https://github.com/aliyun/tea-php", "issues": "https://github.com/aliyun/tea-php/issues" }, "authors": [ { "name": "Alibaba Cloud SDK", "email": "sdk-team@alibabacloud.com", "homepage": "http://www.alibabacloud.com" } ], "require": { "php": ">=5.5", "ext-curl": "*", "ext-json": "*", "ext-libxml": "*", "ext-mbstring": "*", "ext-openssl": "*", "ext-simplexml": "*", "ext-xmlwriter": "*", "guzzlehttp/guzzle": "^6.3", "songshenzong/support": "^2.0", "danielstjules/stringy": "^3.1", "mtdowling/jmespath.php": "^2.4", "adbario/php-dot-notation": "^2.2" }, "require-dev": { "symfony/dotenv": "^3.4", "phpunit/phpunit": "^4.8.35|^5.4.3", "symfony/var-dumper": "^3.4" }, "suggest": { "ext-sockets": "To use client-side monitoring" }, "autoload": { "psr-4": { "AlibabaCloud\\Tea\\": "src" } }, "autoload-dev": { "psr-4": { "AlibabaCloud\\Tea\\Tests\\": "tests" } }, "config": { "sort-packages": true, "preferred-install": "dist", "optimize-autoloader": true }, "prefer-stable": true, "minimum-stability": "dev", "scripts": { "cs": "phpcs --standard=PSR2 -n ./", "cbf": "phpcbf --standard=PSR2 -n ./", "fixer": "php-cs-fixer fix ./", "test": [ "@clearCache", "phpunit --colors=always" ], "unit": [ "@clearCache", "phpunit --testsuite=Unit --colors=always" ], "feature": [ "@clearCache", "phpunit --testsuite=Feature --colors=always" ], "clearCache": "rm -rf cache/*", "coverage": "open cache/coverage/index.html" } } src/Exception/TeaError.php000064400000001335146412204310011524 0ustar00errorInfo = $errorInfo; } /** * @return array */ public function getErrorInfo() { return $this->errorInfo; } } src/Exception/TeaRetryError.php000064400000000774146412204310012560 0ustar00lastRequest = $lastRequest; } public function getLastRequest() { return $this->lastRequest; } } src/Model.php000064400000003142146412204310007101 0ustar00 $v) { $this->{$k} = $v; } } } public function getName($name = null) { if (null === $name) { return $this->_name; } return isset($this->_name[$name]) ? $this->_name[$name] : $name; } public function toMap() { $map = get_object_vars($this); foreach ($map as $k => $m) { if (0 === strpos($k, "_")) { unset($map[$k]); } } $res = []; foreach ($map as $k => $v) { $name = isset($this->_name[$k]) ? $this->_name[$k] : $k; $res[$name] = $v; } return $res; } public function validate() { $vars = get_object_vars($this); foreach ($vars as $k => $v) { if (isset($this->_required[$k]) && $this->_required[$k] && empty($v)) { throw new \InvalidArgumentException("$k is required."); } } } /** * @param array $map * @param Model $model * * @return mixed */ public static function toModel($map, $model) { $names = $model->getName(); $names = array_flip($names); foreach ($map as $key => $value) { $name = isset($names[$key]) ? $names[$key] : $key; $model->{$name} = $value; } return $model; } } src/Parameter.php000064400000002114146412204310007757 0ustar00toArray()); } /** * @return array */ public function getRealParameters() { $array = []; $obj = new ReflectionObject($this); $properties = $obj->getProperties(); foreach ($properties as $property) { $docComment = $property->getDocComment(); $key = (string)Stringy::create($docComment)->between('@real ', "\n"); $key = trim($key); $value = $property->getValue($this); $array[$key] = $value; } return $array; } /** * @return array */ public function toArray() { return $this->getRealParameters(); } } src/Request.php000064400000004742146412204310007500 0ustar00method = $method; } /** * These fields are compatible if you define other fields. * Mainly for compatibility situations where the code generator cannot generate set properties. * * @return PsrRequest */ public function getPsrRequest() { $this->assertQuery($this->query); $request = clone $this; $uri = $request->getUri(); if ($this->query) { $uri = $uri->withQuery(http_build_query($this->query)); } if ($this->port) { $uri = $uri->withPort($this->port); } if ($this->protocol) { $uri = $uri->withScheme($this->protocol); } if ($this->pathname) { $uri = $uri->withPath($this->pathname); } if (isset($this->headers['host'])) { $uri = $uri->withHost($this->headers['host']); } $request = $request->withUri($uri); $request = $request->withMethod($this->method); if ($this->body !== '' && $this->body !== null) { if ($this->body instanceof StreamInterface) { $request = $request->withBody($this->body); } else { $request = $request->withBody(\GuzzleHttp\Psr7\stream_for($this->body)); } } if ($this->headers) { foreach ($this->headers as $key => $value) { $request = $request->withHeader($key, $value); } } return $request; } /** * @param array $query */ private function assertQuery($query) { if (!is_array($query)) { throw new InvalidArgumentException('Query must be array.'); } } } src/Response.php000064400000017125146412204310007645 0ustar00getStatusCode(), $response->getHeaders(), $response->getBody(), $response->getProtocolVersion(), $response->getReasonPhrase() ); $this->headers = $response->getHeaders(); $this->body = $response->getBody(); $this->statusCode = $response->getStatusCode(); if ($this->body->isSeekable()) { $this->body->seek(0); } if (Strings::isJson((string)$this->getBody())) { $this->dot = new Dot($this->toArray()); } else { $this->dot = new Dot(); } } /** * @return string */ public function __toString() { return (string)$this->getBody(); } /** * @return array */ public function toArray() { return \GuzzleHttp\json_decode((string)$this->getBody(), true); } /** * @param string $expression * * @return mixed|null */ public function search($expression) { return JmesPath::search($expression, $this->dot->all()); } /** * @param array|int|string $keys * @param mixed $value */ public function add($keys, $value = null) { return $this->dot->add($keys, $value); } /** * @return array */ public function all() { return $this->dot->all(); } /** * @param array|int|string|null $keys */ public function clear($keys = null) { return $this->dot->clear($keys); } /** * @param array|int|string $keys */ public function delete($keys) { return $this->dot->delete($keys); } /** * @param string $delimiter * @param array|null $items * @param string $prepend * * @return array */ public function flatten($delimiter = '.', $items = null, $prepend = '') { return $this->dot->flatten($delimiter, $items, $prepend); } /** * @param int|string|null $key * @param mixed $default * * @return mixed */ public function get($key = null, $default = null) { return $this->dot->get($key, $default); } /** * @param array|int|string $keys * * @return bool */ public function has($keys) { return $this->dot->has($keys); } /** * @param array|int|string|null $keys * * @return bool */ public function isEmpty($keys = null) { return $this->dot->isEmpty($keys); } /** * @param array|string|self $key * @param array|self $value */ public function merge($key, $value = []) { return $this->dot->merge($key, $value); } /** * @param array|string|self $key * @param array|self $value */ public function mergeRecursive($key, $value = []) { return $this->dot->mergeRecursive($key, $value); } /** * @param array|string|self $key * @param array|self $value */ public function mergeRecursiveDistinct($key, $value = []) { return $this->dot->mergeRecursiveDistinct($key, $value); } /** * @param int|string|null $key * @param mixed $default * * @return mixed */ public function pull($key = null, $default = null) { return $this->dot->pull($key, $default); } /** * @param int|string|null $key * @param mixed $value * * @return mixed */ public function push($key = null, $value = null) { return $this->dot->push($key, $value); } /** * Replace all values or values within the given key * with an array or Dot object * * @param array|string|self $key * @param array|self $value */ public function replace($key, $value = []) { return $this->dot->replace($key, $value); } /** * Set a given key / value pair or pairs * * @param array|int|string $keys * @param mixed $value */ public function set($keys, $value = null) { return $this->dot->set($keys, $value); } /** * Replace all items with a given array * * @param mixed $items */ public function setArray($items) { return $this->dot->setArray($items); } /** * Replace all items with a given array as a reference * * @param array $items */ public function setReference(array &$items) { return $this->dot->setReference($items); } /** * Return the value of a given key or all the values as JSON * * @param mixed $key * @param int $options * * @return string */ public function toJson($key = null, $options = 0) { return $this->dot->toJson($key, $options); } /** * Retrieve an external iterator */ public function getIterator() { return $this->dot->getIterator(); } /** * Whether a offset exists * * @param $offset * * @return bool */ public function offsetExists($offset) { return $this->dot->offsetExists($offset); } /** * Offset to retrieve * * @param $offset * * @return mixed */ public function offsetGet($offset) { return $this->dot->offsetGet($offset); } /** * Offset to set * * @param $offset * @param $value */ public function offsetSet($offset, $value) { $this->dot->offsetSet($offset, $value); } /** * Offset to unset * * @param $offset */ public function offsetUnset($offset) { $this->dot->offsetUnset($offset); } /** * Count elements of an object * * @param null $key * * @return int */ public function count($key = null) { return $this->dot->count($key); } /** * @param string $name * * @return mixed|null */ public function __get($name) { $data = $this->dot->all(); if (!isset($data[$name])) { return null; } return \json_decode(\json_encode($data))->$name; } /** * @param string $name * @param mixed $value */ public function __set($name, $value) { $this->dot->set($name, $value); } /** * @param string $name * * @return bool */ public function __isset($name) { return $this->dot->has($name); } /** * @param $offset * * @return void */ public function __unset($offset) { $this->dot->delete($offset); } } src/Tea.php000064400000014022146412204310006551 0ustar00getPsrRequest(); } $config['http_errors'] = false; $res = self::client()->send( $request, $config ); return new Response($res); } /** * @param RequestInterface $request * @param array $config * * @return PromiseInterface */ public static function sendAsync(RequestInterface $request, array $config = []) { if (method_exists($request, 'getPsrRequest')) { $request = $request->getPsrRequest(); } $config['http_errors'] = false; return self::client()->sendAsync( $request, $config ); } /** * @param array $config * * @return Client */ public static function client(array $config = []) { if (isset(self::$config['handler'])) { $stack = self::$config['handler']; } else { $stack = HandlerStack::create(); } $stack->push(Middleware::mapResponse(static function (ResponseInterface $response) { return new Response($response); })); self::$config['handler'] = $stack; if (!isset(self::$config['on_stats'])) { self::$config['on_stats'] = function (TransferStats $stats) { Response::$info = $stats->getHandlerStats(); }; } $new_config = Arrays::merge([self::$config, $config]); return new Client($new_config); } /** * @param string $method * @param string|UriInterface $uri * @param array $options * * @return ResponseInterface */ public static function request($method, $uri, $options = []) { return self::client()->request($method, $uri, $options); } /** * @param string $method * @param string $uri * @param array $options * * @return string */ public static function string($method, $uri, $options = []) { return (string)self::client()->request($method, $uri, $options) ->getBody(); } /** * @param string $method * @param string|UriInterface $uri * @param array $options * * @return PromiseInterface */ public static function requestAsync($method, $uri, $options = []) { return self::client()->requestAsync($method, $uri, $options); } /** * @param string|UriInterface $uri * @param array $options * * @return mixed|null * @throws GuzzleException */ public static function getHeaders($uri, $options = []) { return self::request('HEAD', $uri, $options)->getHeaders(); } /** * @param string|UriInterface $uri * @param string $key * @param mixed|null $default * * @return mixed|null * @throws GuzzleException */ public static function getHeader($uri, $key, $default = null) { $headers = self::getHeaders($uri); return isset($headers[$key][0]) ? $headers[$key][0] : $default; } /** * @param array $runtime * @param int $retryTimes * @param float $now * * @return bool */ public static function allowRetry(array $runtime, $retryTimes, $now) { unset($now); if (empty($runtime) || !isset($runtime['maxAttempts'])) { return false; } $maxAttempts = $runtime['maxAttempts']; $retry = empty($maxAttempts) ? 0 : intval($maxAttempts); return $retry >= $retryTimes; } /** * @param array $runtime * @param int $retryTimes * * @return int */ public static function getBackoffTime(array $runtime, $retryTimes) { $backOffTime = 0; $policy = isset($runtime["policy"]) ? $runtime["policy"] : ""; if (empty($policy) || $policy == "no") { return $backOffTime; } $period = isset($runtime["period"]) ? $runtime["period"] : ""; if (null !== $period && "" !== $period) { $backOffTime = intval($period); if ($backOffTime <= 0) { return $retryTimes; } } return $backOffTime; } public static function sleep($time) { sleep($time); } public static function isRetryable($retry, $retryTimes) { $max = isset($retry["maxAttempts"]) ? intval($retry["maxAttempts"]) : 3; return $retryTimes <= $max; } /** * @param mixed|Model[] ...$item * * @return mixed */ public static function merge(...$item) { $tmp = []; $n = 0; foreach ($item as $i) { if (is_object($i)) { if ($i instanceof Model) { $i = $i->toMap(); } else { $i = json_decode(json_encode($i), true); } } if (!is_array($i)) { throw new \InvalidArgumentException($i); } $tmp[$n++] = $i; } return call_user_func_array('array_merge', $tmp); } }