Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
api.php
1<?php
2
4
11
18final class Api
19{
23 private const HTTP_VERSION = HttpClient::HTTP_1_1;
24 private const HTTP_SOCKET_TIMEOUT = 10;
25 private const HTTP_STREAM_TIMEOUT = 10;
26
30 private const API_SEARCH_LIMIT = 10;
31
32 private const API_AUTOCOMPLETE_LIMIT = 7;
33
35 private $source;
36
41 public function __construct(OsmSource $source)
42 {
43 $this->source = $source;
44 }
45
51 public function search(array $options): array
52 {
53 $client = $this->makeHttpClient();
54
55 $body = $client->get(
56 $this->buildUrl(
57 'location',
58 'search',
59 $this->wrapQueryData(
60 [
61 'q' => Encoding::convertEncoding($options['q'], SITE_CHARSET, 'UTF-8'),
62 'addressdetails' => isset($options['addressdetails']) ? (int)$options['addressdetails'] : 0,
63 'limit' => isset($options['limit']) ? (int)$options['limit'] : self::API_SEARCH_LIMIT,
64 'accept-language' => $options['accept-language'] ?? '',
65 'format' => 'json',
66 ]
67 )
68 )
69 );
70
71 return $this->getResponse($client, $body);
72 }
73
78 public function autocomplete(array $options): array
79 {
80 $client = $this->makeHttpClient();
81
82 $queryData = [
83 'q' => Encoding::convertEncoding($options['q'], SITE_CHARSET, 'UTF-8'),
84 'limit' => isset($options['limit']) ? (int)$options['limit'] : self::API_AUTOCOMPLETE_LIMIT,
85 'lang' => $options['lang'] ?? '',
86 'version' => 2,
87 ];
88 if (isset($options['lat']) && isset($options['lon']))
89 {
90 $queryData['lat'] = $options['lat'];
91 $queryData['lon'] = $options['lon'];
92 }
93
94 $body = $client->get(
95 $this->buildUrl(
96 'autocomplete',
97 'autocomplete',
98 $this->wrapQueryData(
99 $queryData
100 )
101 )
102 );
103
104 return $this->getResponse($client, $body);
105 }
106
112 public function lookup(array $options): array
113 {
114 $client = $this->makeHttpClient();
115
116 $body = $client->get(
117 $this->buildUrl(
118 'location',
119 'lookup',
120 $this->wrapQueryData(
121 [
122 'osm_ids' => $options['osm_ids'] ?? '',
123 'addressdetails' => isset($options['addressdetails']) ? (int)$options['addressdetails'] : 0,
124 'accept-language' => $options['accept-language'] ?? '',
125 'format' => 'json',
126 ]
127 )
128 )
129 );
130
131 return $this->getResponse($client, $body);
132 }
133
139 public function details(array $options): array
140 {
141 $client = $this->makeHttpClient();
142
143 $body = $client->get(
144 $this->buildUrl(
145 'location',
146 'details',
147 $this->wrapQueryData(
148 [
149 'osmtype' => $options['osm_type'] ?? '',
150 'osmid' => $options['osm_id'] ?? '',
151 'format' => 'json',
152 'addressdetails' => isset($options['addressdetails']) ? (int)$options['addressdetails'] : 0,
153 'linkedplaces' => isset($options['linkedplaces']) ? (int)$options['linkedplaces'] : 0,
154 'hierarchy' => isset($options['hierarchy']) ? (int)$options['hierarchy'] : 0,
155 'accept-language' => $options['accept-language'] ?? '',
156 ]
157 )
158 )
159 );
160
161 return $this->getResponse($client, $body);
162 }
163
170 private function getResponse(HttpClient $client, string $body): array
171 {
172 $status = $client->getStatus();
173
174 if ($body === false)
175 {
176 $errors = $client->getError();
177
178 throw new RuntimeException(
179 implode('; ', array_map(
180 function ($v, $k) { return sprintf("%s=%s", $k, $v); },
181 $errors,
182 array_keys($errors)
183 ))
184 );
185 }
186
187 if ($status != 200)
188 {
189 throw new RuntimeException(sprintf('Unexpected status code - %s', $status));
190 }
191
192 try
193 {
194 $response = Json::decode($body);
195 }
196 catch (ArgumentException $e)
197 {
198 throw new RuntimeException('JSON decode error');
199 }
200
201 if (!is_array($response))
202 {
203 throw new RuntimeException('Response format error');
204 }
205
206 return $response;
207 }
208
215 private function makeHttpClient(): HttpClient
216 {
217 $token = $this->source->getOsmToken();
218
219 $result = new HttpClient(
220 [
221 'version' => self::HTTP_VERSION,
222 'socketTimeout' => self::HTTP_SOCKET_TIMEOUT,
223 'streamTimeout' => self::HTTP_STREAM_TIMEOUT,
224 ]
225 );
226
227 $result->setHeader(
228 'Authorization',
229 sprintf('Bearer %s', ($token ? $token->getToken() : ''))
230 );
231
232 $result->setHeader('Bx-Location-Osm-Host', $this->source->getOsmHostName());
233
234 return $result;
235 }
236
243 private function buildUrl(string $controller, string $action, array $queryData): string
244 {
245 $serviceUrl = $this->source->getOsmApiUrl();
246
247 if (!$serviceUrl)
248 {
249 throw new RuntimeException('Service url is not specified');
250 }
251
252 return sprintf(
253 '%s/?%s',
254 $serviceUrl,
255 http_build_query(
256 array_merge(
257 $queryData,
258 [
259 'action' => sprintf('osmgateway.%s.%s', $controller, $action)
260 ]
261 )
262 )
263 );
264 }
265
270 private function wrapQueryData(array $queryData): array
271 {
272 return [
273 'params' => $queryData
274 ];
275 }
276}
__construct(OsmSource $source)
Definition api.php:41
autocomplete(array $options)
Definition api.php:78