5use Bitrix\Calendar\Core;
25 private static array $httpClients = [];
35 parent::__construct($connection);
36 $this->userId = $userId;
37 if (!$this->initHttpClient())
39 $this->deactivateConnection();
51 private function initHttpClient(
bool $force =
false): bool
54 $userId = $this->userId;
55 if (!isset(self::$httpClients[$userId]) || $force)
57 if (!Loader::includeModule(
'socialservices'))
63 if (\CSocServGoogleProxyOAuth::isProxyAuth())
65 $oAuth = new \CSocServGoogleProxyOAuth($userId);
69 $oAuth = new \CSocServGoogleOAuth($userId);
72 $oAuth->getEntityOAuth()->addScope(
74 'https://www.googleapis.com/auth/calendar',
75 'https://www.googleapis.com/auth/calendar.readonly',
79 $oAuth->getEntityOAuth()->setUser($userId);
81 if ($oAuth->getEntityOAuth()->GetAccessToken())
83 $httpClient->setHeader(
'Authorization',
'Bearer ' . $oAuth->getEntityOAuth()->getToken());
84 $httpClient->setHeader(
'Content-Type',
'application/json');
85 $httpClient->setHeader(
'Referer', Helper::getDomain());
93 self::$httpClients[
$userId] =
new HttpQuery($httpClient, $userId);
96 $this->httpClient = self::$httpClients[
$userId];
101 private function deactivateConnection()
104 ->setStatus(
'[401] Unauthorized')
105 ->setLastSyncTime(
new Core\Base\Date())
109 $mapperFactory = ServiceLocator::getInstance()->get(
'calendar.service.mappers.factory');
110 $mapperFactory->getConnection()->update($this->connection);
116 'type' => $this->connection->getAccountType(),
118 'id' => $this->connection->getId(),
131 return $this->httpClient->getStatus() === 200;
136 $acceptedCodes = [200, 201, 204, 404];
138 return in_array($this->httpClient->getStatus(), $acceptedCodes);
150 $this->deactivateConnection();
__construct(Connection $connection, int $userId)
handleUnauthorize(Connection $connection)
static addPullEvent(string $command, int $userId, array $params=[])