Skip to content

Expect a PSR-18 client instead of a PHP-HTTP client #1110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 30, 2022
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
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ Read more about HTTPlug in [their docs](http://docs.php-http.org/en/latest/httpl

### Summary (Just give me the command)

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

```cmd
composer require geocoder-php/google-maps-provider php-http/guzzle6-adapter
composer require geocoder-php/google-maps-provider guzzlehttp/guzzle
```

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

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

## Usage

In the code snippet below we use GoogleMaps and Guzzle6.
In the code snippet below we use GoogleMaps and Guzzle 7.

```php
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;

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

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

$geocoder = new \Geocoder\ProviderAggregator();
$adapter = new \Http\Adapter\Guzzle6\Client();
$client = new \GuzzleHttp\Client();

$chain = new \Geocoder\Provider\Chain\Chain([
new \Geocoder\Provider\FreeGeoIp\FreeGeoIp($adapter),
new \Geocoder\Provider\HostIp\HostIp($adapter),
new \Geocoder\Provider\GoogleMaps\GoogleMaps($adapter, 'France'),
new \Geocoder\Provider\BingMaps\BingMaps($adapter, '<API_KEY>'),
new \Geocoder\Provider\FreeGeoIp\FreeGeoIp($client),
new \Geocoder\Provider\HostIp\HostIp($client),
new \Geocoder\Provider\GoogleMaps\GoogleMaps($client, 'France'),
new \Geocoder\Provider\BingMaps\BingMaps($client, '<API_KEY>'),
// ...
]);

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

$adapter = new \Http\Adapter\Guzzle6\Client();
$client = new \GuzzleHttp\Client();
$geocoder = new \Geocoder\ProviderAggregator();

$geocoder->registerProviders([
new \Geocoder\Provider\GoogleMaps\GoogleMaps($adapter),
new \Geocoder\Provider\GoogleMaps\GoogleMapsBusiness($adapter, '<CLIENT_ID>'),
new \Geocoder\Provider\Yandex\Yandex($adapter),
new \Geocoder\Provider\MaxMind\MaxMind($adapter, '<MAXMIND_API_KEY>'),
new \Geocoder\Provider\ArcGISOnline\ArcGISOnline($adapter),
new \Geocoder\Provider\GoogleMaps\GoogleMaps($client),
new \Geocoder\Provider\GoogleMaps\GoogleMapsBusiness($client, '<CLIENT_ID>'),
new \Geocoder\Provider\Yandex\Yandex($client),
new \Geocoder\Provider\MaxMind\MaxMind($client, '<MAXMIND_API_KEY>'),
new \Geocoder\Provider\ArcGISOnline\ArcGISOnline($client),
]);

$geocoder->registerProvider(new \Geocoder\Provider\Nominatim\Nominatim($adapter, 'https://your.nominatim.server'));
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
"require": {
"php": "^7.4 || ^8.0",
"igorw/get-in": "^1.0",
"php-http/client-implementation": "^1.0",
"php-http/discovery": "^1.4",
"php-http/httplug": "^2.2",
"php-http/message-factory": "^1.0.2",
"php-http/promise": "^1.0",
"psr/http-client": "^1.0",
"psr/http-client-implementation": "^1.0",
"psr/http-message-implementation": "^1.0",
"psr/log": "^1.0|^2.0|^3.0",
"psr/simple-cache": "^1.0|^2.0|^3.0"
Expand Down
10 changes: 5 additions & 5 deletions docs/cookbook/cache.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Caching responses

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

```php
use Cache\Adapter\Redis\RedisCachePool;
use Http\Adapter\Guzzle6\Client as GuzzleClient;
use GuzzleHttp\Client as GuzzleClient;
use Http\Client\Common\PluginClient;
use Geocoder\Provider\GoogleMaps;

Expand All @@ -23,8 +23,8 @@ $cachePlugin = new CachePlugin($pool, StreamFactoryDiscovery::find(), [
'default_ttl' => null,
'cache_lifetime' => 86400*365
]);
$adapter = new GuzzleClient();

$adapter = new GuzzleClient();
$pluginClient = new PluginClient($adapter, [$cachePlugin]);

// Get a geocoder
Expand Down
21 changes: 9 additions & 12 deletions docs/cookbook/http-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,25 @@

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

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

## Guzzle6
## Guzzle 7

```php
use GuzzleHttp\Client as GuzzleClient;
use Http\Adapter\Guzzle6\Client;
use GuzzleHttp\Client;
use Geocoder\Provider\GoogleMaps;

$config = [
'timeout' => 2.0,
'verify' => false,
];
$guzzle = new GuzzleClient($config);

$adapter = new Client($guzzle);
$geocoder = new GoogleMaps($adapter);
$client = new Client($config);
$geocoder = new GoogleMaps($client);

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

$options = [
CURLOPT_CONNECTTIMEOUT => 2,
CURLOPT_CONNECTTIMEOUT => 2,
CURLOPT_SSL_VERIFYPEER => false,
];

$adapter = new Client(null, null, $options);
$geocoder = new GoogleMaps($adapter);
$client = new Client(null, null, $options);
$geocoder = new GoogleMaps($client);

$geocoder->geocode(...);
```

3 changes: 1 addition & 2 deletions docs/cookbook/rate-limiting.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ $stack = \GuzzleHttp\HandlerStack::create();
$stack->push(\Spatie\GuzzleRateLimiterMiddleware\RateLimiterMiddleware::perSecond(1));

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

Expand Down
12 changes: 6 additions & 6 deletions src/Http/Provider/AbstractHttpProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Geocoder\Provider\AbstractProvider;
use Http\Message\MessageFactory;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Client\HttpClient;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;

/**
Expand All @@ -28,7 +28,7 @@
abstract class AbstractHttpProvider extends AbstractProvider
{
/**
* @var HttpClient
* @var ClientInterface
*/
private $client;

Expand All @@ -38,10 +38,10 @@ abstract class AbstractHttpProvider extends AbstractProvider
private $messageFactory;

/**
* @param HttpClient $client
* @param ClientInterface $client
* @param MessageFactory|null $factory
*/
public function __construct(HttpClient $client, MessageFactory $factory = null)
public function __construct(ClientInterface $client, MessageFactory $factory = null)
{
$this->client = $client;
$this->messageFactory = $factory ?: MessageFactoryDiscovery::find();
Expand Down Expand Up @@ -106,9 +106,9 @@ protected function getParsedResponse(RequestInterface $request): string
/**
* Returns the HTTP adapter.
*
* @return HttpClient
* @return ClientInterface
*/
protected function getHttpClient(): HttpClient
protected function getHttpClient(): ClientInterface
{
return $this->client;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Tests/Provider/AbstractHttpProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Geocoder\Model\AddressCollection;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;
use Http\Client\HttpClient;
use Psr\Http\Client\ClientInterface;
use Http\Mock\Client;
use PHPUnit\Framework\TestCase;

Expand All @@ -33,7 +33,7 @@ public function testHttpClientGetter()

class DummyProvider extends AbstractHttpProvider
{
public function getHttpClient(): HttpClient
public function getHttpClient(): ClientInterface
{
return parent::getHttpClient();
}
Expand Down
6 changes: 3 additions & 3 deletions src/Provider/AlgoliaPlaces/AlgoliaPlaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use Geocoder\Provider\Provider;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;
use Http\Client\HttpClient;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;

class AlgoliaPlaces extends AbstractHttpProvider implements Provider
Expand Down Expand Up @@ -53,10 +53,10 @@ class AlgoliaPlaces extends AbstractHttpProvider implements Provider
/** @var GeocodeQuery */
private $query;

/** @var HttpClient */
/** @var ClientInterface */
private $client;

public function __construct(HttpClient $client, string $apiKey = null, string $appId = null)
public function __construct(ClientInterface $client, string $apiKey = null, string $appId = null)
{
parent::__construct($client);

Expand Down
8 changes: 4 additions & 4 deletions src/Provider/AlgoliaPlaces/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)

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

## Install

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

$httpClient = new \Http\Adapter\Guzzle6\Client();
$httpClient = new \GuzzleHttp\Client();

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

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

## Contribute

Contributions are very welcome! Send a pull request to the [main repository](https://github.com/geocoder-php/Geocoder) or
Contributions are very welcome! Send a pull request to the [main repository](https://github.com/geocoder-php/Geocoder) or
report any issues you find on the [issue tracker](https://github.com/geocoder-php/Geocoder/issues).
12 changes: 6 additions & 6 deletions src/Provider/AlgoliaPlaces/Tests/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
use Geocoder\Model\Country;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;
use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Psr\Http\Client\ClientInterface;

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

protected $testReverse = false;

protected function createProvider(HttpClient $httpClient)
protected function createProvider(ClientInterface $httpClient)
{
return new AlgoliaPlaces($httpClient, $this->getApiKey(), $this->getAppId());
}
Expand All @@ -53,9 +53,9 @@ protected function getCacheDir()
private function getCachedHttpClient()
{
try {
$client = HttpClientDiscovery::find();
} catch (\Http\Discovery\NotFoundException $e) {
$client = $this->getMockForAbstractClass(HttpClient::class);
$client = Psr18ClientDiscovery::find();
} catch (\Http\Discovery\Exception\NotFoundException $e) {
$client = $this->getMockForAbstractClass(ClientInterface::class);

$client
->expects($this->any())
Expand Down
20 changes: 10 additions & 10 deletions src/Provider/ArcGISOnline/ArcGISOnline.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use Geocoder\Query\ReverseQuery;
use Geocoder\Http\Provider\AbstractHttpProvider;
use Geocoder\Provider\Provider;
use Http\Client\HttpClient;
use Psr\Http\Client\ClientInterface;

/**
* @author ALKOUM Dorian <[email protected]>
Expand Down Expand Up @@ -62,14 +62,14 @@ final class ArcGISOnline extends AbstractHttpProvider implements Provider
* ArcGIS World Geocoding Service.
* https://developers.arcgis.com/rest/geocode/api-reference/overview-world-geocoding-service.htm.
*
* @param HttpClient $client An HTTP adapter
* @param string $token Your authentication token
* @param string $sourceCountry Country biasing (optional)
* @param ClientInterface $client An HTTP adapter
* @param string $token Your authentication token
* @param string $sourceCountry Country biasing (optional)
*
* @return ArcGISOnline
*/
public static function token(
HttpClient $client,
ClientInterface $client,
string $token,
string $sourceCountry = null
) {
Expand All @@ -79,12 +79,12 @@ public static function token(
}

/**
* @param HttpClient $client An HTTP adapter
* @param string $sourceCountry Country biasing (optional)
* @param string $token ArcGIS World Geocoding Service token
* Required for the geocodeAddresses endpoint
* @param ClientInterface $client An HTTP adapter
* @param string $sourceCountry Country biasing (optional)
* @param string $token ArcGIS World Geocoding Service token
* Required for the geocodeAddresses endpoint
*/
public function __construct(HttpClient $client, string $sourceCountry = null, string $token = null)
public function __construct(ClientInterface $client, string $sourceCountry = null, string $token = null)
{
parent::__construct($client);

Expand Down
4 changes: 2 additions & 2 deletions src/Provider/ArcGISOnline/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Since a token is required for the `geocodeAddresses` API, the
### Without a token

```php
$httpClient = new \Http\Adapter\Guzzle6\Client();
$httpClient = new \GuzzleHttp\Client();

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

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

```php

$httpClient = new \Http\Adapter\Guzzle6\Client();
$httpClient = new \GuzzleHttp\Client();

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