Skip to content

Commit 25ff787

Browse files
Merge pull request #22 from evertharmeling/develop
Support use of Guzzle 7
2 parents a62ffaf + e094997 commit 25ff787

5 files changed

Lines changed: 72 additions & 60 deletions

File tree

CHANGELOG.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
11
# CHANGELOG
22

3+
## 4.0.1
4+
5+
### Added
6+
7+
* `psr/http-client` to support direct use of `Guzzle 7` by using the `ClientInterface`.
8+
Change is BC because `Http\Client\HttpClient` implements the same interface.
9+
10+
## 4.0.0
11+
12+
### Added
13+
14+
* `getPostcodeDataByPostcode` function for `/postcodes/{postalCode}` endpoint.
15+
* Bumped PHPUnit version
16+
17+
### Removed
18+
19+
* Support for PHP <7.1
20+
321
## 3.0.0
422

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

27+
### Removed
28+
29+
* Support direct usage of Guzzle, now handled via [HTTPPlug](http://httplug.io/), see README.
30+
831
## 2.0.0
932

1033
* Rewrite to support postcodeapi.nu version 2
1134

1235
## 1.0.2 (2015-06-11)
1336

14-
* Added call for type = P4 support
37+
* Added call for type = P4 support
1538

1639
## 1.0.1 (2013-08-06)
1740

README.md

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ by [Freshheads](https://www.freshheads.com) and will be maintained in sync with
1111
* [More information](https://www.postcodeapi.nu)
1212
* [API documentation](https://swaggerhub.com/api/apiwise/postcode-api)
1313

14-
Requirements
15-
------------
16-
17-
FHPostcodeAPIClient works with PHP 7.1 and up. This library depends on the [HTTPPlug](http://httplug.io/), see http://docs.php-http.org/en/latest/httplug/introduction.html.
18-
1914
Installation
2015
------------
2116

@@ -36,15 +31,13 @@ require_once 'vendor/autoload.php';
3631

3732
// initiate client
3833
$apiKey = 'replace_with_your_own_api_key';
39-
// In this example we made use of the Guzzle6 as HTTPClient in combination with an HTTPPlug compatible adapter.
34+
// In this example we made use of the Guzzle as HTTPClient.
4035
$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-
)
36+
new GuzzleHttp\Client([
37+
'headers' => [
38+
'X-Api-Key' => $apiKey
39+
]
40+
])
4841
);
4942

5043
// call endpoints
@@ -56,17 +49,31 @@ $response = $client->getPostcode('5041EB');
5649
$response = $client->getPostcodes('51.566405', '5.077171');
5750
```
5851

59-
Note that to be able to run the example above you should have ran the following command, to have Guzzle6 and the Adapter available.
52+
Within a Symfony project
53+
----------------------
6054

61-
```bash
62-
composer require php-http/guzzle6-adapter
63-
```
55+
We recommend to use [Guzzle](https://github.com/guzzle/guzzle), to be able to use Guzzle in combination with the PostcodeApiClient.
56+
Following definition is used with an implementation of `Guzzle 7`.
6457

65-
Within Symfony project
66-
----------------------
58+
```yaml
59+
_defaults:
60+
autowire: true
61+
autoconfigure: true
62+
63+
project.http.client.postal_code:
64+
class: GuzzleHttp\Client
65+
bind:
66+
$config: { headers: { X-Api-Key: '%postcode_api_nu.key%' } }
6767

68-
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
69-
[Guzzle6Adapter](https://github.com/php-http/guzzle6-adapter). By running the following command you automatically install Guzzle aswel.
68+
FH\PostcodeAPI\Client:
69+
$httpClient: '@project.http.client.postal_code'
70+
```
71+
72+
You should now be able use the `FH\PostcodeAPI\Client` service to make requests to the PostcodeAPI.
73+
74+
#### Guzzle 6
75+
To make use of `Guzzle 6`, you should also make use of the
76+
[Guzzle6Adapter](https://github.com/php-http/guzzle6-adapter). By running the following command you automatically install Guzzle as well.
7077

7178
```bash
7279
composer require php-http/guzzle6-adapter
@@ -92,5 +99,3 @@ services:
9299
FH\PostcodeAPI\Client:
93100
$httpClient: '@project.http.adapter.guzzle.client'
94101
```
95-
96-
You should now be able use the `project.client.postal_code` service to make requests to the PostcodeAPI.

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"type": "library",
44
"description": "Client library for the postcodeapi.nu web service.",
55
"keywords": ["postcode","api","client"],
6-
"homepage": "http://www.postcodeapi.nu",
6+
"homepage": "https://www.postcodeapi.nu",
77
"license": "MIT",
88
"authors": [
99
{
@@ -18,8 +18,8 @@
1818
"require": {
1919
"php": "^7.1",
2020
"ext-json": "*",
21-
"php-http/httplug": "^2.0",
22-
"guzzlehttp/psr7": "^1.3"
21+
"guzzlehttp/psr7": "^1.3",
22+
"psr/http-client": "^1.0"
2323
},
2424
"require-dev": {
2525
"phpunit/phpunit": "^7.5 || ^8.2",
@@ -33,7 +33,7 @@
3333
},
3434
"extra": {
3535
"branch-alias": {
36-
"dev-master": "3.0.x-dev"
36+
"dev-master": "4.x-dev"
3737
}
3838
},
3939
"scripts": {

lib/FH/PostcodeAPI/Client.php

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
use FH\PostcodeAPI\Exception\InvalidApiKeyException;
77
use FH\PostcodeAPI\Exception\ServerErrorException;
88
use GuzzleHttp\Psr7\Request;
9-
use Http\Client\HttpClient;
9+
use Psr\Http\Client\ClientExceptionInterface;
10+
use Psr\Http\Client\ClientInterface;
1011
use Psr\Http\Message\RequestInterface;
1112
use Psr\Http\Message\ResponseInterface;
1213

@@ -18,10 +19,10 @@
1819
*/
1920
class Client
2021
{
21-
const POSTCODES_SORT_DISTANCE = 'distance';
22+
public const POSTCODES_SORT_DISTANCE = 'distance';
2223

2324
/**
24-
* @var null|string
25+
* @var string|null
2526
*/
2627
private $url = 'https://api.postcodeapi.nu';
2728

@@ -31,12 +32,12 @@ class Client
3132
private $version = 'v2';
3233

3334
/**
34-
* @var HttpClient
35+
* @var ClientInterface
3536
*/
3637
private $httpClient;
3738

3839

39-
public function __construct(HttpClient $httpClient, $url = null)
40+
public function __construct(ClientInterface $httpClient, string $url = null)
4041
{
4142
if (null !== $url) {
4243
$this->url = $url;
@@ -100,14 +101,11 @@ public function getPostcodesByCoordinates($latitude, $longitude, $sort = self::P
100101
}
101102

102103
/**
103-
* @param string $path
104-
* @param array $params
105-
*
106104
* @return \stdClass
107105
*
108-
* @throws RequestException
106+
* @throws ClientExceptionInterface
109107
*/
110-
private function get($path, array $params = [])
108+
private function get(string $path, array $params = [])
111109
{
112110
$request = $this->createHttpGetRequest($this->buildUrl($path), $params);
113111

@@ -116,39 +114,28 @@ private function get($path, array $params = [])
116114
return $this->parseResponse($response, $request);
117115
}
118116

119-
/**
120-
* @param string $path
121-
* @return string
122-
*/
123-
private function buildUrl($path)
117+
private function buildUrl(string $path): string
124118
{
125119
return sprintf('%s/%s%s', $this->url, $this->version, $path);
126120
}
127121

128-
/**
129-
* @param string $method
130-
* @param string $url
131-
* @param array $queryParams
132-
*
133-
* @return Request
134-
*/
135-
private function createHttpGetRequest($url, array $params = [])
122+
private function createHttpGetRequest(string $url, array $params = []): Request
136123
{
137124
$url .= (count($params) > 0 ? '?' . http_build_query($params, null, '&', PHP_QUERY_RFC3986) : '');
138125

139126
return new Request('GET', $url);
140127
}
141128

142129
/**
143-
* @param ResponseInterface $response
144-
*
145130
* @return \stdClass
146131
*
147132
* @throws CouldNotParseResponseException
133+
* @throws ServerErrorException
134+
* @throws InvalidApiKeyException
148135
*/
149136
private function parseResponse(ResponseInterface $response, RequestInterface $request)
150137
{
151-
$result = json_decode((string) $response->getBody()->getContents());
138+
$result = json_decode((string) $response->getBody()->getContents(), false);
152139

153140
if (json_last_error() !== JSON_ERROR_NONE) {
154141
throw new CouldNotParseResponseException('Could not parse response', $response);
@@ -157,7 +144,7 @@ private function parseResponse(ResponseInterface $response, RequestInterface $re
157144
if (property_exists($result, 'error')) {
158145
switch ($result->error) {
159146
case 'API key is invalid.':
160-
throw new InvalidApiKeyException();
147+
throw new InvalidApiKeyException('API key is invalid');
161148
case 'An unknown server error occured.':
162149
throw ServerErrorException::fromRequest($request);
163150
}

lib/FH/PostcodeAPI/Exception/CouldNotParseResponseException.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ public function __construct($message, ResponseInterface $response)
2525
$this->response = $response;
2626
}
2727

28-
/**
29-
* @return ResponseInterface
30-
*/
31-
public function getResponse()
28+
public function getResponse(): ResponseInterface
3229
{
3330
return $this->response;
3431
}

0 commit comments

Comments
 (0)