Skip to content

Commit bf19113

Browse files
author
Evert Harmeling
committed
Switched to httplug implementation
1 parent 6890c33 commit bf19113

File tree

10 files changed

+189
-73
lines changed

10 files changed

+189
-73
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ php:
44
- 5.5
55
- 5.6
66
- 7.0
7+
- 7.1
78

89
before_script:
9-
- composer install --dev
10+
- composer install
1011

1112
script: phpunit --coverage-text
1213

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# CHANGELOG
22

3-
## 2.1.0
3+
## 3.0.0
44

5+
* Dropped direct usage of Guzzle and now make use of [HTTPPlug](http://httplug.io/), see README.
56
* Added `getPostcodes()` call, to get postcodes based on provided `latitude, longitude`. Note that this call is only available with a premium account.
67

78
## 2.0.0

README.md

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ by [Freshheads](https://www.freshheads.com) and will be maintained in sync with
1414
Requirements
1515
------------
1616

17-
FHPostcodeAPIClient works with PHP 5.4.0 or up. This library is dependent on the awesome [Guzzle](http://guzzlephp.org/) HTTP client library.
17+
FHPostcodeAPIClient works with PHP 5.5.0 or up. This library depends on the [HTTPPlug](http://httplug.io/), see http://docs.php-http.org/en/latest/httplug/introduction.html.
1818

1919
Installation
2020
------------
@@ -36,7 +36,16 @@ require_once 'vendor/autoload.php';
3636

3737
// initiate client
3838
$apiKey = 'replace_with_your_own_api_key';
39-
$client = new \FH\PostcodeAPI\Client(new \GuzzleHttp\Client(), $apiKey);
39+
// In this example we made use of the Guzzle6 as HTTPClient in combination with an HTTPPlug compatible adapter.
40+
$client = new \FH\PostcodeAPI\Client(
41+
new Http\Adapter\Guzzle6\Client(
42+
new GuzzleHttp\Client([
43+
'headers' => [
44+
'X-Api-Key' => $apiKey
45+
]
46+
])
47+
)
48+
);
4049

4150
// call endpoints
4251
$response = $client->getAddresses('5041EB', 21);
@@ -46,4 +55,38 @@ $response = $client->getAddress('0855200000061001');
4655
$response = $client->getPostcodes('51.566405', '5.077171');
4756
```
4857

49-
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/freshheads/fhpostcodeapiclient/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
58+
Note that to be able to run the example above you should have ran the following command, to have Guzzle6 and the Adapter available.
59+
60+
```bash
61+
composer require php-http/guzzle6-adapter
62+
```
63+
64+
Within Symfony project
65+
----------------------
66+
67+
We recommend to use [Guzzle](https://github.com/guzzle/guzzle), to be able to use Guzzle in combination with the PostcodeApiClient you should also make use of the
68+
[Guzzle6Adapter](https://github.com/php-http/guzzle6-adapter). By running the following command you automatically install Guzzle aswel.
69+
70+
```bash
71+
composer require php-http/guzzle6-adapter
72+
```
73+
74+
And add the following service definitions:
75+
```yaml
76+
project.http.guzzle.client:
77+
class: GuzzleHttp\Client
78+
arguments:
79+
- { headers: { X-Api-Key: 'replace_with_your_own_api_key' } }
80+
81+
project.http.adapter.guzzle.client:
82+
class: Http\Adapter\Guzzle6\Client
83+
arguments:
84+
- '@project.http.guzzle.client'
85+
86+
project.client.postal_code:
87+
class: FH\PostcodeAPI\Client
88+
arguments:
89+
- '@project.http.adapter.guzzle.client'
90+
```
91+
92+
You should now be able use the `project.client.postal_code` service to make requests to the PostcodeAPI.

composer.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@
1717
],
1818
"require": {
1919
"php": ">=5.4.0",
20-
"guzzlehttp/guzzle": "^6.0"
20+
"php-http/httplug": "^1.1",
21+
"guzzlehttp/psr7": "^1.3"
2122
},
2223
"require-dev": {
23-
"phpunit/phpunit": "^4.3.5"
24+
"phpunit/phpunit": "^4.3.5|^5.0",
25+
"php-http/mock-client": "^0.3.0"
26+
},
27+
"suggest": {
28+
"php-http/guzzle6-adapter": "An HTTPlug adapter for the Guzzle 6 HTTP client"
2429
},
2530
"autoload": {
2631
"psr-0": { "FH\\PostcodeAPI": "lib/" }
@@ -29,5 +34,8 @@
2934
"branch-alias": {
3035
"dev-master": "3.0.x-dev"
3136
}
37+
},
38+
"scripts": {
39+
"test": "./vendor/phpunit/phpunit/phpunit --coverage-text"
3240
}
3341
}

lib/FH/PostcodeAPI/Client.php

Lines changed: 50 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,43 @@
33
namespace FH\PostcodeAPI;
44

55
use FH\PostcodeAPI\Exception\CouldNotParseResponseException;
6-
use GuzzleHttp\Client as HTTPClient;
7-
use GuzzleHttp\ClientInterface;
8-
use GuzzleHttp\Exception\RequestException;
6+
use FH\PostcodeAPI\Exception\InvalidApiKeyException;
7+
use FH\PostcodeAPI\Exception\ServerErrorException;
98
use GuzzleHttp\Psr7\Request;
9+
use Http\Client\HttpClient;
10+
use Psr\Http\Message\RequestInterface;
1011
use Psr\Http\Message\ResponseInterface;
1112

1213
/**
1314
* Client library for postcodeapi.nu 2.0 web service.
1415
*
1516
* @author Gijs Nieuwenhuis <gijs.nieuwenhuis@freshheads.com>
17+
* @author Evert Harmeling <evert@freshheads.com>
1618
*/
1719
class Client
1820
{
1921
const POSTCODES_SORT_DISTANCE = 'distance';
2022

21-
/**
22-
* @var string
23-
*/
24-
private $uriScheme = 'https://';
25-
2623
/**
2724
* @var null|string
2825
*/
29-
private $domain = 'postcode-api.apiwise.nl';
26+
private $url = 'https://postcode-api.apiwise.nl';
3027

3128
/**
3229
* @var string
3330
*/
34-
private $version = 'v2';
31+
private $version = 'v2';
3532

3633
/**
37-
* @var HTTPClient
34+
* @var HttpClient
3835
*/
3936
private $httpClient;
4037

4138

42-
public function __construct(ClientInterface $httpClient, $domain = null)
39+
public function __construct(HttpClient $httpClient, $url = null)
4340
{
44-
if (null !== $domain) {
45-
$this->domain = $domain;
41+
if (null !== $url) {
42+
$this->url = $url;
4643
}
4744

4845
$this->httpClient = $httpClient;
@@ -94,39 +91,28 @@ public function getPostcodesByCoordinates($latitude, $longitude, $sort = self::P
9491

9592
/**
9693
* @param string $path
97-
* @param array $queryParams
94+
* @param array $params
9895
*
9996
* @return \stdClass
10097
*
10198
* @throws RequestException
10299
*/
103-
private function get($path, array $queryParams = array())
100+
private function get($path, array $params = [])
104101
{
105-
$request = $this->createHttpRequest('GET', sprintf('%s%s/%s%s', $this->uriScheme, $this->domain, $this->version, $path),
106-
$queryParams
107-
);
102+
$request = $this->createHttpGetRequest($this->buildUrl($path), $params);
108103

109-
$response = $this->httpClient->send($request);
104+
$response = $this->httpClient->sendRequest($request);
110105

111-
return $this->parseResponse($response);
106+
return $this->parseResponse($response, $request);
112107
}
113108

114109
/**
115-
* @param ResponseInterface $response
116-
*
117-
* @return \stdClass
118-
*
119-
* @throws CouldNotParseResponseException
110+
* @param string $path
111+
* @return string
120112
*/
121-
private function parseResponse(ResponseInterface $response)
113+
private function buildUrl($path)
122114
{
123-
$out = json_decode((string) $response->getBody());
124-
125-
if (json_last_error() !== JSON_ERROR_NONE) {
126-
throw new CouldNotParseResponseException('Could not parse response', $response);
127-
}
128-
129-
return $out;
115+
return sprintf('%s/%s%s', $this->url, $this->version, $path);
130116
}
131117

132118
/**
@@ -136,10 +122,37 @@ private function parseResponse(ResponseInterface $response)
136122
*
137123
* @return Request
138124
*/
139-
private function createHttpRequest($method, $url, array $queryParams = array())
125+
private function createHttpGetRequest($url, array $params = [])
140126
{
141-
$url = $url . (count($queryParams) > 0 ? '?' . http_build_query($queryParams) : '');
127+
$url .= (count($params) > 0 ? '?' . http_build_query($params, null, '&', PHP_QUERY_RFC3986) : '');
128+
129+
return new Request('GET', $url);
130+
}
131+
132+
/**
133+
* @param ResponseInterface $response
134+
*
135+
* @return \stdClass
136+
*
137+
* @throws CouldNotParseResponseException
138+
*/
139+
private function parseResponse(ResponseInterface $response, RequestInterface $request)
140+
{
141+
$result = json_decode((string) $response->getBody()->getContents());
142+
143+
if (json_last_error() !== JSON_ERROR_NONE) {
144+
throw new CouldNotParseResponseException('Could not parse response', $response);
145+
}
146+
147+
if (property_exists($result, 'error')) {
148+
switch ($result->error) {
149+
case 'API key is invalid.':
150+
throw new InvalidApiKeyException();
151+
case 'An unknown server error occured.':
152+
throw ServerErrorException::fromRequest($request);
153+
}
154+
}
142155

143-
return new Request($method, $url);
156+
return $result;
144157
}
145158
}

lib/FH/PostcodeAPI/Exception/CouldNotParseResponseException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* @author Gijs Nieuwenhuis <gijs.nieuwenhuis@freshheads.com>
99
*/
10-
final class CouldNotParseResponseException extends \Exception
10+
final class CouldNotParseResponseException extends \Exception implements PostcodeApiExceptionInterface
1111
{
1212
/**
1313
* @var ResponseInterface
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace FH\PostcodeAPI\Exception;
4+
5+
/**
6+
* @author Evert Harmeling <evert@freshheads.com>
7+
*/
8+
class InvalidApiKeyException extends \Exception implements PostcodeApiExceptionInterface
9+
{
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace FH\PostcodeAPI\Exception;
4+
5+
/**
6+
* @author Evert Harmeling <evert@freshheads.com>
7+
*/
8+
interface PostcodeApiExceptionInterface
9+
{
10+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace FH\PostcodeAPI\Exception;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
7+
/**
8+
* @author Evert Harmeling <evert@freshheads.com>
9+
*/
10+
class ServerErrorException extends \Exception implements PostcodeApiExceptionInterface
11+
{
12+
/**
13+
* @var RequestInterface
14+
*/
15+
private $request;
16+
17+
/**
18+
* @param RequestInterface $request
19+
* @return ServerErrorException
20+
*/
21+
public function setRequest($request)
22+
{
23+
$this->request = $request;
24+
25+
return $this;
26+
}
27+
28+
/**
29+
* @return RequestInterface
30+
*/
31+
public function getRequest()
32+
{
33+
return $this->request;
34+
}
35+
36+
/**
37+
* @param RequestInterface $request
38+
* @return ServerErrorException
39+
*/
40+
public static function fromRequest(RequestInterface $request)
41+
{
42+
$self = new self();
43+
$self->setRequest($request);
44+
45+
return $self;
46+
}
47+
}

0 commit comments

Comments
 (0)