|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ExileeD\Inoreader\HttpClient; |
|
6
|
|
|
|
|
7
|
|
|
use ExileeD\Inoreader\Exception\InoreaderException; |
|
8
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
9
|
|
|
use GuzzleHttp\ClientInterface; |
|
10
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
12
|
|
|
|
|
13
|
|
|
class GuzzleHttpClient implements HttpClient |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var GuzzleClient |
|
17
|
|
|
*/ |
|
18
|
|
|
private $client; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param ClientInterface|null $client |
|
22
|
|
|
* |
|
23
|
|
|
* @return void |
|
24
|
|
|
*/ |
|
25
|
72 |
|
public function __construct(ClientInterface $client = null) |
|
26
|
|
|
{ |
|
27
|
72 |
|
$this->client = $client ?? self::createClient(); |
|
28
|
72 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return ClientInterface |
|
32
|
|
|
*/ |
|
33
|
9 |
|
private static function createClient() |
|
34
|
|
|
{ |
|
35
|
9 |
|
return new GuzzleClient(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @inheritdoc |
|
40
|
|
|
*/ |
|
41
|
54 |
|
public function request( |
|
42
|
|
|
$endpoint, |
|
43
|
|
|
$params = [], |
|
44
|
|
|
$body = [], |
|
45
|
|
|
$method = 'GET', |
|
46
|
|
|
array $headers = [] |
|
47
|
|
|
): ResponseInterface { |
|
48
|
|
|
$options = [ |
|
49
|
54 |
|
'headers' => $headers, |
|
50
|
54 |
|
'json' => $body, |
|
51
|
54 |
|
'query' => $params, |
|
52
|
|
|
]; |
|
53
|
|
|
try { |
|
54
|
54 |
|
return $this->getClient()->request($method, $endpoint, $options); |
|
55
|
3 |
|
} catch (GuzzleException $e) { |
|
56
|
3 |
|
throw new InoreaderException($e->getMessage(), $e->getCode(), $e); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return GuzzleClient |
|
62
|
|
|
*/ |
|
63
|
60 |
|
public function getClient(): GuzzleClient |
|
64
|
|
|
{ |
|
65
|
60 |
|
return $this->client; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param GuzzleClient $client |
|
70
|
|
|
*/ |
|
71
|
3 |
|
public function setClient(GuzzleClient $client): void |
|
72
|
|
|
{ |
|
73
|
3 |
|
$this->client = $client; |
|
74
|
3 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param ResponseInterface $response |
|
78
|
|
|
* |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
|
private static function getHeaders(ResponseInterface $response) |
|
|
|
|
|
|
82
|
|
|
{ |
|
83
|
|
|
return [ |
|
84
|
|
|
'Content-Type' => $response->getHeader('Content-Type'), |
|
85
|
|
|
'X-Reader-Zone1-Limit' => $response->getHeader('X-Reader-Zone1-Limit'), |
|
86
|
|
|
'X-Reader-Zone2-Limit' => $response->getHeader('X-Reader-Zone2-Limit'), |
|
87
|
|
|
'X-Reader-Zone1-Usage' => $response->getHeader('X-Reader-Zone1-Usage'), |
|
88
|
|
|
'X-Reader-Zone2-Usage' => $response->getHeader('X-Reader-Zone2-Usage'), |
|
89
|
|
|
'X-Reader-Limits-Reset-After' => $response->getHeader('X-Reader-Limits-Reset-After'), |
|
90
|
|
|
]; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.