1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Freyo\MtaH5\Kernel; |
4
|
|
|
|
5
|
|
|
use Freyo\MtaH5\Kernel\Http\Response; |
6
|
|
|
use Freyo\MtaH5\Kernel\Traits\HasHttpRequests; |
7
|
|
|
use GuzzleHttp\MessageFormatter; |
8
|
|
|
use GuzzleHttp\Middleware; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use function Freyo\MtaH5\Kernel\Support\generate_sign; |
11
|
|
|
|
12
|
|
|
class BaseClient |
13
|
|
|
{ |
14
|
|
|
use HasHttpRequests { |
15
|
|
|
request as performRequest; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var \Freyo\MtaH5\Kernel\ServiceContainer |
20
|
|
|
*/ |
21
|
|
|
protected $app; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $baseUri; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* BaseClient constructor. |
30
|
|
|
* |
31
|
|
|
* @param \Freyo\MtaH5\Kernel\ServiceContainer $app |
32
|
|
|
*/ |
33
|
|
|
public function __construct(ServiceContainer $app) |
34
|
|
|
{ |
35
|
|
|
$this->app = $app; |
36
|
|
|
$this->baseUri = $this->getBaseUri(); |
37
|
|
|
|
38
|
|
|
$this->setHttpClient($this->app['http_client']); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* GET request. |
43
|
|
|
* |
44
|
|
|
* @param string $url |
45
|
|
|
* @param array $query |
46
|
|
|
* |
47
|
|
|
* @throws \Freyo\MtaH5\Kernel\Exceptions\InvalidConfigException |
48
|
|
|
* |
49
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\Freyo\MtaH5\Kernel\Support\Collection|array|object|string |
50
|
|
|
*/ |
51
|
|
|
public function httpGet($url, array $query = []) |
52
|
|
|
{ |
53
|
|
|
$query['app_id'] = $this->app->getAppId(); |
54
|
|
|
$query['sign'] = generate_sign($query, $this->app->getSecretKey()); |
55
|
|
|
|
56
|
|
|
return $this->request($url, 'GET', ['query' => $query]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* POST request. |
61
|
|
|
* |
62
|
|
|
* @param string $url |
63
|
|
|
* @param array $data |
64
|
|
|
* |
65
|
|
|
* @throws \Freyo\MtaH5\Kernel\Exceptions\InvalidConfigException |
66
|
|
|
* |
67
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\Freyo\MtaH5\Kernel\Support\Collection|array|object|string |
68
|
|
|
*/ |
69
|
|
|
public function httpPost($url, array $data = []) |
70
|
|
|
{ |
71
|
|
|
return $this->request($url, 'POST', ['form_params' => $data]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* JSON request. |
76
|
|
|
* |
77
|
|
|
* @param string $url |
78
|
|
|
* @param string|array $data |
79
|
|
|
* @param array $query |
80
|
|
|
* |
81
|
|
|
* @throws \Freyo\MtaH5\Kernel\Exceptions\InvalidConfigException |
82
|
|
|
* |
83
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\Freyo\MtaH5\Kernel\Support\Collection|array|object|string |
84
|
|
|
*/ |
85
|
|
|
public function httpPostJson($url, array $data = [], array $query = []) |
86
|
|
|
{ |
87
|
|
|
return $this->request($url, 'POST', ['query' => $query, 'json' => $data]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Upload file. |
92
|
|
|
* |
93
|
|
|
* @param string $url |
94
|
|
|
* @param array $files |
95
|
|
|
* @param array $form |
96
|
|
|
* @param array $query |
97
|
|
|
* |
98
|
|
|
* @throws \Freyo\MtaH5\Kernel\Exceptions\InvalidConfigException |
99
|
|
|
* |
100
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\Freyo\MtaH5\Kernel\Support\Collection|array|object|string |
101
|
|
|
*/ |
102
|
|
|
public function httpUpload($url, array $files = [], array $form = [], array $query = []) |
103
|
|
|
{ |
104
|
|
|
$multipart = []; |
105
|
|
|
|
106
|
|
|
foreach ($files as $name => $path) { |
107
|
|
|
$multipart[] = [ |
108
|
|
|
'name' => $name, |
109
|
|
|
'contents' => fopen($path, 'r'), |
110
|
|
|
]; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
foreach ($form as $name => $contents) { |
114
|
|
|
$multipart[] = compact('name', 'contents'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->request($url, 'POST', ['query' => $query, 'multipart' => $multipart, 'connect_timeout' => 30, 'timeout' => 30, 'read_timeout' => 30]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Make a request and return raw response. |
122
|
|
|
* |
123
|
|
|
* @param string $url |
124
|
|
|
* @param string $method |
125
|
|
|
* @param array $options |
126
|
|
|
* |
127
|
|
|
* @throws \Freyo\MtaH5\Kernel\Exceptions\InvalidConfigException |
128
|
|
|
* |
129
|
|
|
* @return ResponseInterface |
130
|
|
|
*/ |
131
|
|
|
public function requestRaw($url, $method = 'GET', array $options = []) |
132
|
|
|
{ |
133
|
|
|
return Response::buildFromPsrResponse($this->request($url, $method, $options, true)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Make a API request. |
138
|
|
|
* |
139
|
|
|
* @param string $url |
140
|
|
|
* @param string $method |
141
|
|
|
* @param array $options |
142
|
|
|
* @param bool $returnRaw |
143
|
|
|
* |
144
|
|
|
* @throws \Freyo\MtaH5\Kernel\Exceptions\InvalidConfigException |
145
|
|
|
* |
146
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\Freyo\MtaH5\Kernel\Support\Collection|array|object|string |
147
|
|
|
*/ |
148
|
|
|
public function request($url, $method = 'GET', array $options = [], $returnRaw = false) |
149
|
|
|
{ |
150
|
|
|
if (empty($this->middlewares)) { |
151
|
|
|
$this->registerHttpMiddlewares(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$response = $this->performRequest($url, $method, $options); |
155
|
|
|
|
156
|
|
|
return $returnRaw ? $response : $this->castResponseToType($response, $this->app->config->get('response_type')); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Register Guzzle middlewares. |
161
|
|
|
*/ |
162
|
|
|
protected function registerHttpMiddlewares() |
163
|
|
|
{ |
164
|
|
|
// log |
165
|
|
|
$this->pushMiddleware($this->logMiddleware(), 'log'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Log the request. |
170
|
|
|
* |
171
|
|
|
* @return \Closure |
172
|
|
|
*/ |
173
|
|
|
protected function logMiddleware() |
174
|
|
|
{ |
175
|
|
|
$formatter = new MessageFormatter($this->app['config']->get('http.log_template', MessageFormatter::DEBUG)); |
176
|
|
|
|
177
|
|
|
return Middleware::log($this->app['logger'], $formatter); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Extra request params. |
182
|
|
|
* |
183
|
|
|
* @return array |
184
|
|
|
*/ |
185
|
|
|
protected function prepends() |
186
|
|
|
{ |
187
|
|
|
return []; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
|
|
protected function getBaseUri() |
194
|
|
|
{ |
195
|
|
|
return $this->app['config']->get('http.base_uri'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Wrapping an API endpoint. |
200
|
|
|
* |
201
|
|
|
* @param string $endpoint |
202
|
|
|
* @param string $prefix |
203
|
|
|
* |
204
|
|
|
* @return string |
205
|
|
|
*/ |
206
|
|
|
protected function wrap($endpoint, $prefix = '') |
207
|
|
|
{ |
208
|
|
|
return implode('/', [$prefix, $endpoint]); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|