1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace OpenSubtitles; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\ClientInterface; |
8
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
9
|
|
|
use GuzzleHttp\RequestOptions; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
|
12
|
|
|
use function json_decode; |
13
|
|
|
|
14
|
|
|
final class HttpClientHandler |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var ClientInterface |
18
|
|
|
*/ |
19
|
|
|
protected ClientInterface $client; |
20
|
|
|
|
21
|
|
|
private array $options; |
22
|
|
|
|
23
|
|
|
public function __construct(ClientInterface $client) |
24
|
|
|
{ |
25
|
|
|
$this->client = $client; |
26
|
|
|
|
27
|
|
|
$this->options = [ |
28
|
|
|
RequestOptions::HEADERS => [ |
29
|
|
|
'Content-Type' => 'application/json', |
30
|
|
|
], |
31
|
|
|
]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Set api key on the header |
36
|
|
|
* |
37
|
|
|
* @param string $apiKey |
38
|
|
|
* @return $this |
39
|
|
|
*/ |
40
|
|
|
final public function setApiKey(string $apiKey): self |
41
|
|
|
{ |
42
|
|
|
$this->options[RequestOptions::HEADERS]['Api-Key'] = $apiKey; |
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set access token on the header |
48
|
|
|
* |
49
|
|
|
* @param string $accessToken |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
final public function setAccessToken(string $accessToken): self |
53
|
|
|
{ |
54
|
|
|
$this->options[RequestOptions::HEADERS]['Authorization'] = 'Bearer ' . $accessToken; |
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get request with the http client |
60
|
|
|
* |
61
|
|
|
* @param string $uri |
62
|
|
|
* @param array $options |
63
|
|
|
* @return ResponseInterface |
64
|
|
|
* @throws GuzzleException |
65
|
|
|
*/ |
66
|
|
|
final public function get(string $uri, array $options = []): ResponseInterface |
67
|
|
|
{ |
68
|
|
|
return $this->sendRequest('GET', $uri, $options); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Send request with the http client |
73
|
|
|
* |
74
|
|
|
* @param string $method |
75
|
|
|
* @param string $uri |
76
|
|
|
* @param array $options |
77
|
|
|
* @return ResponseInterface |
78
|
|
|
* @throws GuzzleException |
79
|
|
|
*/ |
80
|
|
|
private function sendRequest(string $method, string $uri, array $options): ResponseInterface |
81
|
|
|
{ |
82
|
|
|
return $this->client->request($method, $uri, $this->mergeWithDefaultOptions($options)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function mergeWithDefaultOptions(array $options = []): array |
86
|
|
|
{ |
87
|
|
|
$merged = $this->options; |
88
|
|
|
foreach ($options as $requestOptionKey => $data) { |
89
|
|
|
foreach ($data as $key => $value) { |
90
|
|
|
$merged[$requestOptionKey][$key] = $value; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $merged; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Post request with the http client |
99
|
|
|
* |
100
|
|
|
* @param string $uri |
101
|
|
|
* @param array $options |
102
|
|
|
* @return ResponseInterface |
103
|
|
|
* @throws GuzzleException |
104
|
|
|
*/ |
105
|
|
|
final public function post(string $uri, array $options = []): ResponseInterface |
106
|
|
|
{ |
107
|
|
|
return $this->sendRequest('POST', $uri, $options); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Delete request with the http client |
112
|
|
|
* |
113
|
|
|
* @param string $uri |
114
|
|
|
* @param array $options |
115
|
|
|
* @return ResponseInterface |
116
|
|
|
* @throws GuzzleException |
117
|
|
|
*/ |
118
|
|
|
final public function delete(string $uri, array $options = []): ResponseInterface |
119
|
|
|
{ |
120
|
|
|
return $this->sendRequest('DELETE', $uri, $options); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param ResponseInterface $response |
125
|
|
|
* @return mixed |
126
|
|
|
*/ |
127
|
|
|
final public function toJson(ResponseInterface $response) |
128
|
|
|
{ |
129
|
|
|
return json_decode($response->getBody()->getContents()); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|