From 45934ae744c0107cef1a5e4f324975e2b2a83705 Mon Sep 17 00:00:00 2001 From: brjun Date: Mon, 28 Mar 2022 14:30:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Src/Request/Request.php | 220 ++++++++++++++++++++-------------------- 1 file changed, 110 insertions(+), 110 deletions(-) diff --git a/Src/Request/Request.php b/Src/Request/Request.php index e8df6a5..9e4c034 100644 --- a/Src/Request/Request.php +++ b/Src/Request/Request.php @@ -21,114 +21,114 @@ class Request { - protected $apiUrl = ''; - /** - * @var Client - */ - protected $httpClient; - protected $defaultHttpClientConfig = [ - 'headers' => [], - 'verify' => false, - 'curl.options' => [ - CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1, - ], - 'connect_timeout' => 3, - 'timeout' => 10 - ]; - protected static $middlewareMap = []; - protected $cache; - - /** - * @var Response - */ - protected $response; - /** - * @var string - */ - protected $responseContent; - - public function __construct(CacheInterface $cache = null) - { - $header = []; - - if (defined('W7_CLOUD_SDK_LOCAL') && !empty(W7_CLOUD_SDK_LOCAL)) { - $this->apiUrl = W7_CLOUD_SDK_LOCAL_URL; - } - - if (defined('W7_CLOUD_SDK_CUSTOM_AGENT') && !empty(W7_CLOUD_SDK_CUSTOM_AGENT)) { - $header['User-Agent'] = W7_CLOUD_SDK_CUSTOM_AGENT; - } - - $this->defaultHttpClientConfig['headers'] = array_merge([], $this->defaultHttpClientConfig['headers'], $header); - $this->cache = $cache; - } - - public function debug() - { - $this->defaultHttpClientConfig['debug'] = true; - return $this; - } - - public function setConnectTimeout($seconds) { - $this->defaultHttpClientConfig['connect_timeout'] = $seconds; - return $this; - } - - public function setRequestTimeout($seconds) { - $this->defaultHttpClientConfig['timeout'] = $seconds; - return $this; - } - - public static function setEnvCustom($agent) - { - !defined('W7_CLOUD_SDK_CUSTOM_AGENT') && define('W7_CLOUD_SDK_CUSTOM_AGENT', $agent); - } - - public static function setEnvLocal($url = 'http://127.0.0.1/', $authCode = '') - { - !defined('W7_CLOUD_SDK_LOCAL') && define('W7_CLOUD_SDK_LOCAL', 1); - !defined('W7_CLOUD_SDK_LOCAL_URL') && define('W7_CLOUD_SDK_LOCAL_URL', $url); - !defined('W7_CLOUD_SDK_AUTHKEY') && define('W7_CLOUD_SDK_AUTHKEY', $authCode); - } - - public function getClient() - { - if (!$this->httpClient) { - if (empty($this->defaultHttpClientConfig['handler'])) { - $this->defaultHttpClientConfig['handler'] = HandlerStack::create(); - } - - $this->defaultHttpClientConfig['handler']->push(function ($handler) { - return new RetryMiddleware($handler, $this->defaultHttpClientConfig); - }); - foreach (static::$middlewareMap as $middleware) { - $this->defaultHttpClientConfig['handler']->push($middleware); - } - - $this->httpClient = new Client($this->defaultHttpClientConfig); - } - - return $this->httpClient; - } - - public function withHeader($key, $value) - { - $this->defaultHttpClientConfig['headers'][$key] = $value; - return $this; - } - - public static function registerMiddleware(Closure $middleware) - { - static::$middlewareMap[] = $middleware; - } - - public function getResponse() - { - return $this->response; - } - - public function getResponseContent() - { - return $this->responseContent; - } + protected $apiUrl = ''; + /** + * @var Client + */ + protected $httpClient; + protected $defaultHttpClientConfig = [ + 'headers' => [], + 'verify' => false, + 'curl.options' => [ + CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1, + ], + 'connect_timeout' => 3, + 'timeout' => 10 + ]; + protected static $middlewareMap = []; + protected $cache; + + /** + * @var Response + */ + protected $response; + /** + * @var string + */ + protected $responseContent; + + public function __construct(CacheInterface $cache = null) + { + $header = []; + + if (defined('W7_CLOUD_SDK_LOCAL') && !empty(W7_CLOUD_SDK_LOCAL)) { + $this->apiUrl = W7_CLOUD_SDK_LOCAL_URL; + } + + if (defined('W7_CLOUD_SDK_CUSTOM_AGENT') && !empty(W7_CLOUD_SDK_CUSTOM_AGENT)) { + $header['User-Agent'] = W7_CLOUD_SDK_CUSTOM_AGENT; + } + + $this->defaultHttpClientConfig['headers'] = array_merge([], $this->defaultHttpClientConfig['headers'], $header); + $this->cache = $cache; + } + + public function debug() + { + $this->defaultHttpClientConfig['debug'] = true; + return $this; + } + + public function setConnectTimeout($seconds) { + $this->defaultHttpClientConfig['connect_timeout'] = $seconds; + return $this; + } + + public function setRequestTimeout($seconds) { + $this->defaultHttpClientConfig['timeout'] = $seconds; + return $this; + } + + public static function setEnvCustom($agent) + { + !defined('W7_CLOUD_SDK_CUSTOM_AGENT') && define('W7_CLOUD_SDK_CUSTOM_AGENT', $agent); + } + + public static function setEnvLocal($url = 'http://127.0.0.1/', $authCode = '') + { + !defined('W7_CLOUD_SDK_LOCAL') && define('W7_CLOUD_SDK_LOCAL', 1); + !defined('W7_CLOUD_SDK_LOCAL_URL') && define('W7_CLOUD_SDK_LOCAL_URL', $url); + !defined('W7_CLOUD_SDK_AUTHKEY') && define('W7_CLOUD_SDK_AUTHKEY', $authCode); + } + + public function getClient() + { + if (!$this->httpClient) { + if (empty($this->defaultHttpClientConfig['handler'])) { + $this->defaultHttpClientConfig['handler'] = HandlerStack::create(); + } + + $this->defaultHttpClientConfig['handler']->push(function ($handler) { + return new RetryMiddleware($handler, $this->defaultHttpClientConfig); + }); + foreach (static::$middlewareMap as $middleware) { + $this->defaultHttpClientConfig['handler']->push($middleware); + } + + $this->httpClient = new Client($this->defaultHttpClientConfig); + } + + return $this->httpClient; + } + + public function withHeader($key, $value) + { + $this->defaultHttpClientConfig['headers'][$key] = $value; + return $this; + } + + public static function registerMiddleware(Closure $middleware) + { + static::$middlewareMap[] = $middleware; + } + + public function getResponse() + { + return $this->response; + } + + public function getResponseContent() + { + return $this->responseContent; + } }