-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from evertharmeling/postcodes
Make use of HTTPlug and implemented the /postcodes call
- Loading branch information
Showing
10 changed files
with
264 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,63 +3,58 @@ | |
namespace FH\PostcodeAPI; | ||
|
||
use FH\PostcodeAPI\Exception\CouldNotParseResponseException; | ||
use GuzzleHttp\Client as HTTPClient; | ||
use GuzzleHttp\ClientInterface; | ||
use GuzzleHttp\Exception\RequestException; | ||
use GuzzleHttp\Message\Request; | ||
use GuzzleHttp\Message\ResponseInterface; | ||
use FH\PostcodeAPI\Exception\InvalidApiKeyException; | ||
use FH\PostcodeAPI\Exception\ServerErrorException; | ||
use GuzzleHttp\Psr7\Request; | ||
use Http\Client\HttpClient; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* Client library for postcodeapi.nu 2.0 web service. | ||
* | ||
* @author Gijs Nieuwenhuis <[email protected]> | ||
* @author Evert Harmeling <[email protected]> | ||
*/ | ||
class Client | ||
{ | ||
/** @var string */ | ||
const BASE_URI = 'https://postcode-api.apiwise.nl'; | ||
const POSTCODES_SORT_DISTANCE = 'distance'; | ||
|
||
/** | ||
* @var HTTPClient | ||
* @var null|string | ||
*/ | ||
private $httpClient; | ||
private $url = 'https://postcode-api.apiwise.nl'; | ||
|
||
/** | ||
* @param ClientInterface $httpClient | ||
* @param string $apiKey Required API key for authenticating client | ||
* @var string | ||
*/ | ||
public function __construct(ClientInterface $httpClient, $apiKey) | ||
{ | ||
$this->httpClient = $this->prepareClient($httpClient, $apiKey); | ||
} | ||
private $version = 'v2'; | ||
|
||
/** | ||
* @param ClientInterface $client | ||
* @param string $apiKey | ||
* | ||
* @return HTTPClient | ||
* @var HttpClient | ||
*/ | ||
private function prepareClient(ClientInterface $client, $apiKey) | ||
private $httpClient; | ||
|
||
|
||
public function __construct(HttpClient $httpClient, $url = null) | ||
{ | ||
if ($client->getDefaultOption('timeout') === null) { | ||
$client->setDefaultOption('timeout', 5.0); | ||
if (null !== $url) { | ||
$this->url = $url; | ||
} | ||
|
||
$client->setDefaultOption('headers/X-Api-Key', $apiKey); | ||
|
||
return $client; | ||
$this->httpClient = $httpClient; | ||
} | ||
|
||
/** | ||
* @param string|null $postcode | ||
* @param string|null $number | ||
* @param int $from | ||
* | ||
* @return \StdClass | ||
* @return \stdClass | ||
*/ | ||
public function getAddresses($postcode = null, $number = null, $from = 0) | ||
{ | ||
return $this->get('/v2/addresses/', [ | ||
return $this->get('/addresses/', [ | ||
'postcode' => $postcode, | ||
'number' => $number, | ||
'from' => $from | ||
|
@@ -69,61 +64,95 @@ public function getAddresses($postcode = null, $number = null, $from = 0) | |
/** | ||
* @param string $id | ||
* | ||
* @return \StdClass | ||
* @return \stdClass | ||
*/ | ||
public function getAddress($id) | ||
{ | ||
return $this->get("/v2/addresses/{$id}"); | ||
return $this->get(sprintf('/addresses/%s', $id)); | ||
} | ||
|
||
/** | ||
* @param string $latitude | ||
* @param string $longitude | ||
* @param string $sort | ||
* | ||
* @return \stdClass | ||
*/ | ||
public function getPostcodesByCoordinates($latitude, $longitude, $sort = self::POSTCODES_SORT_DISTANCE) | ||
{ | ||
return $this->get('/postcodes/', [ | ||
'coords' => [ | ||
'latitude' => $latitude, | ||
'longitude' => $longitude | ||
], | ||
'sort' => $sort | ||
]); | ||
} | ||
|
||
/** | ||
* @param string $path | ||
* @param array $queryParams | ||
* @param array $params | ||
* | ||
* @return \StdClass | ||
* @return \stdClass | ||
* | ||
* @throws RequestException | ||
*/ | ||
private function get($path, array $queryParams = array()) | ||
private function get($path, array $params = []) | ||
{ | ||
$url = self::BASE_URI . $path; | ||
$request = $this->createHttpGetRequest($this->buildUrl($path), $params); | ||
|
||
$request = $this->createHttpRequest('GET', $url, $queryParams); | ||
$response = $this->httpClient->sendRequest($request); | ||
|
||
$response = $this->httpClient->send($request); | ||
|
||
return $this->parseResponse($response); | ||
return $this->parseResponse($response, $request); | ||
} | ||
|
||
/** | ||
* @param ResponseInterface $response | ||
* | ||
* @return \StdClass | ||
* | ||
* @throws CouldNotParseResponseException | ||
* @param string $path | ||
* @return string | ||
*/ | ||
private function parseResponse(ResponseInterface $response) | ||
private function buildUrl($path) | ||
{ | ||
$out = json_decode((string) $response->getBody()); | ||
|
||
if (json_last_error() !== JSON_ERROR_NONE) { | ||
throw new CouldNotParseResponseException('Could not parse resonse', $response); | ||
} | ||
|
||
return $out; | ||
return sprintf('%s/%s%s', $this->url, $this->version, $path); | ||
} | ||
|
||
/** | ||
* @param string $method | ||
* @param string $path | ||
* @param string $url | ||
* @param array $queryParams | ||
* | ||
* @return Request | ||
*/ | ||
private function createHttpRequest($method, $path, array $queryParams = array()) | ||
private function createHttpGetRequest($url, array $params = []) | ||
{ | ||
$path = $path . (count($queryParams) > 0 ? '?' . http_build_query($queryParams) : ''); | ||
$url .= (count($params) > 0 ? '?' . http_build_query($params, null, '&', PHP_QUERY_RFC3986) : ''); | ||
|
||
return new Request('GET', $url); | ||
} | ||
|
||
/** | ||
* @param ResponseInterface $response | ||
* | ||
* @return \stdClass | ||
* | ||
* @throws CouldNotParseResponseException | ||
*/ | ||
private function parseResponse(ResponseInterface $response, RequestInterface $request) | ||
{ | ||
$result = json_decode((string) $response->getBody()->getContents()); | ||
|
||
if (json_last_error() !== JSON_ERROR_NONE) { | ||
throw new CouldNotParseResponseException('Could not parse response', $response); | ||
} | ||
|
||
if (property_exists($result, 'error')) { | ||
switch ($result->error) { | ||
case 'API key is invalid.': | ||
throw new InvalidApiKeyException(); | ||
case 'An unknown server error occured.': | ||
throw ServerErrorException::fromRequest($request); | ||
} | ||
} | ||
|
||
return $this->httpClient->createRequest($method, $path); | ||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,12 @@ | |
|
||
namespace FH\PostcodeAPI\Exception; | ||
|
||
use GuzzleHttp\Message\ResponseInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* @author Gijs Nieuwenhuis <[email protected]> | ||
*/ | ||
final class CouldNotParseResponseException extends \Exception | ||
final class CouldNotParseResponseException extends \Exception implements PostcodeApiExceptionInterface | ||
{ | ||
/** | ||
* @var ResponseInterface | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace FH\PostcodeAPI\Exception; | ||
|
||
/** | ||
* @author Evert Harmeling <[email protected]> | ||
*/ | ||
class InvalidApiKeyException extends \Exception implements PostcodeApiExceptionInterface | ||
{ | ||
} |
10 changes: 10 additions & 0 deletions
10
lib/FH/PostcodeAPI/Exception/PostcodeApiExceptionInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace FH\PostcodeAPI\Exception; | ||
|
||
/** | ||
* @author Evert Harmeling <[email protected]> | ||
*/ | ||
interface PostcodeApiExceptionInterface | ||
{ | ||
} |
Oops, something went wrong.