GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#145)
by Yong
07:52 queued 02:47
created

Request::requestAsync()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace AlibabaCloud\Client\Request;
4
5
use Exception;
6
use ArrayAccess;
7
use GuzzleHttp\Client;
8
use GuzzleHttp\Psr7\Uri;
9
use GuzzleHttp\Middleware;
10
use AlibabaCloud\Client\SDK;
11
use GuzzleHttp\HandlerStack;
12
use AlibabaCloud\Client\Encode;
13
use AlibabaCloud\Client\AlibabaCloud;
14
use AlibabaCloud\Client\Filter\Filter;
15
use AlibabaCloud\Client\Result\Result;
16
use GuzzleHttp\Promise\PromiseInterface;
17
use AlibabaCloud\Client\Filter\ApiFilter;
18
use AlibabaCloud\Client\Log\LogFormatter;
19
use AlibabaCloud\Client\Traits\HttpTrait;
20
use GuzzleHttp\Exception\GuzzleException;
21
use AlibabaCloud\Client\Filter\HttpFilter;
22
use AlibabaCloud\Client\Traits\RegionTrait;
23
use AlibabaCloud\Client\Filter\ClientFilter;
24
use AlibabaCloud\Client\Request\Traits\AcsTrait;
25
use AlibabaCloud\Client\Traits\ArrayAccessTrait;
26
use AlibabaCloud\Client\Traits\ObjectAccessTrait;
27
use AlibabaCloud\Client\Request\Traits\RetryTrait;
28
use AlibabaCloud\Client\Exception\ClientException;
29
use AlibabaCloud\Client\Exception\ServerException;
30
use AlibabaCloud\Client\Request\Traits\MagicTrait;
31
use AlibabaCloud\Client\Request\Traits\ClientTrait;
32
use AlibabaCloud\Client\Request\Traits\DeprecatedTrait;
33
use AlibabaCloud\Client\Credentials\Providers\CredentialsProvider;
34
35
/**
36
 * Class Request
37
 *
38
 * @package   AlibabaCloud\Client\Request
39
 *
40
 * @method string stringToSign()
41
 * @method string resolveParameter()
42
 */
