protected function getResult($userAgent, array $headers)
{
/*
* an empty UserAgent makes no sense
*/
if ($userAgent == '') {
throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
}
$parameters = '/' . $this->apiKey;
$parameters .= '/' . rawurlencode($userAgent);
$uri = self::$uri . $parameters;
$request = new Request('GET', $uri);
try {
$response = $this->getResponse($request);
} catch (Exception\RequestException $ex) {
/* @var $prevEx \GuzzleHttp\Exception\ClientException */
$prevEx = $ex->getPrevious();
if ($prevEx->hasResponse() === true && $prevEx->getResponse()->getStatusCode() === 400) {
$content = $prevEx->getResponse()->getBody()->getContents();
$content = json_decode($content);
/*
* Error
*/
if (isset($content->error->code) && $content->error->code == 'key_invalid') {
throw new Exception\InvalidCredentialsException('Your API key "' . $this->apiKey . '" is not valid for ' . $this->getName(), null, $ex);
}
if (isset($content->error->code) && $content->error->code == 'useragent_invalid') {
throw new Exception\RequestException('User agent is invalid "' . $userAgent . '"');
}
}
throw $ex;
}
/*
* no json returned?
*/
$contentType = $response->getHeader('Content-Type');
if (!isset($contentType[0]) || $contentType[0] != 'application/json') {
throw new Exception\RequestException('Could not get valid "application/json" response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"');
}
$content = json_decode($response->getBody()->getContents());
/*
* No result
*/
if (isset($content->error->code) && $content->error->code == 'useragent_not_found') {
throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
}
/*
* Missing data?
*/
if (!$content instanceof stdClass || !isset($content->data)) {
throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Data is missing "' . $response->getBody()->getContents() . '"');
}
return $content->data;
}