Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/Common/Exception/InvalidServerResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,24 @@
*/
class InvalidServerResponse extends \RuntimeException implements Exception
{
public static function create($query)
/**
* @param string $query
* @param int $code
*
* @return InvalidServerResponse
*/
public static function create($query, $code = 0)
{
return new self(sprintf('The geocoder server returned an invalid response for query "%s". We could not parse it.', $query));
return new self(sprintf('The geocoder server returned an invalid response (%d) for query "%s". We could not parse it.', $code, $query));
}

/**
* @param string $query
*
* @return InvalidServerResponse
*/
public static function emptyResponse($query)
{
return new self(sprintf('The geocoder server returned an empty response for query "%s".', $query));
}
}
32 changes: 32 additions & 0 deletions src/Common/Provider/AbstractHttpProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

namespace Geocoder\Provider;

use Geocoder\Exception\InvalidCredentials;
use Geocoder\Exception\InvalidServerResponse;
use Geocoder\Exception\QuotaExceeded;
use Http\Message\MessageFactory;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Client\HttpClient;
Expand Down Expand Up @@ -39,6 +42,35 @@ public function __construct(HttpClient $client, MessageFactory $factory = null)
$this->messageFactory = $factory;
}

/**
* Get URL and retrun contents.
*
* @param string $url
*
* @return string
*/
protected function getUrlContents($url)
{
$request = $this->getMessageFactory()->createRequest('GET', $url);
$response = $this->getHttpClient()->sendRequest($request);

$statusCode = $response->getStatusCode();
if (401 === $statusCode) {
throw new InvalidCredentials();
} elseif (429 === $statusCode) {
throw new QuotaExceeded();
} elseif ($statusCode >= 300) {
throw InvalidServerResponse::create($url, $statusCode);
}

$body = (string) $response->getBody();
if (empty($body)) {
throw InvalidServerResponse::emptyResponse($url);
}

return $body;
}

/**
* Returns the HTTP adapter.
*
Expand Down
7 changes: 1 addition & 6 deletions src/Provider/GoogleMaps/GoogleMaps.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,13 @@ private function buildQuery($url, $locale)
private function fetchUrl($url, $locale, $limit)
{
$url = $this->buildQuery($url, $locale);
$request = $this->getMessageFactory()->createRequest('GET', $url);
$content = (string) $this->getHttpClient()->sendRequest($request)->getBody();
$content = $this->getUrlContents($url);

// Throw exception if invalid clientID and/or privateKey used with GoogleMapsBusinessProvider
if (strpos($content, "Provided 'signature' is not valid for the provided client ID") !== false) {
throw new InvalidCredentials(sprintf('Invalid client ID / API Key %s', $url));
}

if (empty($content)) {
throw InvalidServerResponse::create($url);
}

$json = json_decode($content);

// API error
Expand Down