Skip to content

Commit 2cf66db

Browse files
authored
Expect a PSR-18 client instead of a PHP-HTTP client (#1110)
* Expect a PSR-18 client instead of a PHP-HTTP client * Update integration tests
1 parent 95ac2b2 commit 2cf66db

File tree

78 files changed

+289
-292
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+289
-292
lines changed

README.md

+16-16
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ Read more about HTTPlug in [their docs](http://docs.php-http.org/en/latest/httpl
5050

5151
### Summary (Just give me the command)
5252

53-
To install Google Maps geocoder with Guzzle 6 you may run the following command:
53+
To install Google Maps geocoder with Guzzle 7 you may run the following command:
5454

5555
```cmd
56-
composer require geocoder-php/google-maps-provider php-http/guzzle6-adapter
56+
composer require geocoder-php/google-maps-provider guzzlehttp/guzzle
5757
```
5858

59-
Or using the curl client (you'll need to provide a PSR7 implementation such as `nyholm/psr7` if not using guzzle)
59+
Or using the curl client (you'll need to provide a PSR7 implementation such as `nyholm/psr7` if not using Guzzle)
6060

6161
```cmd
6262
composer require geocoder-php/google-maps-provider php-http/curl-client nyholm/psr7
@@ -81,13 +81,13 @@ We have a small cookbook where you can find examples on common use cases:
8181

8282
## Usage
8383

84-
In the code snippet below we use GoogleMaps and Guzzle6.
84+
In the code snippet below we use GoogleMaps and Guzzle 7.
8585

8686
```php
8787
use Geocoder\Query\GeocodeQuery;
8888
use Geocoder\Query\ReverseQuery;
8989

90-
$httpClient = new \Http\Adapter\Guzzle6\Client();
90+
$httpClient = new \GuzzleHttp\Client();
9191
$provider = new \Geocoder\Provider\GoogleMaps\GoogleMaps($httpClient, null, 'your-api-key');
9292
$geocoder = new \Geocoder\StatefulGeocoder($provider, 'en');
9393

@@ -203,13 +203,13 @@ when a provider returns a result. The result is returned by `GoogleMaps` because
203203
use Geocoder\Query\GeocodeQuery;
204204

205205
$geocoder = new \Geocoder\ProviderAggregator();
206-
$adapter = new \Http\Adapter\Guzzle6\Client();
206+
$client = new \GuzzleHttp\Client();
207207

208208
$chain = new \Geocoder\Provider\Chain\Chain([
209-
new \Geocoder\Provider\FreeGeoIp\FreeGeoIp($adapter),
210-
new \Geocoder\Provider\HostIp\HostIp($adapter),
211-
new \Geocoder\Provider\GoogleMaps\GoogleMaps($adapter, 'France'),
212-
new \Geocoder\Provider\BingMaps\BingMaps($adapter, '<API_KEY>'),
209+
new \Geocoder\Provider\FreeGeoIp\FreeGeoIp($client),
210+
new \Geocoder\Provider\HostIp\HostIp($client),
211+
new \Geocoder\Provider\GoogleMaps\GoogleMaps($client, 'France'),
212+
new \Geocoder\Provider\BingMaps\BingMaps($client, '<API_KEY>'),
213213
// ...
214214
]);
215215

@@ -230,15 +230,15 @@ decide which provider to use later on.
230230
use Geocoder\Query\GeocodeQuery;
231231
use Geocoder\Query\ReverseQuery;
232232

233-
$adapter = new \Http\Adapter\Guzzle6\Client();
233+
$client = new \GuzzleHttp\Client();
234234
$geocoder = new \Geocoder\ProviderAggregator();
235235

236236
$geocoder->registerProviders([
237-
new \Geocoder\Provider\GoogleMaps\GoogleMaps($adapter),
238-
new \Geocoder\Provider\GoogleMaps\GoogleMapsBusiness($adapter, '<CLIENT_ID>'),
239-
new \Geocoder\Provider\Yandex\Yandex($adapter),
240-
new \Geocoder\Provider\MaxMind\MaxMind($adapter, '<MAXMIND_API_KEY>'),
241-
new \Geocoder\Provider\ArcGISOnline\ArcGISOnline($adapter),
237+
new \Geocoder\Provider\GoogleMaps\GoogleMaps($client),
238+
new \Geocoder\Provider\GoogleMaps\GoogleMapsBusiness($client, '<CLIENT_ID>'),
239+
new \Geocoder\Provider\Yandex\Yandex($client),
240+
new \Geocoder\Provider\MaxMind\MaxMind($client, '<MAXMIND_API_KEY>'),
241+
new \Geocoder\Provider\ArcGISOnline\ArcGISOnline($client),
242242
]);
243243

244244
$geocoder->registerProvider(new \Geocoder\Provider\Nominatim\Nominatim($adapter, 'https://your.nominatim.server'));

composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
"require": {
2020
"php": "^7.4 || ^8.0",
2121
"igorw/get-in": "^1.0",
22-
"php-http/client-implementation": "^1.0",
2322
"php-http/discovery": "^1.4",
24-
"php-http/httplug": "^2.2",
2523
"php-http/message-factory": "^1.0.2",
2624
"php-http/promise": "^1.0",
25+
"psr/http-client": "^1.0",
26+
"psr/http-client-implementation": "^1.0",
2727
"psr/http-message-implementation": "^1.0",
2828
"psr/log": "^1.0|^2.0|^3.0",
2929
"psr/simple-cache": "^1.0|^2.0|^3.0"

docs/cookbook/cache.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Caching responses
22

3-
Many of the APIs are not free so it is a good idea to cache the responses so you
4-
are not paying for the same information twice. The caching is out of scope for this
3+
Many of the APIs are not free so it is a good idea to cache the responses so you
4+
are not paying for the same information twice. The caching is out of scope for this
55
library but we will show you an example how to properly cache responses with the
66
HTTPlug [cache plugin](http://php-http.readthedocs.io/en/latest/plugins/cache.html):
77

88
```php
99
use Cache\Adapter\Redis\RedisCachePool;
10-
use Http\Adapter\Guzzle6\Client as GuzzleClient;
10+
use GuzzleHttp\Client as GuzzleClient;
1111
use Http\Client\Common\PluginClient;
1212
use Geocoder\Provider\GoogleMaps;
1313

@@ -23,8 +23,8 @@ $cachePlugin = new CachePlugin($pool, StreamFactoryDiscovery::find(), [
2323
'default_ttl' => null,
2424
'cache_lifetime' => 86400*365
2525
]);
26-
27-
$adapter = new GuzzleClient();
26+
27+
$adapter = new GuzzleClient();
2828
$pluginClient = new PluginClient($adapter, [$cachePlugin]);
2929

3030
// Get a geocoder

docs/cookbook/http-client.md

+9-12
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,25 @@
22

33
The Geocoder is decoupled from the HTTP client that sends the HTTP messages. This means
44
that you are responsible for configuring the HTTP client. Usually the default configuration
5-
is good enough but sometime you may want to do something differently.
5+
is good enough but sometime you may want to do something differently.
66

77
How you configure the client differs between different clients below are two examples,
8-
one with [Guzzle6 client](https://github.com/guzzle/guzzle) and one with the
8+
one with [Guzzle 7 client](https://github.com/guzzle/guzzle) and one with the
99
[cURL client](https://github.com/php-http/curl-client).
1010

11-
## Guzzle6
11+
## Guzzle 7
1212

1313
```php
14-
use GuzzleHttp\Client as GuzzleClient;
15-
use Http\Adapter\Guzzle6\Client;
14+
use GuzzleHttp\Client;
1615
use Geocoder\Provider\GoogleMaps;
1716

1817
$config = [
1918
'timeout' => 2.0,
2019
'verify' => false,
2120
];
22-
$guzzle = new GuzzleClient($config);
2321

24-
$adapter = new Client($guzzle);
25-
$geocoder = new GoogleMaps($adapter);
22+
$client = new Client($config);
23+
$geocoder = new GoogleMaps($client);
2624

2725
$geocoder->geocode(...);
2826
```
@@ -35,13 +33,12 @@ use Http\Client\Curl\Client;
3533
use Geocoder\Provider\GoogleMaps;
3634

3735
$options = [
38-
CURLOPT_CONNECTTIMEOUT => 2,
36+
CURLOPT_CONNECTTIMEOUT => 2,
3937
CURLOPT_SSL_VERIFYPEER => false,
4038
];
4139

42-
$adapter = new Client(null, null, $options);
43-
$geocoder = new GoogleMaps($adapter);
40+
$client = new Client(null, null, $options);
41+
$geocoder = new GoogleMaps($client);
4442

4543
$geocoder->geocode(...);
4644
```
47-

docs/cookbook/rate-limiting.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ $stack = \GuzzleHttp\HandlerStack::create();
2020
$stack->push(\Spatie\GuzzleRateLimiterMiddleware\RateLimiterMiddleware::perSecond(1));
2121

2222
$httpClient = new \GuzzleHttp\Client(['handler' => $stack, 'timeout' => 30.0]);
23-
$httpAdapter = new \Http\Adapter\Guzzle6\Client($httpClient);
2423
$psr6Cache = new \Cache\Adapter\PHPArray\ArrayCachePool();
25-
$provider = new \Geocoder\Provider\Nominatim\Nominatim($httpAdapter, 'https://nominatim.openstreetmap.org', 'Geocoder test');
24+
$provider = new \Geocoder\Provider\Nominatim\Nominatim($httpClient, 'https://nominatim.openstreetmap.org', 'Geocoder test');
2625
$cachedProvider = new \Geocoder\Provider\Cache\ProviderCache($provider, $psr6Cache);
2726
$geocoder = new \Geocoder\StatefulGeocoder($cachedProvider, 'en');
2827

src/Http/Provider/AbstractHttpProvider.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Geocoder\Provider\AbstractProvider;
1919
use Http\Message\MessageFactory;
2020
use Http\Discovery\MessageFactoryDiscovery;
21-
use Http\Client\HttpClient;
21+
use Psr\Http\Client\ClientInterface;
2222
use Psr\Http\Message\RequestInterface;
2323

2424
/**
@@ -28,7 +28,7 @@
2828
abstract class AbstractHttpProvider extends AbstractProvider
2929
{
3030
/**
31-
* @var HttpClient
31+
* @var ClientInterface
3232
*/
3333
private $client;
3434

@@ -38,10 +38,10 @@ abstract class AbstractHttpProvider extends AbstractProvider
3838
private $messageFactory;
3939

4040
/**
41-
* @param HttpClient $client
41+
* @param ClientInterface $client
4242
* @param MessageFactory|null $factory
4343
*/
44-
public function __construct(HttpClient $client, MessageFactory $factory = null)
44+
public function __construct(ClientInterface $client, MessageFactory $factory = null)
4545
{
4646
$this->client = $client;
4747
$this->messageFactory = $factory ?: MessageFactoryDiscovery::find();
@@ -106,9 +106,9 @@ protected function getParsedResponse(RequestInterface $request): string
106106
/**
107107
* Returns the HTTP adapter.
108108
*
109-
* @return HttpClient
109+
* @return ClientInterface
110110
*/
111-
protected function getHttpClient(): HttpClient
111+
protected function getHttpClient(): ClientInterface
112112
{
113113
return $this->client;
114114
}

src/Http/Tests/Provider/AbstractHttpProviderTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Geocoder\Model\AddressCollection;
1818
use Geocoder\Query\GeocodeQuery;
1919
use Geocoder\Query\ReverseQuery;
20-
use Http\Client\HttpClient;
20+
use Psr\Http\Client\ClientInterface;
2121
use Http\Mock\Client;
2222
use PHPUnit\Framework\TestCase;
2323

@@ -33,7 +33,7 @@ public function testHttpClientGetter()
3333

3434
class DummyProvider extends AbstractHttpProvider
3535
{
36-
public function getHttpClient(): HttpClient
36+
public function getHttpClient(): ClientInterface
3737
{
3838
return parent::getHttpClient();
3939
}

src/Provider/AlgoliaPlaces/AlgoliaPlaces.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use Geocoder\Provider\Provider;
2323
use Geocoder\Query\GeocodeQuery;
2424
use Geocoder\Query\ReverseQuery;
25-
use Http\Client\HttpClient;
25+
use Psr\Http\Client\ClientInterface;
2626
use Psr\Http\Message\RequestInterface;
2727

2828
class AlgoliaPlaces extends AbstractHttpProvider implements Provider
@@ -53,10 +53,10 @@ class AlgoliaPlaces extends AbstractHttpProvider implements Provider
5353
/** @var GeocodeQuery */
5454
private $query;
5555

56-
/** @var HttpClient */
56+
/** @var ClientInterface */
5757
private $client;
5858

59-
public function __construct(HttpClient $client, string $apiKey = null, string $appId = null)
59+
public function __construct(ClientInterface $client, string $apiKey = null, string $appId = null)
6060
{
6161
parent::__construct($client);
6262

src/Provider/AlgoliaPlaces/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
1010

1111
This is the Algolia Places provider from the PHP Geocoder. This is a **READ ONLY** repository. See the
12-
[main repo](https://github.com/geocoder-php/Geocoder) for information and documentation.
12+
[main repo](https://github.com/geocoder-php/Geocoder) for information and documentation.
1313

1414
## Install
1515

@@ -34,11 +34,11 @@ You should set a locale on the query. If it is missing, results may not be as co
3434
use Geocoder\Query\GeocodeQuery;
3535
use Geocoder\Query\ReverseQuery;
3636

37-
$httpClient = new \Http\Adapter\Guzzle6\Client();
37+
$httpClient = new \GuzzleHttp\Client();
3838

3939
// Unauthenticated
4040
$provider = new \Geocoder\Provider\AlgoliaPlaces\AlgoliaPlaces($httpClient);
41-
// Authenticated
41+
// Authenticated
4242
$provider = new \Geocoder\Provider\AlgoliaPlaces\AlgoliaPlaces($httpClient, '<your-key>', '<your-app-id>');
4343

4444
$geocoder = new \Geocoder\StatefulGeocoder($provider, 'en');
@@ -48,5 +48,5 @@ $result = $geocoder->geocodeQuery(GeocodeQuery::create('Paris')->withLocale('fr-
4848

4949
## Contribute
5050

51-
Contributions are very welcome! Send a pull request to the [main repository](https://github.com/geocoder-php/Geocoder) or
51+
Contributions are very welcome! Send a pull request to the [main repository](https://github.com/geocoder-php/Geocoder) or
5252
report any issues you find on the [issue tracker](https://github.com/geocoder-php/Geocoder/issues).

src/Provider/AlgoliaPlaces/Tests/IntegrationTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
use Geocoder\Model\Country;
2222
use Geocoder\Query\GeocodeQuery;
2323
use Geocoder\Query\ReverseQuery;
24-
use Http\Client\HttpClient;
25-
use Http\Discovery\HttpClientDiscovery;
24+
use Http\Discovery\Psr18ClientDiscovery;
25+
use Psr\Http\Client\ClientInterface;
2626

2727
/**
2828
* @author Sébastien Barré <[email protected]>
@@ -35,7 +35,7 @@ class IntegrationTest extends ProviderIntegrationTest
3535

3636
protected $testReverse = false;
3737

38-
protected function createProvider(HttpClient $httpClient)
38+
protected function createProvider(ClientInterface $httpClient)
3939
{
4040
return new AlgoliaPlaces($httpClient, $this->getApiKey(), $this->getAppId());
4141
}
@@ -53,9 +53,9 @@ protected function getCacheDir()
5353
private function getCachedHttpClient()
5454
{
5555
try {
56-
$client = HttpClientDiscovery::find();
57-
} catch (\Http\Discovery\NotFoundException $e) {
58-
$client = $this->getMockForAbstractClass(HttpClient::class);
56+
$client = Psr18ClientDiscovery::find();
57+
} catch (\Http\Discovery\Exception\NotFoundException $e) {
58+
$client = $this->getMockForAbstractClass(ClientInterface::class);
5959

6060
$client
6161
->expects($this->any())

src/Provider/ArcGISOnline/ArcGISOnline.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use Geocoder\Query\ReverseQuery;
2424
use Geocoder\Http\Provider\AbstractHttpProvider;
2525
use Geocoder\Provider\Provider;
26-
use Http\Client\HttpClient;
26+
use Psr\Http\Client\ClientInterface;
2727

2828
/**
2929
* @author ALKOUM Dorian <[email protected]>
@@ -62,14 +62,14 @@ final class ArcGISOnline extends AbstractHttpProvider implements Provider
6262
* ArcGIS World Geocoding Service.
6363
* https://developers.arcgis.com/rest/geocode/api-reference/overview-world-geocoding-service.htm.
6464
*
65-
* @param HttpClient $client An HTTP adapter
66-
* @param string $token Your authentication token
67-
* @param string $sourceCountry Country biasing (optional)
65+
* @param ClientInterface $client An HTTP adapter
66+
* @param string $token Your authentication token
67+
* @param string $sourceCountry Country biasing (optional)
6868
*
6969
* @return ArcGISOnline
7070
*/
7171
public static function token(
72-
HttpClient $client,
72+
ClientInterface $client,
7373
string $token,
7474
string $sourceCountry = null
7575
) {
@@ -79,12 +79,12 @@ public static function token(
7979
}
8080

8181
/**
82-
* @param HttpClient $client An HTTP adapter
83-
* @param string $sourceCountry Country biasing (optional)
84-
* @param string $token ArcGIS World Geocoding Service token
85-
* Required for the geocodeAddresses endpoint
82+
* @param ClientInterface $client An HTTP adapter
83+
* @param string $sourceCountry Country biasing (optional)
84+
* @param string $token ArcGIS World Geocoding Service token
85+
* Required for the geocodeAddresses endpoint
8686
*/
87-
public function __construct(HttpClient $client, string $sourceCountry = null, string $token = null)
87+
public function __construct(ClientInterface $client, string $sourceCountry = null, string $token = null)
8888
{
8989
parent::__construct($client);
9090

src/Provider/ArcGISOnline/Readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Since a token is required for the `geocodeAddresses` API, the
3333
### Without a token
3434

3535
```php
36-
$httpClient = new \Http\Adapter\Guzzle6\Client();
36+
$httpClient = new \GuzzleHttp\Client();
3737

3838
$provider = new \Geocoder\Provider\ArcGISList\ArcGISList($httpClient);
3939

@@ -45,7 +45,7 @@ $result = $geocoder->geocodeQuery(GeocodeQuery::create('Buckingham Palace, Londo
4545

4646
```php
4747

48-
$httpClient = new \Http\Adapter\Guzzle6\Client();
48+
$httpClient = new \GuzzleHttp\Client();
4949

5050
// Your token is required.
5151
$provider = \Geocoder\Provider\ArcGISList\ArcGISList::token($httpClient, 'your-token');

0 commit comments

Comments
 (0)