Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ExternalSender.php
1<?php
2
4
14
16{
17
18 protected string $apiKey;
19 protected string $apiEndpoint;
20
21 public function __construct(?string $apiKey, string $apiEndpoint, int $socketTimeout = 10, int $streamTimeout = 30)
22 {
23 $this->apiKey = $apiKey ?? '';
24 $this->apiEndpoint = $apiEndpoint;
25 $this->socketTimeout = $socketTimeout;
26 $this->streamTimeout = $streamTimeout;
27 }
28
29 public function callExternalMethod(string $method, ?array $requestParams = null, string $httpMethod = ''): HttpRequestResult
30 {
31 if ($this->apiKey === '')
32 {
33 $result = new HttpRequestResult();
34 $result->addError(new Error('Missing API key when requesting a service.'));
35
36 return $result;
37 }
38 $url = $this->apiEndpoint . $method;
39 $queryMethod = HttpClient::HTTP_GET;
40
41 $httpClient = new HttpClient([
42 'socketTimeout' => $this->socketTimeout,
43 'streamTimeout' => $this->streamTimeout,
44 'waitResponse' => true,
45 'version' => HttpClient::HTTP_1_1,
46 ]);
47 $httpClient->setHeader('User-Agent', static::USER_AGENT);
48 $httpClient->setHeader('Content-type', static::CONTENT_TYPE);
49 $httpClient->setHeader('X-API-KEY', $this->apiKey);
50 $httpClient->setCharset(static::CHARSET);
51
52 if (is_array($requestParams))
53 {
54 $queryMethod = HttpClient::HTTP_POST;
55 $requestParams = Json::encode($requestParams);
56 }
57
58 $result = new HttpRequestResult();
59 $result->setHttpRequest(new DTO\Request([
60 'method' => $queryMethod,
61 'uri' => $url,
62 'headers' => method_exists($httpClient, 'getRequestHeaders') ? $httpClient->getRequestHeaders()->toArray() : [],
63 'body' => $requestParams
64 ]));
65
66 $answer = [];
67 $errorInfo = [];
68 if ($httpClient->query($queryMethod, $url, $requestParams))
69 {
70 $answer = $this->parseExternalAnswer($httpClient->getResult());
71
72 if ($httpClient->getStatus() !== 200)
73 {
74 $errorInfo = [
75 'code' => $httpClient->getStatus(),
76 'error' => $this->getMessageByErrorCode('error-' . $httpClient->getStatus()),
77 ];
78 }
79 }
80 else
81 {
82 $error = $httpClient->getError();
83 $errorInfo = [
84 'code' => key($error),
85 'error' => current($error),
86 ];
87 }
88 $result->setHttpResponse(new DTO\Response([
89 'statusCode' => $httpClient->getStatus(),
90 'headers' => $httpClient->getHeaders()->toArray(),
91 'body' => $httpClient->getResult(),
92 'error' => Util::getHttpClientErrorString($httpClient)
93 ]));
94
95 $result->setData($answer);
96
97 if (array_key_exists('code', $errorInfo) && $errorInfo['code'] !== 'ok')
98 {
99 $result->addError(new Error($errorInfo['error'], $errorInfo['code'], $errorInfo));
100 }
101
102 return $result;
103 }
104
105 protected function getMessageByErrorCode(string $code)
106 {
107 $locCode = 'MESSAGESERVICE_SENDER_SMS_SMSEDNARU_';
108 $locCode .= StringHelper::str_replace('-', '_', mb_strtoupper($code));
109
110 return Loc::getMessage($locCode) ?? $code;
111 }
112
113 protected function parseExternalAnswer(string $httpResult): array
114 {
115 try
116 {
117 return Json::decode($httpResult);
118 }
119 catch (ArgumentException $exception)
120 {
121 return ['error' => 'error-json-parsing'];
122 }
123 }
124}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
callExternalMethod(string $method, ?array $requestParams=null, string $httpMethod='')
__construct(?string $apiKey, string $apiEndpoint, int $socketTimeout=10, int $streamTimeout=30)
static getHttpClientErrorString(HttpClient $httpClient)
Definition util.php:24