public function request(string $method, string $uri, array $params): array
{
$getLogContext = static function (int $statusCode, $response, string $error = '' )
use ($method, $uri, $params): array
{
return [
'serviceName' => Helper::ACCOUNT_TYPE,
'host' => Helper::SERVER_PATH,
'method' => $method,
'url' => $uri,
'requestParams' => $params,
'statusCode' => $statusCode,
'error' => $error,
'response' => $response,
];
};
$this->getStatus()->resetErrors();
try
{
$response = [];
$paramString = $this->prepareParams($params);
$uri = Helper::SERVER_PATH . $uri;
$this->httpClient->waitResponse(true);
$this->httpClient->query($method, $uri, $paramString);
if ($this->httpClient->getStatus() < 300)
{
$response = $this->prepareResponse();
$this->context->getLogger()
->debug("API office365 success" . $this->httpClient->getStatus()
, $getLogContext(
$this->httpClient->getStatus(),
$this->httpClient->getResult(),
)
);
}
else
{
try
{
$error = Json::decode($this->httpClient->getResult());
$this->getStatus()->addError(
"CONNECTION",
"[" . $error['error']['code'] . "] " . $error['error']['message'],
);
$this->context->getLogger()
->warning("API office365 returned error code "
. $this->httpClient->getStatus()
. ": " . $error['error']['message'],
$getLogContext(
$this->httpClient->getStatus(),
$this->httpClient->getResult(),
$error['error']['message']
)
);
switch ($this->httpClient->getStatus())
{
case 401:
throw new AuthException(
$error['error']['code'],
401,
__FILE__,
__LINE__
);
case 404:
throw new NotFoundException(
$error['error']['code'],
404,
__FILE__,
__LINE__
);
case 409:
throw new ConflictException(
$error['error']['code'],
409,
__FILE__,
__LINE__
);
case 410:
throw new GoneException(
$error['error']['code'],
410,
__FILE__,
__LINE__
);
default:
throw new ApiException(
$error['error']['code'],
$this->httpClient->getStatus(),
__FILE__,
__LINE__
);
}
}
catch (ArgumentException $exception)
{
$this->context->getLogger()
->error("ArgumentException from office365", $getLogContext(
$this->httpClient->getStatus(),
$this->httpClient->getResult(),
$exception->getMessage()
));
foreach($this->httpClient->getError() as $code => $error)
{
$this->getStatus()->addError($code, $error);
}
}
}
}
catch (ApiException $e)
{
$this->context->getLogger()
->error("ApiException from office365", $getLogContext(
$e->getCode(),
'',
$e->getMessage()
)
);
throw $e;
}
catch (Exception $e)
{
$this->context->getLogger()
->error("Exception from office365: " . $e->getMessage(), $getLogContext(
$e->getCode(),
'',
$e->getMessage()
)
);
throw $e;
}
return $response;
}