31 if ($this->apiKey ===
'')
34 $result->addError(
new Error(
'Missing API key when requesting a service.'));
38 $url = $this->apiEndpoint . $method;
42 'socketTimeout' => $this->socketTimeout,
43 'streamTimeout' => $this->streamTimeout,
44 'waitResponse' =>
true,
47 $httpClient->setHeader(
'User-Agent', static::USER_AGENT);
48 $httpClient->setHeader(
'Content-type', static::CONTENT_TYPE);
49 $httpClient->setHeader(
'X-API-KEY', $this->apiKey);
50 $httpClient->setCharset(static::CHARSET);
52 if (is_array($requestParams))
55 $requestParams = Json::encode($requestParams);
59 $result->setHttpRequest(
new DTO\
Request([
60 'method' => $queryMethod,
62 'headers' => method_exists($httpClient,
'getRequestHeaders') ? $httpClient->getRequestHeaders()->toArray() : [],
63 'body' => $requestParams
68 if ($httpClient->query($queryMethod, $url, $requestParams))
72 if ($httpClient->getStatus() !== 200)
75 'code' => $httpClient->getStatus(),
82 $error = $httpClient->getError();
84 'code' => key($error),
85 'error' => current($error),
88 $result->setHttpResponse(
new DTO\
Response([
89 'statusCode' => $httpClient->getStatus(),
90 'headers' => $httpClient->getHeaders()->toArray(),
91 'body' => $httpClient->getResult(),
95 $result->setData($answer);
97 if (array_key_exists(
'code', $errorInfo) && $errorInfo[
'code'] !==
'ok')
99 $result->addError(
new Error($errorInfo[
'error'], $errorInfo[
'code'], $errorInfo));