Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
httpquery.php
1<?php
2
4
8
10{
11 private const SERVICE_NAME = 'google';
15 private HttpClient $client;
19 private ?RequestLogger $logger = null;
20
21 public function __construct(HttpClient $client, int $userId, string $serviceName = 'google')
22 {
23 $this->client = $client;
24 if (RequestLogger::isEnabled())
25 {
26 $this->logger = new RequestLogger($userId, self::SERVICE_NAME);
27 }
28 }
29
33 public function getClient(): HttpClient
34 {
35 return $this->client;
36 }
37
45 public function query(string $method, string $url, $body = null): void
46 {
47 $this->client->query($method, $url, $body);
48
49 if ($this->logger)
50 {
51 $this->logger->write([
52 'requestParams' => $body,
53 'url' => $url,
54 'method' => $method,
55 'statusCode' => $this->client->getStatus(),
56 'response' => $this->prepareResponseForDebug($this->client->getResult()),
57 'error' => $this->prepareErrorForDebug($this->client->getResult()),
58 ]);
59 }
60 }
61
65 public function getStatus(): int
66 {
67 return $this->client->getStatus();
68 }
69
73 public function getResult(): string
74 {
75 return $this->client->getResult();
76 }
77
84 public function post(string $url, $body = null): void
85 {
86 $this->query(HttpClient::HTTP_POST, $url, $body);
87 }
88
95 public function get(string $url, $body = null): void
96 {
97 $this->query(HttpClient::HTTP_GET, $url, $body);
98 }
99
106 public function delete(string $url, $body = null): void
107 {
108 $this->query(HttpClient::HTTP_DELETE, $url, $body);
109 }
110
116 public function put(string $url, $body = null): void
117 {
118 $this->query(HttpClient::HTTP_PUT, $url, $body);
119 }
120
125 private function prepareResponseForDebug($response): string
126 {
127 if ($this->client->getStatus() >= 300)
128 {
129 return '';
130 }
131
132 try
133 {
134 $response = \Bitrix\Main\Web\Json::decode($response);
135 }
136 catch (\Exception $e){}
137
138 if (!$response || !is_array($response))
139 {
140 return '';
141 }
142
143 $result = '';
144
145 foreach ($response as $key => $value)
146 {
147 if (is_string($value))
148 {
149 $result .= "{$key}:{$value}; ";
150 }
151 elseif (is_array($value))
152 {
153 $result .= "{$key}:";
154 foreach ($value as $valueKey => $valueValue)
155 {
156 $result .= "{$valueKey}:{$valueValue}, ";
157 }
158 $result .= "; ";
159 }
160 }
161
162 return $result;
163 }
164
168 private function prepareErrorForDebug($response): string
169 {
170 try
171 {
172 $response = \Bitrix\Main\Web\Json::decode($response);
173 }
174 catch (\Exception $e){}
175
176 if (
177 (!$response || !is_array($response))
178 || ($this->client->getStatus() < 400)
179 )
180 {
181 return '';
182 }
183
184 return $response['error']['code'] . " " . $response['error']['message'] . "; ";
185 }
186}
post(string $url, $body=null)
Definition httpquery.php:84
__construct(HttpClient $client, int $userId, string $serviceName='google')
Definition httpquery.php:21
put(string $url, $body=null)
query(string $method, string $url, $body=null)
Definition httpquery.php:45