@cdek-it/widget 3.10.3 → 3.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cdek-widget.es.js +69 -40
- package/dist/cdek-widget.umd.js +2 -2
- package/dist/service.php +57 -9
- package/package.json +1 -1
package/dist/service.php
CHANGED
|
@@ -33,16 +33,21 @@ class service
|
|
|
33
33
|
* @var array Data From Request
|
|
34
34
|
*/
|
|
35
35
|
private $requestData;
|
|
36
|
+
/** @var array Request metrics */
|
|
37
|
+
private $metrics;
|
|
36
38
|
|
|
37
39
|
public function __construct($login, $secret, $baseUrl = 'https://api.cdek.ru/v2')
|
|
38
40
|
{
|
|
39
41
|
$this->login = $login;
|
|
40
42
|
$this->secret = $secret;
|
|
41
43
|
$this->baseUrl = $baseUrl;
|
|
44
|
+
$this->metrics = array();
|
|
42
45
|
}
|
|
43
46
|
|
|
44
47
|
public function process($requestData, $body)
|
|
45
48
|
{
|
|
49
|
+
$time = $this->startMetrics();
|
|
50
|
+
|
|
46
51
|
$this->requestData = array_merge($requestData, json_decode($body, true) ?: array());
|
|
47
52
|
|
|
48
53
|
if (!isset($this->requestData['action'])) {
|
|
@@ -53,9 +58,11 @@ class service
|
|
|
53
58
|
|
|
54
59
|
switch ($this->requestData['action']) {
|
|
55
60
|
case 'offices':
|
|
56
|
-
$this->sendResponse($this->getOffices());
|
|
61
|
+
$this->sendResponse($this->getOffices(), $time);
|
|
62
|
+
break;
|
|
57
63
|
case 'calculate':
|
|
58
|
-
$this->sendResponse($this->calculate());
|
|
64
|
+
$this->sendResponse($this->calculate(), $time);
|
|
65
|
+
break;
|
|
59
66
|
default:
|
|
60
67
|
$this->sendValidationError('Unknown action');
|
|
61
68
|
}
|
|
@@ -65,7 +72,7 @@ class service
|
|
|
65
72
|
{
|
|
66
73
|
$this->http_response_code(400);
|
|
67
74
|
header('Content-Type: application/json');
|
|
68
|
-
header('X-Service-Version: 3.
|
|
75
|
+
header('X-Service-Version: 3.11.0');
|
|
69
76
|
echo json_encode(array('message' => $message));
|
|
70
77
|
exit();
|
|
71
78
|
}
|
|
@@ -186,7 +193,6 @@ class service
|
|
|
186
193
|
break;
|
|
187
194
|
default:
|
|
188
195
|
exit('Unknown http status code "' . htmlentities($code) . '"');
|
|
189
|
-
break;
|
|
190
196
|
}
|
|
191
197
|
|
|
192
198
|
$protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
|
|
@@ -196,12 +202,18 @@ class service
|
|
|
196
202
|
|
|
197
203
|
private function getAuthToken()
|
|
198
204
|
{
|
|
205
|
+
$time = $this->startMetrics();
|
|
206
|
+
|
|
199
207
|
$token = $this->httpRequest('oauth/token', array(
|
|
200
208
|
'grant_type' => 'client_credentials',
|
|
201
209
|
'client_id' => $this->login,
|
|
202
210
|
'client_secret' => $this->secret,
|
|
203
211
|
), true);
|
|
212
|
+
|
|
213
|
+
$this->endMetrics('auth', 'Server Auth Time', $time);
|
|
214
|
+
|
|
204
215
|
$result = json_decode($token['result'], true);
|
|
216
|
+
|
|
205
217
|
if (!isset($result['access_token'])) {
|
|
206
218
|
throw new RuntimeException('Server not authorized to CDEK API');
|
|
207
219
|
}
|
|
@@ -209,6 +221,11 @@ class service
|
|
|
209
221
|
$this->authToken = $result['access_token'];
|
|
210
222
|
}
|
|
211
223
|
|
|
224
|
+
private function startMetrics()
|
|
225
|
+
{
|
|
226
|
+
return function_exists('hrtime') ? hrtime(true) : microtime(true);
|
|
227
|
+
}
|
|
228
|
+
|
|
212
229
|
private function httpRequest($method, $data, $useFormData = false, $useJson = false)
|
|
213
230
|
{
|
|
214
231
|
$ch = curl_init("$this->baseUrl/$method");
|
|
@@ -216,6 +233,7 @@ class service
|
|
|
216
233
|
$headers = array(
|
|
217
234
|
'Accept: application/json',
|
|
218
235
|
'X-App-Name: widget_pvz',
|
|
236
|
+
'X-App-Version: 3.11.0'
|
|
219
237
|
);
|
|
220
238
|
|
|
221
239
|
if ($this->authToken) {
|
|
@@ -238,7 +256,7 @@ class service
|
|
|
238
256
|
}
|
|
239
257
|
|
|
240
258
|
curl_setopt_array($ch, array(
|
|
241
|
-
CURLOPT_USERAGENT => 'widget/3.
|
|
259
|
+
CURLOPT_USERAGENT => 'widget/3.11.0',
|
|
242
260
|
CURLOPT_HTTPHEADER => $headers,
|
|
243
261
|
CURLOPT_RETURNTRANSFER => true,
|
|
244
262
|
CURLOPT_HEADER => true,
|
|
@@ -248,7 +266,9 @@ class service
|
|
|
248
266
|
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
|
|
249
267
|
$headers = substr($response, 0, $headerSize);
|
|
250
268
|
$result = substr($response, $headerSize);
|
|
269
|
+
|
|
251
270
|
$addedHeaders = $this->getHeaderValue($headers);
|
|
271
|
+
|
|
252
272
|
if ($result === false) {
|
|
253
273
|
throw new RuntimeException(curl_error($ch), curl_errno($ch));
|
|
254
274
|
}
|
|
@@ -264,27 +284,55 @@ class service
|
|
|
264
284
|
});
|
|
265
285
|
}
|
|
266
286
|
|
|
267
|
-
private function
|
|
287
|
+
private function endMetrics($metricName, $metricDescription, $start)
|
|
288
|
+
{
|
|
289
|
+
$this->metrics[] = array(
|
|
290
|
+
'name' => $metricName,
|
|
291
|
+
'description' => $metricDescription,
|
|
292
|
+
'time' => round(function_exists('hrtime') ? (hrtime(true) - $start) / 1e+6 : (microtime(true) - $start) * 1000,
|
|
293
|
+
2),
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
private function sendResponse($data, $start)
|
|
268
298
|
{
|
|
269
299
|
$this->http_response_code(200);
|
|
270
300
|
header('Content-Type: application/json');
|
|
271
|
-
header('X-Service-Version: 3.
|
|
301
|
+
header('X-Service-Version: 3.11.0');
|
|
272
302
|
if (!empty($data['addedHeaders'])) {
|
|
273
303
|
foreach ($data['addedHeaders'] as $header) {
|
|
274
304
|
header($header);
|
|
275
305
|
}
|
|
276
306
|
}
|
|
307
|
+
|
|
308
|
+
$this->endMetrics('total', 'Total Time', $start);
|
|
309
|
+
|
|
310
|
+
if (!empty($this->metrics)) {
|
|
311
|
+
header('Server-Timing: ' . array_reduce($this->metrics, function ($c, $i) {
|
|
312
|
+
return $c . $i['name'] . ';desc="' . $i['description'] . '";dur=' . $i['time'] . ',';
|
|
313
|
+
}, ''));
|
|
314
|
+
}
|
|
315
|
+
|
|
277
316
|
echo $data['result'];
|
|
317
|
+
|
|
278
318
|
exit();
|
|
279
319
|
}
|
|
280
320
|
|
|
281
321
|
protected function getOffices()
|
|
282
322
|
{
|
|
283
|
-
|
|
323
|
+
$time = $this->startMetrics();
|
|
324
|
+
$result = $this->httpRequest('deliverypoints', $this->requestData);
|
|
325
|
+
|
|
326
|
+
$this->endMetrics('office', 'Offices Request', $time);
|
|
327
|
+
return $result;
|
|
284
328
|
}
|
|
285
329
|
|
|
286
330
|
protected function calculate()
|
|
287
331
|
{
|
|
288
|
-
|
|
332
|
+
$time = $this->startMetrics();
|
|
333
|
+
$result = $this->httpRequest('calculator/tarifflist', $this->requestData, false, true);
|
|
334
|
+
|
|
335
|
+
$this->endMetrics('calc', 'Calculate Request', $time);
|
|
336
|
+
return $result;
|
|
289
337
|
}
|
|
290
338
|
}
|