Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ExternalSender.php
1<?php
2
4
16
18{
19
20 public function __construct(?string $apiKey, string $apiEndpoint, int $socketTimeout = 10, int $streamTimeout = 30)
21 {
22 $this->apiKey = $apiKey ?? '';
23 $this->apiEndpoint = $apiEndpoint;
24 $this->socketTimeout = $socketTimeout;
25 $this->streamTimeout = $streamTimeout;
26 }
27
34 public function callExternalMethod(string $method, ?array $requestParams = null, string $httpMethod = ''): HttpRequestResult
35 {
36 if ($this->apiKey === '')
37 {
38 $result = new HttpRequestResult();
39 $result->addError(new Error(Loc::getMessage('MESSAGESERVICE_SENDER_SMS_EDNARU_ERROR_SYSTEM')));
40
41 return $result;
42 }
43
44 $url = $this->apiEndpoint . $method;
45 $queryMethod = HttpClient::HTTP_GET;
46
47 $httpClient = new HttpClient([
48 'socketTimeout' => $this->socketTimeout,
49 'streamTimeout' => $this->streamTimeout,
50 'waitResponse' => static::WAIT_RESPONSE,
51 'version' => HttpClient::HTTP_1_1,
52 ]);
53 $httpClient->setHeader('User-Agent', static::USER_AGENT);
54 $httpClient->setHeader('Content-type', static::CONTENT_TYPE);
55 $httpClient->setHeader('X-API-KEY', $this->apiKey);
56 $httpClient->setCharset(static::CHARSET);
57
58
59 if (isset($requestParams) && $httpMethod !== HttpClient::HTTP_GET)
60 {
61 $queryMethod = HttpClient::HTTP_POST;
62 }
63 $queryMethod = $httpMethod ?: $queryMethod;
64
65 if (isset($requestParams) && $queryMethod === HttpClient::HTTP_POST)
66 {
67 $requestParams = Json::encode($this->convertRequestParams($requestParams));
68 }
69
70 if (isset($requestParams) && $queryMethod === HttpClient::HTTP_GET)
71 {
72 $url .= '?' . http_build_query($requestParams);
73 }
74
75 $result = new HttpRequestResult();
76 $result->setHttpRequest(new Request([
77 'method' => $queryMethod,
78 'uri' => $url,
79 'headers' => method_exists($httpClient, 'getRequestHeaders') ? $httpClient->getRequestHeaders()->toArray() : [],
80 'body' => $requestParams,
81 ]));
82
83 if ($httpClient->query($queryMethod, $url, $requestParams))
84 {
85 $response = $this->parseExternalResponse($httpClient->getResult());
86 }
87 else
88 {
89 $result->setHttpResponse(new Response([
90 'error' => Util::getHttpClientErrorString($httpClient)
91 ]));
92 $error = $httpClient->getError();
93 $response = ['code' => current($error)];
94 }
95
96 $result->setHttpResponse(new Response([
97 'statusCode' => $httpClient->getStatus(),
98 'headers' => $httpClient->getHeaders()->toArray(),
99 'body' => $httpClient->getResult(),
100 ]));
101
102 if (!$this->checkResponse($response))
103 {
104 $errorMessage = '';
105
106 if (isset($response['title']))
107 {
108 $errorMessage = $response['title'];
109 }
110
111 if (isset($response['code']))
112 {
113 $errorMessage = $this->getErrorMessageByCode($response['code']);
114 }
115
116 if (isset($response['detail']))
117 {
118 $errorMessage = $response['detail'];
119 }
120
121 $result->addError(new Error($errorMessage));
122
123 return $result;
124 }
125 $result->setData($response);
126
127 return $result;
128 }
129
130 protected function parseExternalResponse(string $httpResult): array
131 {
132 try
133 {
134 return Json::decode($httpResult);
135 }
136 catch (ArgumentException $exception)
137 {
138 return ['code' => 'error-json-parsing'];
139 }
140 }
141
142 protected function checkResponse(array $response): bool
143 {
144 // Success response without "code" parameter https://edna.docs.apiary.io/#reference/api/by-apikey
145 if ($this->apiEndpoint === Old\Constants::API_ENDPOINT)
146 {
147 return (isset($response['code']) && $response['code'] === 'ok') || !isset($response['code']);
148 }
149
150 if (isset($response['title']) && $response['title'] === 'system-error')
151 {
152 return false;
153 }
154
155 return (isset($response['status']) && (int)$response['status'] === 200) || !isset($response['status']);
156 }
157
158 protected function convertRequestParams(array $requestParams): array
159 {
161 {
162 $requestParams = Encoding::convertEncoding($requestParams, SITE_CHARSET, 'UTF-8');
163 }
164
165 return $requestParams;
166 }
167
175 protected function getErrorMessageByCode(?string $errorCode): string
176 {
177 $errorCode = mb_strtoupper($errorCode);
178 $errorCode = str_replace("-", "_", $errorCode);
179
180 $errorMessage = Loc::getMessage('MESSAGESERVICE_SENDER_SMS_EDNARU_'.$errorCode);
181
182 return $errorMessage ? : Loc::getMessage('MESSAGESERVICE_SENDER_SMS_EDNARU_UNKNOWN_ERROR');
183 }
184}
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)