|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace ExileeD\Inoreader; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use ExileeD\Inoreader\Client\ClientInterface; |
|
7
|
|
|
use ExileeD\Inoreader\Client\GuzzleClient; |
|
8
|
|
|
use ExileeD\Inoreader\Exception\InoreaderException; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
|
|
11
|
|
|
class Client |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var ClientInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
private $http_client; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** @var string|null */ |
|
21
|
|
|
private $access_token = null; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Instantiates a new Client object. |
|
25
|
|
|
* |
|
26
|
|
|
* @param ClientInterface|null $http_client_handler |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(ClientInterface $http_client_handler = null) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->http_client = $http_client_handler ?? new GuzzleClient(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Makes a POST request to the Inoreader API and returns the response |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $endpoint |
|
37
|
|
|
* @param array $params |
|
38
|
|
|
* |
|
39
|
|
|
* @throws InoreaderException |
|
40
|
|
|
* @return \stdClass |
|
41
|
|
|
*/ |
|
42
|
|
|
public function post(string $endpoint, $params = []): \stdClass |
|
43
|
|
|
{ |
|
44
|
|
|
|
|
45
|
|
|
$headers = $this->headers(); |
|
46
|
|
|
|
|
47
|
|
|
$response = $this->getHttpClient()->post($endpoint, $params, $headers); |
|
48
|
|
|
|
|
49
|
|
|
return $this->processResponse($response); |
|
50
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function headers(): array |
|
54
|
|
|
{ |
|
55
|
|
|
|
|
56
|
|
|
$headers = []; |
|
57
|
|
|
if ($this->getAccessToken() !== null) { |
|
58
|
|
|
$headers[ 'Authorization' ] = 'Bearer ' . $this->getAccessToken(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $headers; |
|
62
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getAccessToken(): ?string |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->access_token; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $access_token |
|
75
|
|
|
*/ |
|
76
|
|
|
public function setAccessToken(string $access_token): void |
|
77
|
|
|
{ |
|
78
|
|
|
$this->access_token = $access_token; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Returns the inoreader client's http client to the given http client. Client. |
|
83
|
|
|
* |
|
84
|
|
|
* @return ClientInterface |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getHttpClient(): ClientInterface |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->http_client; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Makes a request to the Inoreader API and returns the response |
|
93
|
|
|
* |
|
94
|
|
|
* @param ResponseInterface $response |
|
95
|
|
|
* |
|
96
|
|
|
* @return \stdClass The JSON response from the request |
|
97
|
|
|
* @throws InoreaderException |
|
98
|
|
|
*/ |
|
99
|
|
|
private function processResponse(ResponseInterface $response): \stdClass |
|
100
|
|
|
{ |
|
101
|
|
|
return json_decode($response->getBody()->getContents()); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Makes a GET request to the Inoreader API and returns the response |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $endpoint |
|
108
|
|
|
* @param array $params |
|
109
|
|
|
* |
|
110
|
|
|
* @throws InoreaderException |
|
111
|
|
|
* @return \stdClass |
|
112
|
|
|
*/ |
|
113
|
|
|
public function get(string $endpoint, $params = []): \stdClass |
|
114
|
|
|
{ |
|
115
|
|
|
|
|
116
|
|
|
$headers = $this->headers(); |
|
117
|
|
|
|
|
118
|
|
|
$response = $this->getHttpClient()->get($endpoint, $params, $headers); |
|
119
|
|
|
|
|
120
|
|
|
return $this->processResponse($response); |
|
121
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
} |