1
|
|
|
<?php |
2
|
|
|
namespace Mezon\CustomClient; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class CustomClient |
6
|
|
|
* |
7
|
|
|
* @package Mezon |
8
|
|
|
* @subpackage CustomClient |
9
|
|
|
* @author Dodonov A.A. |
10
|
|
|
* @version v.1.0 (2019/08/07) |
11
|
|
|
* @copyright Copyright (c) 2019, aeon.org |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Custom API client class |
16
|
|
|
*/ |
17
|
|
|
class CustomClient |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Server host |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $url = false; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Headers |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $headers = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Idempotence key |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $idempotencyKey = ''; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor |
43
|
|
|
* |
44
|
|
|
* @param string $url |
45
|
|
|
* Service URL |
46
|
|
|
* @param array $headers |
47
|
|
|
* HTTP headers |
48
|
|
|
*/ |
49
|
|
|
public function __construct(string $url, array $headers = []) |
50
|
|
|
{ |
51
|
|
|
if ($url === false || $url === '') { |
52
|
|
|
throw (new \Exception( |
53
|
|
|
'Service URL must be set in class ' . __CLASS__ . ' extended in ' . get_called_class() . |
54
|
|
|
' and called from ' . ($_SERVER['SERVER_NAME'] ?? 'console') . ($_SERVER['REQUEST_URI'] ?? ''), |
55
|
|
|
- 23)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->url = rtrim($url, '/'); |
59
|
|
|
|
60
|
|
|
$this->headers = $headers; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Method send request to the URL |
65
|
|
|
* |
66
|
|
|
* @param string $url |
67
|
|
|
* URL |
68
|
|
|
* @param array $headers |
69
|
|
|
* Headers |
70
|
|
|
* @param string $method |
71
|
|
|
* Request HTTP Method |
72
|
|
|
* @param array $data |
73
|
|
|
* Request data |
74
|
|
|
* @return array Response body and HTTP code |
75
|
|
|
* @codeCoverageIgnore |
76
|
|
|
*/ |
77
|
|
|
protected function sendRequest(string $url, array $headers, string $method, array $data = []): array |
78
|
|
|
{ |
79
|
|
|
return \Mezon\CustomClient\CurlWrapper::sendRequest($url, $headers, $method, $data); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Method gets result and validates it. |
84
|
|
|
* |
85
|
|
|
* @param string $url |
86
|
|
|
* request URL |
87
|
|
|
* @param int $code |
88
|
|
|
* response HTTP code |
89
|
|
|
* @param string $body |
90
|
|
|
* response body |
91
|
|
|
* @return mixed Request result |
92
|
|
|
*/ |
93
|
|
|
protected function dispatchResult(string $url, int $code, string $body) |
94
|
|
|
{ |
95
|
|
|
if ($code == 404) { |
96
|
|
|
throw (new \Exception("URL: $url not found")); |
97
|
|
|
} elseif ($code == 400) { |
98
|
|
|
throw (new \Exception("Bad request on URL $url")); |
99
|
|
|
} elseif ($code == 403) { |
100
|
|
|
throw (new \Exception("Auth error")); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $body; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Method returns common headers |
108
|
|
|
* |
109
|
|
|
* @return array Headers |
110
|
|
|
*/ |
111
|
|
|
protected function getCommonHeaders(): array |
112
|
|
|
{ |
113
|
|
|
$result = $this->headers; |
114
|
|
|
|
115
|
|
|
if ($this->idempotencyKey !== '') { |
116
|
|
|
$result[] = 'Idempotency-Key: ' . $this->idempotencyKey; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$result[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0'; |
120
|
|
|
|
121
|
|
|
return $result; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Method compiles post headers |
126
|
|
|
* |
127
|
|
|
* @return array Header |
128
|
|
|
*/ |
129
|
|
|
protected function getFormHeaders(): array |
130
|
|
|
{ |
131
|
|
|
$fullHeaders = $this->getCommonHeaders(); |
132
|
|
|
|
133
|
|
|
$fullHeaders[] = 'Content-type: application/x-www-form-urlencoded'; |
134
|
|
|
|
135
|
|
|
return $fullHeaders; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Method sends request on server |
140
|
|
|
* |
141
|
|
|
* @param string $method |
142
|
|
|
* HTTP method (POST|PUT|DELETE) |
143
|
|
|
* @param string $endpoint |
144
|
|
|
* Calling endpoint |
145
|
|
|
* @param mixed $data |
146
|
|
|
* Request data |
147
|
|
|
*/ |
148
|
|
|
protected function sendFormRequest(string $method, string $endpoint, array $data = []) |
149
|
|
|
{ |
150
|
|
|
$fullUrl = $this->url . '/' . ltrim($endpoint, '/'); |
151
|
|
|
|
152
|
|
|
list ($body, $code) = $this->sendRequest($fullUrl, $this->getFormHeaders(), $method, $data); |
153
|
|
|
|
154
|
|
|
return $this->dispatchResult($fullUrl, $code, $body); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Method sends POST request to server |
159
|
|
|
* |
160
|
|
|
* @param string $endpoint |
161
|
|
|
* Calling endpoint |
162
|
|
|
* @param array $data |
163
|
|
|
* Request data |
164
|
|
|
* @return mixed Result of the request |
165
|
|
|
*/ |
166
|
|
|
public function sendPostRequest(string $endpoint, array $data = []) |
167
|
|
|
{ |
168
|
|
|
return $this->sendFormRequest('POST', $endpoint, $data); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Method sends PUT request to server |
173
|
|
|
* |
174
|
|
|
* @param string $endpoint |
175
|
|
|
* Calling endpoint |
176
|
|
|
* @param array $data |
177
|
|
|
* Request data |
178
|
|
|
* @return mixed Result of the request |
179
|
|
|
*/ |
180
|
|
|
public function sendPutRequest(string $endpoint, array $data = []) |
181
|
|
|
{ |
182
|
|
|
return $this->sendFormRequest('PUT', $endpoint, $data); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Method sends DELETE request to server |
187
|
|
|
* |
188
|
|
|
* @param string $endpoint |
189
|
|
|
* Calling endpoint |
190
|
|
|
* @param array $data |
191
|
|
|
* Request data |
192
|
|
|
* @return mixed Result of the request |
193
|
|
|
*/ |
194
|
|
|
public function sendDeleteRequest(string $endpoint, array $data = []) |
195
|
|
|
{ |
196
|
|
|
return $this->sendFormRequest('DELETE', $endpoint, $data); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Method sends GET request to server. |
201
|
|
|
* |
202
|
|
|
* @param string $endpoint |
203
|
|
|
* Calling endpoint. |
204
|
|
|
* @return mixed Result of the remote call. |
205
|
|
|
*/ |
206
|
|
|
public function sendGetRequest(string $endpoint) |
207
|
|
|
{ |
208
|
|
|
$fullUrl = $this->url . '/' . ltrim($endpoint, '/'); |
209
|
|
|
|
210
|
|
|
$fullUrl = str_replace(' ', '%20', $fullUrl); |
211
|
|
|
|
212
|
|
|
list ($body, $code) = $this->sendRequest($fullUrl, $this->getCommonHeaders(), 'GET'); |
213
|
|
|
|
214
|
|
|
return $this->dispatchResult($fullUrl, $code, $body); |
|
|
|
|
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Method sets idempotence key. |
219
|
|
|
* To remove the key just call this method the second time with the '' parameter |
220
|
|
|
* |
221
|
|
|
* @param string $key |
222
|
|
|
* Idempotence key |
223
|
|
|
*/ |
224
|
|
|
public function setIdempotencyKey(string $key): void |
225
|
|
|
{ |
226
|
|
|
$this->idempotencyKey = $key; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Method returns idempotency key |
231
|
|
|
* |
232
|
|
|
* @return string Idempotency key |
233
|
|
|
*/ |
234
|
|
|
public function getIdempotencyKey(): string |
235
|
|
|
{ |
236
|
|
|
return $this->idempotencyKey; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Method returns URL |
241
|
|
|
* |
242
|
|
|
* @return string URL |
243
|
|
|
*/ |
244
|
|
|
public function getUrl(): string |
245
|
|
|
{ |
246
|
|
|
return $this->url; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Method returns headers |
251
|
|
|
* |
252
|
|
|
* @return array Headers |
253
|
|
|
*/ |
254
|
|
|
public function getHeaders(): array |
255
|
|
|
{ |
256
|
|
|
return $this->headers; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|