1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
ExternalSender.php
См. документацию.
1<?php
2
3namespace Bitrix\MessageService\Providers\Edna\WhatsApp;
4
5use Bitrix\Main\ArgumentException;
6use Bitrix\Main\Error;
7use Bitrix\Main\Localization\Loc;
8use Bitrix\Main\Web\HttpClient;
9use Bitrix\Main\Web\Json;
10use Bitrix\MessageService\DTO\Request;
11use Bitrix\MessageService\DTO\Response;
12use Bitrix\MessageService\Sender\Result\HttpRequestResult;
13use Bitrix\MessageService\Sender\Util;
14
16{
17
18 public function __construct(?string $apiKey, string $apiEndpoint, int $socketTimeout = 10, int $streamTimeout = 30)
19 {
20 $this->apiKey = $apiKey ?? '';
21 $this->apiEndpoint = $apiEndpoint;
22 $this->socketTimeout = $socketTimeout;
23 $this->streamTimeout = $streamTimeout;
24 }
25
32 public function callExternalMethod(string $method, ?array $requestParams = null, string $httpMethod = ''): HttpRequestResult
33 {
34 if ($this->apiKey === '')
35 {
37 $result->addError(new Error(Loc::getMessage('MESSAGESERVICE_SENDER_SMS_EDNARU_ERROR_SYSTEM')));
38
39 return $result;
40 }
41
42 $url = $this->apiEndpoint . $method;
43 $queryMethod = HttpClient::HTTP_GET;
44
45 $httpClient = new HttpClient([
46 'socketTimeout' => $this->socketTimeout,
47 'streamTimeout' => $this->streamTimeout,
48 'waitResponse' => static::WAIT_RESPONSE,
49 'version' => HttpClient::HTTP_1_1,
50 ]);
51 $httpClient->setHeader('User-Agent', static::USER_AGENT);
52 $httpClient->setHeader('Content-type', static::CONTENT_TYPE);
53 $httpClient->setHeader('X-API-KEY', $this->apiKey);
54 $httpClient->setCharset(static::CHARSET);
55
56
57 if (isset($requestParams) && $httpMethod !== HttpClient::HTTP_GET)
58 {
59 $queryMethod = HttpClient::HTTP_POST;
60 }
61 $queryMethod = $httpMethod ?: $queryMethod;
62
63 if (isset($requestParams) && $queryMethod === HttpClient::HTTP_POST)
64 {
65 $requestParams = Json::encode($requestParams, JSON_UNESCAPED_UNICODE);
66 }
67
68 if (isset($requestParams) && $queryMethod === HttpClient::HTTP_GET)
69 {
70 $url .= '?' . http_build_query($requestParams);
71 }
72
74 $result->setHttpRequest(new Request([
75 'method' => $queryMethod,
76 'uri' => $url,
77 'headers' => method_exists($httpClient, 'getRequestHeaders') ? $httpClient->getRequestHeaders()->toArray() : [],
78 'body' => $requestParams,
79 ]));
80
81 if ($httpClient->query($queryMethod, $url, $requestParams))
82 {
83 $response = $this->parseExternalResponse($httpClient->getResult());
84 }
85 else
86 {
87 $result->setHttpResponse(new Response([
88 'error' => Util::getHttpClientErrorString($httpClient)
89 ]));
90 $error = $httpClient->getError();
91 $response = ['code' => current($error)];
92 }
93
94 $result->setHttpResponse(new Response([
95 'statusCode' => $httpClient->getStatus(),
96 'headers' => $httpClient->getHeaders()->toArray(),
97 'body' => $httpClient->getResult(),
98 ]));
99
100 if (!$this->checkResponse($response))
101 {
102 $errorMessage = '';
103
104 if (isset($response['title']))
105 {
106 $errorMessage = $response['title'];
107 }
108
109 if (isset($response['code']))
110 {
112 }
113
114 if (isset($response['detail']))
115 {
116 $errorMessage = $response['detail'];
117 }
118
119 $result->addError(new Error($errorMessage));
120
121 return $result;
122 }
123 $result->setData($response);
124
125 return $result;
126 }
127
128 protected function parseExternalResponse(string $httpResult): array
129 {
130 try
131 {
132 return Json::decode($httpResult);
133 }
134 catch (ArgumentException $exception)
135 {
136 return ['code' => 'error-json-parsing'];
137 }
138 }
139
140 protected function checkResponse(array $response): bool
141 {
142 // Success response without "code" parameter https://edna.docs.apiary.io/#reference/api/by-apikey
143 if ($this->apiEndpoint === Old\Constants::API_ENDPOINT)
144 {
145 return (isset($response['code']) && $response['code'] === 'ok') || !isset($response['code']);
146 }
147
148 if (isset($response['title']) && $response['title'] === 'system-error')
149 {
150 return false;
151 }
152
153 return (isset($response['status']) && (int)$response['status'] === 200) || !isset($response['status']);
154 }
155
163 protected function getErrorMessageByCode(?string $errorCode): string
164 {
165 $errorCode = mb_strtoupper($errorCode);
166 $errorCode = str_replace("-", "_", $errorCode);
167
168 $errorMessage = Loc::getMessage('MESSAGESERVICE_SENDER_SMS_EDNARU_'.$errorCode);
169
170 return $errorMessage ? : Loc::getMessage('MESSAGESERVICE_SENDER_SMS_EDNARU_UNKNOWN_ERROR');
171 }
172}
Определения error.php:15
Определения request.php:10
Определения response.php:5
callExternalMethod(string $method, ?array $requestParams=null, string $httpMethod='')
Определения ExternalSender.php:32
__construct(?string $apiKey, string $apiEndpoint, int $socketTimeout=10, int $streamTimeout=30)
Определения ExternalSender.php:18
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
while($arParentIBlockProperty=$dbParentIBlockProperty->Fetch()) $errorMessage
$requestParams
Определения urlrewrite.php:46
$response
Определения result.php:21
$method
Определения index.php:27
$error
Определения subscription_card_product.php:20
$url
Определения iframe.php:7