43
abstract class Request implements ArrayAccess
44
{
45
    use DeprecatedTrait;
46
    use HttpTrait;
47
    use RegionTrait;
48
    use MagicTrait;
49
    use ClientTrait;
50
    use AcsTrait;
51
    use ArrayAccessTrait;
52
    use ObjectAccessTrait;
53
    use RetryTrait;
54
55
    /**
56
     * Request Connect Timeout
57
     */
58
    const CONNECT_TIMEOUT = 5;
59
60
    /**
61
     * Request Timeout
62
     */
63
    const TIMEOUT = 10;
64
65
    /**
66
     * @var string HTTP Method
67
     */
68
    public $method = 'GET';
69
70
    /**
71
     * @var string
72
     */
73
    public $format = 'JSON';
74
75
    /**
76
     * @var string HTTP Scheme
77
     */
78
    protected $scheme = 'http';
79
80
    /**
81
     * @var string
82
     */
83
    public $client;
84
85
    /**
86
     * @var Uri
87
     */
88
    public $uri;
89
90
    /**
91
     * @var array The original parameters of the request.
92
     */
93
    public $data = [];
94
95
    /**
96
     * @var array
97
     */
98
    private $userAgent = [];
99
100
    /**
101
     * Request constructor.
102
     *
103
     * @param array $options
104
     *
105
     * @throws ClientException
106
     */
107 191
    public function __construct(array $options = [])
108
    {
109 191
        $this->client                     = CredentialsProvider::getDefaultName();
110 191
        $this->uri                        = new Uri();
111 191
        $this->uri                        = $this->uri->withScheme($this->scheme);
112 191
        $this->options['http_errors']     = false;
113 191
        $this->options['connect_timeout'] = self::CONNECT_TIMEOUT;
114 191
        $this->options['timeout']         = self::TIMEOUT;
115 191
        if ($options !== []) {
116 1
            $this->options($options);
117 1
        }
118
119 191
        if (strtolower(\AlibabaCloud\Client\env('DEBUG')) === 'sdk') {
120 101
            $this->options['debug'] = true;
121 101
        }
122 191
    }
123
124
    /**
125
     * @param string $name
126
     * @param string $value
127
     *
128
     * @return $this
129
     * @throws ClientException
130
     */
131 5
    public function appendUserAgent($name, $value)
132
    {
133 5
        $filter_name = Filter::name($name);
134
135 3
        if (!UserAgent::isGuarded($filter_name)) {
136 3
            $this->userAgent[$filter_name] = Filter::value($value);
137 1
        }
138
139 1
        return $this;
140
    }
141
142
    /**
143
     * @param array $userAgent
144
     *
145
     * @return $this
146
     */
147 2
    public function withUserAgent(array $userAgent)
148
    {
149 2
        $this->userAgent = UserAgent::clean($userAgent);
150
151 2
        return $this;
152
    }
153
154
    /**
155
     * Set Accept format.
156
     *
157
     * @param string $format
158
     *
159
     * @return $this
160
     * @throws ClientException
161
     */
162 14
    public function format($format)
163
    {
164 14
        $this->format = ApiFilter::format($format);
165
166 12
        return $this;
167
    }
168
169
    /**
170
     * @param $contentType
171
     *
172
     * @return $this
173
     * @throws ClientException
174
     */
175 1
    public function contentType($contentType)
176
    {
177 1
        $this->options['headers']['Content-Type'] = HttpFilter::contentType($contentType);
178
179 1
        return $this;
180
    }
181
182
    /**
183
     * @param string $accept
184
     *
185
     * @return $this
186
     * @throws ClientException
187
     */
188 2
    public function accept($accept)
189
    {
190 2
        $this->options['headers']['Accept'] = HttpFilter::accept($accept);
191
192 2
        return $this;
193
    }
194
195
    /**
196
     * Set the request body.
197
     *
198
     * @param string $body
199
     *
200
     * @return $this
201
     * @throws ClientException
202
     */
203 5
    public function body($body)
204
    {
205 5
        $this->options['body'] = HttpFilter::body($body);
206
207 3
        return $this;
208
    }
209
210
    /**
211
     * Set the json as body.
212
     *
213
     * @param array|object $content
214
     *
215
     * @return $this
216
     * @throws ClientException
217
     */
218 3
    public function jsonBody($content)
219
    {
220 3
        if (!\is_array($content) && !\is_object($content)) {
221 1
            throw new ClientException(
222 1
                'jsonBody only accepts an array or object',
223
                SDK::INVALID_ARGUMENT
224 1
            );
225
        }
226
227 2
        return $this->body(\json_encode($content));
228
    }
229
230
    /**
231
     * Set the request scheme.
232
     *
233
     * @param string $scheme
234
     *
235
     * @return $this
236
     * @throws ClientException
237
     */
238 17
    public function scheme($scheme)
239
    {
240 17
        $this->scheme = HttpFilter::scheme($scheme);
241 15
        $this->uri    = $this->uri->withScheme($scheme);
242
243 15
        return $this;
244
    }
245
246
    /**
247
     * Set the request host.
248
     *
249
     * @param string $host
250
     *
251
     * @return $this
252
     * @throws ClientException
253
     */
254 28
    public function host($host)
255
    {
256 28
        $this->uri = $this->uri->withHost(HttpFilter::host($host));
257
258 26
        return $this;
259
    }
260
261
    /**
262
     * @param string $method
263
     *
264
     * @return $this
265
     * @throws ClientException
266
     */
267 51
    public function method($method)
268
    {
269 51
        $this->method = HttpFilter::method($method);
270
271 49
        return $this;
272
    }
273
274
    /**
275
     * @param string $clientName
276
     *
277
     * @return $this
278
     * @throws ClientException
279
     */
280 67
    public function client($clientName)
281
    {
282 67
        $this->client = ClientFilter::clientName($clientName);
283
284 65
        return $this;
285
    }
286
287
    /**
288
     * @return bool
289
     * @throws ClientException
290
     */
291 1
    public function isDebug()
292
    {
293 1
        if (isset($this->options['debug'])) {
294 1
            return $this->options['debug'] === true;
295
        }
296
297 1
        if (isset($this->httpClient()->options['debug'])) {
298 1
            return $this->httpClient()->options['debug'] === true;
299
        }
300
301 1
        return false;
302
    }
303
304
    /**
305
     * @throws ClientException
306
     * @throws ServerException
307
     */
308 68
    public function resolveOption()
309
    {
310 68
        $this->options['headers']['User-Agent'] = UserAgent::toString($this->userAgent);
311
312 68
        $this->cleanQuery();
313 68
        $this->cleanFormParams();
314 68
        $this->resolveHost();
315 65
        $this->resolveParameter();
316
317 62
        if (isset($this->options['form_params'])) {
318 29
            $this->options['form_params'] = \GuzzleHttp\Psr7\parse_query(
319 29
                Encode::create($this->options['form_params'])->toString()
320 29
            );
321 29
        }
322
323 62
        $this->mergeOptionsIntoClient();
324 62
    }
325
326
    /**
327
     * @return Result
328
     * @throws ClientException
329
     * @throws ServerException
330
     */
331 66
    public function request()
332
    {
333 66
        $this->resolveOption();
334 60
        $result = new Result($this->response(), $this);
335
336 55
        if ($this->shouldServerRetry($result)) {
337 2
            return $this->request();
338
        }
339
340 55
        if (!$result->isSuccess()) {
341 21
            throw new ServerException($result);
342
        }
343
344 35
        return $result;
345
    }
346
347
    /***
348
     * @return PromiseInterface
349
     * @throws Exception
350
     */
351 2
    public function requestAsync()
352
    {
353 2
        $this->resolveOption();
354
355 2
        return self::createClient()->requestAsync(
356 2
            $this->method,
357 2
            (string)$this->uri,
358 2
            $this->options
359 2
        );
360
    }
361
362
    /**
363
     * Remove redundant Query
364
     *
365
     * @codeCoverageIgnore
366
     */
367
    private function cleanQuery()
368
    {
369
        if (isset($this->options['query']) && $this->options['query'] === []) {
370
            unset($this->options['query']);
371
        }
372
    }
373
374
    /**
375
     * Remove redundant Headers
376
     *
377
     * @codeCoverageIgnore
378
     */
379
    private function cleanFormParams()
380
    {
381
        if (isset($this->options['form_params']) && $this->options['form_params'] === []) {
382
            unset($this->options['form_params']);
383
        }
384
    }
385
386
    /**
387
     * @return Client
388
     * @throws Exception
389
     */
390 72
    public static function createClient()
391
    {
392 72
        if (AlibabaCloud::hasMock()) {
393 22
            $stack = HandlerStack::create(AlibabaCloud::getMock());
394 22
        } else {
395 50
            $stack = HandlerStack::create();
396
        }
397
398 72
        if (AlibabaCloud::isRememberHistory()) {
399 37
            $stack->push(Middleware::history(AlibabaCloud::referenceHistory()));
400 37
        }
401
402 72
        if (AlibabaCloud::getLogger()) {
403 2
            $stack->push(Middleware::log(
404 2
                AlibabaCloud::getLogger(),
405 2
                new LogFormatter(AlibabaCloud::getLogFormat())
406 2
            ));
407 2
        }
408
409 72
        self::$config['handler'] = $stack;
410
411 72
        return new Client(self::$config);
412
    }
413
414
    /**
415
     * @throws ClientException
416
     * @throws Exception
417
     */
418 60
    private function response()
419
    {
420
        try {
421 60
            return self::createClient()->request(
422 60
                $this->method,
423 60
                (string)$this->uri,
424 60
                $this->options
425 60
            );
426 6
        } catch (GuzzleException $exception) {
427 5
            if ($this->shouldClientRetry($exception)) {
428 1
                return $this->response();
429
            }
430 5
            throw new ClientException(
431 5
                $exception->getMessage(),
432 5
                SDK::SERVER_UNREACHABLE,
433
                $exception
434 5
            );
435
        }
436
    }
437
}
438