1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Freyo\ApiGateway\Kernel; |
4
|
|
|
|
5
|
|
|
use Freyo\ApiGateway\Kernel\Http\Response; |
6
|
|
|
use Freyo\ApiGateway\Kernel\Traits\HasHttpRequests; |
7
|
|
|
use Freyo\ApiGateway\Kernel\Traits\WithFingerprint; |
8
|
|
|
use GuzzleHttp\MessageFormatter; |
9
|
|
|
use GuzzleHttp\Middleware; |
10
|
|
|
use Psr\Http\Message\RequestInterface; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
use function Freyo\ApiGateway\Kernel\Support\generate_sign; |
13
|
|
|
|
14
|
|
|
class BaseClient |
15
|
|
|
{ |
16
|
|
|
use WithFingerprint, HasHttpRequests { |
17
|
|
|
request as performRequest; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \Freyo\ApiGateway\Kernel\ServiceContainer |
22
|
|
|
*/ |
23
|
|
|
protected $app; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $baseUri; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* BaseClient constructor. |
32
|
|
|
* |
33
|
|
|
* @param \Freyo\ApiGateway\Kernel\ServiceContainer $app |
34
|
|
|
*/ |
35
|
|
|
public function __construct(ServiceContainer $app) |
36
|
|
|
{ |
37
|
|
|
$this->app = $app; |
38
|
|
|
$this->baseUri = $this->getBaseUri(); |
39
|
|
|
|
40
|
|
|
$this->setHttpClient($this->app['http_client']); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* GET request. |
45
|
|
|
* |
46
|
|
|
* @param string $url |
47
|
|
|
* @param array $query |
48
|
|
|
* |
49
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\Freyo\ApiGateway\Kernel\Support\Collection|array|object|string |
50
|
|
|
* |
51
|
|
|
* @throws \Freyo\ApiGateway\Kernel\Exceptions\InvalidConfigException |
52
|
|
|
*/ |
53
|
|
|
public function httpGet($url, array $query = []) |
54
|
|
|
{ |
55
|
|
|
return $this->request($url, 'GET', ['query' => $query]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* POST request. |
60
|
|
|
* |
61
|
|
|
* @param string $url |
62
|
|
|
* @param array $data |
63
|
|
|
* |
64
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\Freyo\ApiGateway\Kernel\Support\Collection|array|object|string |
65
|
|
|
* |
66
|
|
|
* @throws \Freyo\ApiGateway\Kernel\Exceptions\InvalidConfigException |
67
|
|
|
*/ |
68
|
|
|
public function httpPost($url, array $data = []) |
69
|
|
|
{ |
70
|
|
|
return $this->request($url, 'POST', ['form_params' => $data]); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* PUT request. |
75
|
|
|
* |
76
|
|
|
* @param string $url |
77
|
|
|
* @param array $data |
78
|
|
|
* |
79
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\Freyo\ApiGateway\Kernel\Support\Collection|array|object|string |
80
|
|
|
* |
81
|
|
|
* @throws \Freyo\ApiGateway\Kernel\Exceptions\InvalidConfigException |
82
|
|
|
*/ |
83
|
|
|
public function httpPut($url, array $data = []) |
84
|
|
|
{ |
85
|
|
|
return $this->request($url, 'PUT', ['form_params' => $data]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* JSON request. |
90
|
|
|
* |
91
|
|
|
* @param string $url |
92
|
|
|
* @param string|array $data |
93
|
|
|
* @param array $query |
94
|
|
|
* |
95
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\Freyo\ApiGateway\Kernel\Support\Collection|array|object|string |
96
|
|
|
* |
97
|
|
|
* @throws \Freyo\ApiGateway\Kernel\Exceptions\InvalidConfigException |
98
|
|
|
*/ |
99
|
|
|
public function httpPostJson($url, array $data = [], array $query = []) |
100
|
|
|
{ |
101
|
|
|
return $this->request($url, 'POST', ['query' => $query, 'json' => $data]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* JSON request. |
106
|
|
|
* |
107
|
|
|
* @param string $url |
108
|
|
|
* @param string|array $data |
109
|
|
|
* @param array $query |
110
|
|
|
* |
111
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\Freyo\ApiGateway\Kernel\Support\Collection|array|object|string |
112
|
|
|
* |
113
|
|
|
* @throws \Freyo\ApiGateway\Kernel\Exceptions\InvalidConfigException |
114
|
|
|
*/ |
115
|
|
|
public function httpPutJson($url, array $data = [], array $query = []) |
116
|
|
|
{ |
117
|
|
|
return $this->request($url, 'PUT', ['query' => $query, 'json' => $data]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Upload file. |
122
|
|
|
* |
123
|
|
|
* @param string $url |
124
|
|
|
* @param array $files |
125
|
|
|
* @param array $form |
126
|
|
|
* @param array $query |
127
|
|
|
* |
128
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\Freyo\ApiGateway\Kernel\Support\Collection|array|object|string |
129
|
|
|
* |
130
|
|
|
* @throws \Freyo\ApiGateway\Kernel\Exceptions\InvalidConfigException |
131
|
|
|
*/ |
132
|
|
|
public function httpUpload($url, array $files = [], array $form = [], array $query = []) |
133
|
|
|
{ |
134
|
|
|
$multipart = []; |
135
|
|
|
|
136
|
|
|
foreach ($files as $name => $path) { |
137
|
|
|
$multipart[] = [ |
138
|
|
|
'name' => $name, |
139
|
|
|
'contents' => fopen($path, 'r'), |
140
|
|
|
]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
foreach ($form as $name => $contents) { |
144
|
|
|
$multipart[] = compact('name', 'contents'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $this->request($url, 'POST', ['query' => $query, 'multipart' => $multipart, 'connect_timeout' => 30, 'timeout' => 30, 'read_timeout' => 30]); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Make a request and return raw response. |
152
|
|
|
* |
153
|
|
|
* @param string $url |
154
|
|
|
* @param string $method |
155
|
|
|
* @param array $options |
156
|
|
|
* |
157
|
|
|
* @return ResponseInterface |
158
|
|
|
* |
159
|
|
|
* @throws \Freyo\ApiGateway\Kernel\Exceptions\InvalidConfigException |
160
|
|
|
*/ |
161
|
|
|
public function requestRaw($url, $method = 'GET', array $options = []) |
162
|
|
|
{ |
163
|
|
|
return Response::buildFromPsrResponse($this->request($url, $method, $options, true)); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Make a API request. |
168
|
|
|
* |
169
|
|
|
* @param string $url |
170
|
|
|
* @param string $method |
171
|
|
|
* @param array $options |
172
|
|
|
* @param bool $returnRaw |
173
|
|
|
* |
174
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\Freyo\ApiGateway\Kernel\Support\Collection|array|object|string |
175
|
|
|
* |
176
|
|
|
* @throws \Freyo\ApiGateway\Kernel\Exceptions\InvalidConfigException |
177
|
|
|
*/ |
178
|
|
|
public function request($url, $method = 'GET', array $options = [], $returnRaw = false) |
179
|
|
|
{ |
180
|
|
|
if (empty($this->middlewares)) { |
181
|
|
|
$this->registerHttpMiddlewares(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
$headers = $options['headers'] ?? []; |
185
|
|
|
|
186
|
|
|
if ($this->app->withFingerprint()) { |
187
|
|
|
$headers['Fingerprint'] = $this->fingerprint( |
188
|
|
|
$method, |
189
|
|
|
$this->baseUri . $url, |
190
|
|
|
($options['json'] ?? []) + ($options['form_params'] ?? []) + ($options['query'] ?? []) |
191
|
|
|
); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if ($this->app->needAuth()) { |
195
|
|
|
$headers['Authorization'] = $this->authorization($headers); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$options = array_merge($options, ['headers' => $headers]); |
199
|
|
|
|
200
|
|
|
$response = $this->performRequest($url, $method, $options); |
|
|
|
|
201
|
|
|
|
202
|
|
|
return $returnRaw ? $response : $this->castResponseToType($response, $this->app->config->get('response_type')); |
|
|
|
|
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Register Guzzle middlewares. |
207
|
|
|
*/ |
208
|
|
|
protected function registerHttpMiddlewares() |
209
|
|
|
{ |
210
|
|
|
// log |
211
|
|
|
$this->pushMiddleware($this->logMiddleware(), 'log'); |
212
|
|
|
// jaeger |
213
|
|
|
if ($this->app['config']->has('jaeger')) { |
214
|
|
|
$this->pushMiddleware($this->jaegerMiddleware(), 'jaeger'); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Log the request. |
220
|
|
|
* |
221
|
|
|
* @return \Closure |
222
|
|
|
*/ |
223
|
|
|
protected function logMiddleware() |
224
|
|
|
{ |
225
|
|
|
$formatter = new MessageFormatter($this->app['config']->get('http.log_template', MessageFormatter::DEBUG)); |
226
|
|
|
|
227
|
|
|
return Middleware::log($this->app['logger'], $formatter); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Jaeger. |
232
|
|
|
* |
233
|
|
|
* @return \Closure |
234
|
|
|
*/ |
235
|
|
|
protected function jaegerMiddleware() |
236
|
|
|
{ |
237
|
|
|
return Middleware::mapRequest(function (RequestInterface $request) { |
238
|
|
|
|
239
|
|
|
if ($headers = $this->app['config']->get('jaeger.headers', [])) { |
240
|
|
|
|
241
|
|
|
$headers = is_callable($headers) ? call_user_func($headers) : $headers; |
242
|
|
|
|
243
|
|
|
foreach ($headers as $name => $value) { |
244
|
|
|
$request = $request->withHeader($name, $value); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return $request; |
249
|
|
|
}); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Extra request params. |
254
|
|
|
* |
255
|
|
|
* @return array |
256
|
|
|
*/ |
257
|
|
|
protected function prepends() |
258
|
|
|
{ |
259
|
|
|
return []; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @return string |
264
|
|
|
*/ |
265
|
|
|
protected function getBaseUri() |
266
|
|
|
{ |
267
|
|
|
return $this->app['config']->get('http.base_uri'); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Wrapping an API endpoint. |
272
|
|
|
* |
273
|
|
|
* @param string $endpoint |
274
|
|
|
* @param string $prefix |
275
|
|
|
* |
276
|
|
|
* @return string |
277
|
|
|
*/ |
278
|
|
|
protected function wrap($endpoint, $prefix = '') |
279
|
|
|
{ |
280
|
|
|
return implode('/', [$prefix, $endpoint]); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @param array $headers |
285
|
|
|
* |
286
|
|
|
* @return string |
287
|
|
|
*/ |
288
|
|
|
protected function authorization(array $headers) |
289
|
|
|
{ |
290
|
|
|
$headers = array_merge( |
291
|
|
|
['Date' => gmdate("D, d M Y H:i:s T"), 'Source' => $this->app->getSource()], |
292
|
|
|
$headers |
293
|
|
|
); |
294
|
|
|
|
295
|
|
|
$signature = generate_sign($headers, $this->app->getSecretKey()); |
296
|
|
|
|
297
|
|
|
$authorization = sprintf( |
298
|
|
|
'hmac id="%s", algorithm="hmac-sha1", headers="%s", signature="%s"', |
299
|
|
|
$this->app->getSecretId(), |
300
|
|
|
implode(' ', array_map('strtolower', array_keys($headers))), |
301
|
|
|
$signature |
302
|
|
|
); |
303
|
|
|
|
304
|
|
|
return $authorization; |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.