1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gemz\HttpClient; |
4
|
|
|
|
5
|
|
|
use Gemz\HttpClient\Contracts\Options; |
6
|
|
|
use Symfony\Component\HttpClient\HttpClient; |
7
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface; |
8
|
|
|
use Symfony\Contracts\HttpClient\ResponseInterface; |
9
|
|
|
use Symfony\Contracts\HttpClient\ResponseStreamInterface; |
10
|
|
|
|
11
|
|
|
class Client |
12
|
|
|
{ |
13
|
|
|
use Options; |
14
|
|
|
|
15
|
|
|
/** @var HttpClientInterface */ |
16
|
|
|
protected $client; |
17
|
|
|
|
18
|
|
|
/** @var Config|null */ |
19
|
|
|
protected $config; |
20
|
|
|
|
21
|
|
|
/** @var array<mixed> */ |
22
|
|
|
protected $options = []; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
protected $bodyFormat = 'json'; |
26
|
|
|
|
27
|
|
|
/** @var bool */ |
28
|
|
|
protected $throwErrors = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param Config|null $config |
32
|
|
|
* |
33
|
|
|
* @return Client |
34
|
|
|
*/ |
35
|
|
|
public static function create(Config $config = null) |
36
|
|
|
{ |
37
|
|
|
return new self($config); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param Config|null $config |
42
|
|
|
*/ |
43
|
|
|
public function __construct(Config $config = null) |
44
|
|
|
{ |
45
|
|
|
$this->config = $config ?: new Config(); |
46
|
|
|
|
47
|
|
|
$this->transferBodyFormatFromConfig(); |
48
|
|
|
$this->transferThrowErrorsFromConfig(); |
49
|
|
|
|
50
|
|
|
$this->client = $this->buildClient(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
|
|
protected function transferThrowErrorsFromConfig(): self |
57
|
|
|
{ |
58
|
|
|
$this->throwErrors = $this->config->shouldThrowErrors(); |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
protected function transferBodyFormatFromConfig(): self |
67
|
|
|
{ |
68
|
|
|
return $this->bodyFormat( |
69
|
|
|
$this->config->getBodyFormat() |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $endpoint |
75
|
|
|
* |
76
|
|
|
* @return Response|mixed |
77
|
|
|
* |
78
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
79
|
|
|
*/ |
80
|
|
|
public function get(string $endpoint) |
81
|
|
|
{ |
82
|
|
|
return $this->request('GET', $endpoint); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $endpoint |
87
|
|
|
* |
88
|
|
|
* @return Response|mixed |
89
|
|
|
* |
90
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
91
|
|
|
*/ |
92
|
|
|
public function head(string $endpoint) |
93
|
|
|
{ |
94
|
|
|
return $this->request('HEAD', $endpoint); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $endpoint |
99
|
|
|
* |
100
|
|
|
* @return Response|mixed |
101
|
|
|
* |
102
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
103
|
|
|
*/ |
104
|
|
|
public function post(string $endpoint) |
105
|
|
|
{ |
106
|
|
|
return $this->request('POST', $endpoint); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $endpoint |
111
|
|
|
* |
112
|
|
|
* @return Response|mixed |
113
|
|
|
* |
114
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
115
|
|
|
*/ |
116
|
|
|
public function put(string $endpoint) |
117
|
|
|
{ |
118
|
|
|
return $this->request('PUT', $endpoint); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $endpoint |
123
|
|
|
* |
124
|
|
|
* @return Response|mixed |
125
|
|
|
* |
126
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
127
|
|
|
*/ |
128
|
|
|
public function patch(string $endpoint) |
129
|
|
|
{ |
130
|
|
|
return $this->request('PATCH', $endpoint); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param string $endpoint |
135
|
|
|
* |
136
|
|
|
* @return Response |
137
|
|
|
* |
138
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
139
|
|
|
*/ |
140
|
|
|
public function delete(string $endpoint) |
141
|
|
|
{ |
142
|
|
|
return $this->request('DELETE', $endpoint); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $method |
147
|
|
|
* @param string $endpoint |
148
|
|
|
* |
149
|
|
|
* @return mixed|Response |
150
|
|
|
* |
151
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface |
152
|
|
|
*/ |
153
|
|
|
protected function request(string $method, string $endpoint) |
154
|
|
|
{ |
155
|
|
|
$this->resolvePayload(); |
156
|
|
|
|
157
|
|
|
return Response::createFromResponse( |
158
|
|
|
$this->client->request($method, $endpoint, $this->options), |
159
|
|
|
$this->throwErrors |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param ResponseInterface|ResponseInterface[]|iterable $responses |
165
|
|
|
* |
166
|
|
|
* @return ResponseStreamInterface |
167
|
|
|
*/ |
168
|
|
|
public function stream($responses): ResponseStreamInterface |
169
|
|
|
{ |
170
|
|
|
return $this->client->stream( |
171
|
|
|
collect($responses)->transform(function ($item) { |
172
|
|
|
return $item->response(); |
173
|
|
|
}) |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return HttpClientInterface |
179
|
|
|
*/ |
180
|
|
|
protected function buildClient(): HttpClientInterface |
181
|
|
|
{ |
182
|
|
|
return HttpClient::create($this->config->toArray()); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return array<String|Array> |
187
|
|
|
*/ |
188
|
|
|
public function config(): array |
189
|
|
|
{ |
190
|
|
|
return $this->config->toArray(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return array<mixed> |
195
|
|
|
*/ |
196
|
|
|
public function options(): array |
197
|
|
|
{ |
198
|
|
|
return $this->options; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return HttpClientInterface |
203
|
|
|
*/ |
204
|
|
|
public function client(): HttpClientInterface |
205
|
|
|
{ |
206
|
|
|
return $this->client; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|