1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
apclient.php
См. документацию.
1<?php
2namespace Bitrix\Socialservices;
3
4use Bitrix\Main\ArgumentException;
5use Bitrix\Main\ArgumentNullException;
6use Bitrix\Main\Error;
7use Bitrix\Main\ErrorCollection;
8use Bitrix\Main\SystemException;
9use Bitrix\Main\Web\HttpClient;
10use Bitrix\Main\Web\Json;
11use Bitrix\Main\Web\Uri;
12
14{
15 const ERROR_WRONG_ANSWER = 'WRONG_ANWSER';
16
17 const METHOD_BATCH = 'batch';
18
21
22 protected $connection = null;
23 protected $errorCollection = null;
24
25 protected static $requiredKeys = array('ENDPOINT');
26
27 public static function init()
28 {
30 if($connection)
31 {
32 return new self($connection);
33 }
34
35 return false;
36 }
37
38 public static function initById($connectionId)
39 {
40 $dbRes = ApTable::getById($connectionId);
41 $connection = $dbRes->fetch();
42
43 if($connection)
44 {
45 return new self($connection);
46 }
47
48 return false;
49 }
50
51 public function __construct(array $connection)
52 {
53 $this->errorCollection = new ErrorCollection();
54
55 if($this->checkConnection($connection))
56 {
57 $this->connection = $connection;
58 }
59 }
60
61 public function getConnection()
62 {
63 return $this->connection;
64 }
65
78 public function call($methodName, $additionalParams = null)
79 {
80 if($this->checkConnection($this->connection))
81 {
82 $request = $this->prepareRequest($additionalParams);
83
84 $httpClient = $this->getHttpClient();
85
86 $response = $httpClient->post(
87 $this->getRequestUrl($methodName),
89 );
90
92
93 if(!$result)
94 {
95 $this->errorCollection->add(
96 array(
97 new Error("Wrong answer from service", static::ERROR_WRONG_ANSWER)
98 )
99 );
100 }
101
102 return $result;
103 }
104 else
105 {
106 throw new SystemException("No connection credentials");
107 }
108 }
109
110 public function batch($actions)
111 {
112 $batch = array();
113
114 if(is_array($actions))
115 {
116 foreach($actions as $queryKey => $cmdData)
117 {
118 if(is_array($cmdData))
119 {
120 list($cmd, $cmdParams) = array_values($cmdData);
121 $batch['cmd'][$queryKey] = $cmd.(is_array($cmdParams) ? '?'.http_build_query($this->prepareRequestData($cmdParams)) : '');
122 }
123 else
124 {
125 $batch['cmd'][$queryKey] = $cmdData;
126 }
127 }
128 }
129
130 return $this->call(static::METHOD_BATCH, $batch);
131 }
132
133 public function getErrorCollection()
134 {
136 }
137
138 protected function getHttpClient()
139 {
140 return new HttpClient(array(
141 'socketTimeout' => static::HTTP_SOCKET_TIMEOUT,
142 'streamTimeout' => static::HTTP_STREAM_TIMEOUT,
143 ));
144 }
145
146 protected function getRequestUrl($methodName)
147 {
148 return $this->connection['ENDPOINT'].$methodName;
149 }
150
151 protected function prepareRequestData($additionalParams)
152 {
153 if(!is_array($additionalParams))
154 {
155 $additionalParams = array();
156 }
157
158 return $additionalParams;
159 }
160
161 protected function prepareRequest($additionalParams)
162 {
163 $additionalParams = $this->prepareRequestData($additionalParams);
164
165 return $additionalParams;
166 }
167
168 protected function prepareResponse($result)
169 {
170 try
171 {
172 return Json::decode($result);
173 }
174 catch(ArgumentException $e)
175 {
176 return false;
177 }
178 }
179
181 {
182 foreach(static::$requiredKeys as $key)
183 {
184 if(empty($connection[$key]))
185 {
186 throw new ArgumentNullException('connection['.$key.']');
187 }
188 }
189
190 $endpoint = new Uri($connection['ENDPOINT']);
191 if(!$endpoint->getHost())
192 {
193 throw new ArgumentException('Invalid connection[ENDPOINT] value');
194 }
195
196 return true;
197 }
198}
if(!Loader::includeModule('catalog')) if(!AccessController::getCurrent() ->check(ActionDictionary::ACTION_PRICE_EDIT)) if(!check_bitrix_sessid()) $request
Определения catalog_reindex.php:36
Определения error.php:15
Определения uri.php:17
const ERROR_WRONG_ANSWER
Определения apclient.php:15
checkConnection(array $connection)
Определения apclient.php:180
call($methodName, $additionalParams=null)
Определения apclient.php:78
prepareRequest($additionalParams)
Определения apclient.php:161
static $requiredKeys
Определения apclient.php:25
prepareResponse($result)
Определения apclient.php:168
const HTTP_SOCKET_TIMEOUT
Определения apclient.php:19
prepareRequestData($additionalParams)
Определения apclient.php:151
const HTTP_STREAM_TIMEOUT
Определения apclient.php:20
const METHOD_BATCH
Определения apclient.php:17
static init()
Определения apclient.php:27
getRequestUrl($methodName)
Определения apclient.php:146
batch($actions)
Определения apclient.php:110
static initById($connectionId)
Определения apclient.php:38
__construct(array $connection)
Определения apclient.php:51
static getConnection()
Определения ap.php:106
</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
if(empty($signedUserToken)) $key
Определения quickway.php:257
$response
Определения result.php:21
$dbRes
Определения yandex_detail.php:168