Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
request.php
1<?
2
4
8
13abstract class Request
14{
15 const TYPE_CODE = '';
16
18 protected $adapter;
19
21 protected $client;
22
24 protected $response;
25
27 protected $type;
28
30 protected $endpoint;
31
33 protected $useDirectQuery = false;
34
38 public function __construct()
39 {
40 $this->type = static::TYPE_CODE;
41
42 $options = array(
43 'socketTimeout' => 5
44 );
45 $this->client = new AdsHttpClient($options);
46 }
47
52 public function setUseDirectQuery($mode)
53 {
54 $this->useDirectQuery = $mode;
55 }
56
62 public function getAuthAdapter()
63 {
64 return $this->adapter;
65 }
66
73 public function setAuthAdapter(AuthAdapter $adapter)
74 {
75 $this->adapter = $adapter;
76 return $this;
77 }
78
84 public function getResponse()
85 {
86 return $this->response;
87 }
88
94 public function getClient()
95 {
96 return $this->client;
97 }
98
105 public function setClient(AdsHttpClient $client)
106 {
107 $this->client = $client;
108 return $this;
109 }
110
116 public function getEndpoint()
117 {
118 return $this->endpoint;
119 }
120
127 public static function create($type)
128 {
129 return Factory::create(get_called_class(), $type);
130 }
131
139 public function send(array $params = array())
140 {
141 if (!$this->adapter)
142 {
143 throw new SystemException('AuthAdapter not applied.');
144 }
145 $this->client = $this->client ?? new AdsHttpClient(['socketTimeout' => 5]);
146 $this->client->clearHeaders();
147
148 $response = Response::create($this->type);
149 $response->setRequest($this);
150 try
151 {
152 $data = $this->query($params);
153 $response->setResponseText($data);
154 $response->parse($data);
155 }
156 catch (\Exception $exception)
157 {
158 $response->addError(new Error($exception->getMessage(), $exception->getCode()));
159 }
160
161 if ($response->getErrorCollection()->count() > 0)
162 {
163 $errors = $response->getErrors();
164 foreach ($errors as $error)
165 {
166 if (!$error->getMessage())
167 {
168 continue;
169 }
170
171 ServiceLogTable::add(array(
172 'GROUP_ID' => 'retargeting',
173 'TYPE' => static::TYPE_CODE,
174 'CODE' => $error->getCode(),
175 'MESSAGE' => $error->getMessage()
176 ));
177 }
178 }
179
180
181 return $response;
182 }
183
190 abstract public function query(array $params = array());
191
192 protected function directQuery(array $params = array())
193 {
194 return [];
195 }
196}
send(array $params=array())
Definition request.php:139
directQuery(array $params=array())
Definition request.php:192
setAuthAdapter(AuthAdapter $adapter)
Definition request.php:73
setClient(AdsHttpClient $client)
Definition request.php:105
query(array $params=array())