Permalink
Browse files

Implement Httplug (#512)

* Implement httplug

* Fix tests and remove .puli from repo

* Remove php 5.4

* Updated dependencies

* Doc update and use latest discovery

* Make sure we using phpuinit 4

* Using discovery 1.0

* Fixed doc block alignment

* Use PSR7 implementations instead of mocks
  • Loading branch information...
Nyholm committed Aug 16, 2016
1 parent c2373a6 commit 9ea2bcbf70cee0d469b67b950243d93c64d627d7
@@ -3,3 +3,4 @@ composer.lock
composer.phar
phpunit.xml
php-cs-fixer.phar
.puli/
@@ -12,7 +12,6 @@ env:
- deps=""
php:
- 5.4
- 5.5
- 5.6
- 7.0
@@ -21,7 +20,7 @@ php:
matrix:
fast_finish: true
include:
- php: 5.4
- php: 5.5
env: deps="low"
before_script:
@@ -31,4 +30,4 @@ before_script:
- if [ "$deps" = "" ]; then composer install --prefer-dist --no-interaction; fi
- if [ "$TRAVIS_PHP_VERSION" == "7.0" ]; then composer require "geoip/geoip"; fi
script: phpunit --coverage-text
script: ./vendor/bin/phpunit --coverage-text
@@ -73,8 +73,8 @@ since each HTTP-based provider implements
[PSR-7](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md).
```php
$curl = new \Ivory\HttpAdapter\CurlHttpAdapter();
$geocoder = new \Geocoder\Provider\GoogleMaps($curl);
$adapter = new \Http\Adapter\Guzzle6\Client();
$geocoder = new \Geocoder\Provider\GoogleMaps($adapter);
$geocoder->geocode(...);
$geocoder->reverse(...);
@@ -214,14 +214,14 @@ In order to talk to geocoding APIs, you need HTTP adapters. While it was part of
the library in Geocoder 1.x and 2.x, Geocoder 3.x and upper now relies on the
[PSR-7
Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md)
which defines how HTTP message should be implemented. Choose any library that
follows this PSR and implement the specified interfaces to use with Geocoder.
which defines how HTTP message should be implemented. You can use any library to send HTTP messages
that implements [php-http/client-implementation](https://packagist.org/providers/php-http/client-implementation).
As making choices is rather hard, Geocoder ships with the
[egeloen/http-adapter](https://github.com/egeloen/ivory-http-adapter) library by
default, but it is up to you to choose a different implementation.
To use Guzzle 6 you should run the follwing command:
**Note:** not all providers are HTTP-based.
```
$ composer require php-http/guzzle6-adapter
```
### Providers
@@ -360,7 +360,7 @@ when a provider returns a result. The result is returned by `GoogleMaps` because
``` php
$geocoder = new \Geocoder\ProviderAggregator();
$adapter = new \Ivory\HttpAdapter\CurlHttpAdapter();
$adapter = new \Http\Adapter\Guzzle6\Client();
$chain = new \Geocoder\Provider\Chain([
new \Geocoder\Provider\FreeGeoIp($adapter),
@@ -12,13 +12,21 @@
}
],
"require": {
"php": "^5.4|^7.0",
"egeloen/http-adapter": "~0.8|~1.0",
"igorw/get-in": "~1.0"
"php": "^5.5 || ^7.0",
"igorw/get-in": "^1.0",
"psr/http-message-implementation": "^1.0",
"php-http/client-implementation": "^1.0",
"php-http/message-factory": "^1.0.2",
"php-http/httplug": "^1.0",
"php-http/discovery": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8",
"geoip2/geoip2": "~2.0",
"symfony/stopwatch": "~2.5"
"symfony/stopwatch": "~2.5",
"php-http/message": "^1.0",
"php-http/guzzle6-adapter": "^1.0",
"php-http/mock-client": "^0.3.0"
},
"suggest": {
"ext-geoip": "Enabling the geoip extension allows you to use the MaxMindProvider.",
@@ -10,35 +10,81 @@
namespace Geocoder\Provider;
use Ivory\HttpAdapter\HttpAdapterInterface;
use Http\Message\MessageFactory;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Client\HttpClient;
/**
* @author William Durand <william.durand1@gmail.com>
*/
class AbstractHttpProvider extends AbstractProvider
{
/**
* @var HttpAdapterInterface
* @var HttpClient
*/
private $adapter;
private $client;
/**
* @param HttpAdapterInterface $adapter An HTTP adapter
* @var MessageFactory
*/
public function __construct(HttpAdapterInterface $adapter)
private $messageFactory;
/**
* @param HttpClient $client
* @param MessageFactory|null $factory
*/
public function __construct(HttpClient $client, MessageFactory $factory = null)
{
parent::__construct();
$this->adapter = $adapter;
$this->client = $client;
$this->messageFactory = $factory;
}
/**
* Returns the HTTP adapter.
*
* @return HttpAdapterInterface
* @return HttpClient
*/
protected function getHttpClient()
{
return $this->client;
}
/**
* @return MessageFactory
*/
public function getAdapter()
protected function getMessageFactory()
{
return $this->adapter;
if ($this->messageFactory === null) {
$this->messageFactory = MessageFactoryDiscovery::find();
}
return $this->messageFactory;
}
/**
* @param HttpClient $client
*
* @return AbstractHttpProvider
*/
public function setClient(HttpClient $client)
{
$this->client = $client;
return $this;
}
/**
* @param MessageFactory $messageFactory
*
* @return AbstractHttpProvider
*/
public function setMessageFactory(MessageFactory $messageFactory)
{
$this->messageFactory = $messageFactory;
return $this;
}
}
@@ -12,7 +12,7 @@
use Geocoder\Exception\NoResult;
use Geocoder\Exception\UnsupportedOperation;
use Ivory\HttpAdapter\HttpAdapterInterface;
use Http\Client\HttpClient;
/**
* @author ALKOUM Dorian <baikunz@gmail.com>
@@ -40,13 +40,13 @@ class ArcGISOnline extends AbstractHttpProvider implements Provider
private $protocol;
/**
* @param HttpAdapterInterface $adapter An HTTP adapter
* @param string $sourceCountry Country biasing (optional)
* @param bool $useSsl Whether to use an SSL connection (optional)
* @param HttpClient $client An HTTP adapter
* @param string $sourceCountry Country biasing (optional)
* @param bool $useSsl Whether to use an SSL connection (optional)
*/
public function __construct(HttpAdapterInterface $adapter, $sourceCountry = null, $useSsl = false)
public function __construct(HttpClient $client, $sourceCountry = null, $useSsl = false)
{
parent::__construct($adapter);
parent::__construct($client);
$this->sourceCountry = $sourceCountry;
$this->protocol = $useSsl ? 'https' : 'http';
@@ -167,8 +167,9 @@ private function buildQuery($query)
*/
private function executeQuery($query)
{
$query = $this->buildQuery($query);
$content = (string) $this->getAdapter()->get($query)->getBody();
$query = $this->buildQuery($query);
$request = $this->getMessageFactory()->createRequest('GET', $query);
$content = (string) $this->getHttpClient()->sendRequest($request)->getBody();
if (empty($content)) {
throw new NoResult(sprintf('Could not execute query "%s".', $query));
@@ -13,7 +13,7 @@
use Geocoder\Exception\InvalidCredentials;
use Geocoder\Exception\NoResult;
use Geocoder\Exception\UnsupportedOperation;
use Ivory\HttpAdapter\HttpAdapterInterface;
use Http\Client\HttpClient;
/**
* @author David Guyon <dguyon@gmail.com>
@@ -38,13 +38,13 @@ class BingMaps extends AbstractHttpProvider implements LocaleAwareProvider
private $apiKey;
/**
* @param HttpAdapterInterface $adapter An HTTP adapter
* @param string $apiKey An API key
* @param string $locale A locale (optional)
* @param HttpClient $client An HTTP adapter
* @param string $apiKey An API key
* @param string $locale A locale (optional)
*/
public function __construct(HttpAdapterInterface $adapter, $apiKey, $locale = null)
public function __construct(HttpClient $client, $apiKey, $locale = null)
{
parent::__construct($adapter);
parent::__construct($client);
$this->apiKey = $apiKey;
$this->locale = $locale;
@@ -100,7 +100,8 @@ private function executeQuery($query)
$query = sprintf('%s&culture=%s', $query, str_replace('_', '-', $this->getLocale()));
}
$content = (string) $this->getAdapter()->get($query)->getBody();
$request = $this->getMessageFactory()->createRequest('GET', $query);
$content = (string) $this->getHttpClient()->sendRequest($request)->getBody();
if (empty($content)) {
throw new NoResult(sprintf('Could not execute query "%s".', $query));
@@ -65,7 +65,8 @@ public function getName()
*/
private function executeQuery($query)
{
$content = (string) $this->getAdapter()->get($query)->getBody();
$request = $this->getMessageFactory()->createRequest('GET', $query);
$content = (string) $this->getHttpClient()->sendRequest($request)->getBody();
if (empty($content)) {
throw new NoResult(sprintf('Could not execute query %s', $query));
@@ -15,7 +15,7 @@
use Geocoder\Exception\NoResult;
use Geocoder\Exception\QuotaExceeded;
use Geocoder\Exception\UnsupportedOperation;
use Ivory\HttpAdapter\HttpAdapterInterface;
use Http\Client\HttpClient;
/**
* @author Andrea Cristaudo <andrea.cristaudo@gmail.com>
@@ -50,12 +50,12 @@ class GeoIPs extends AbstractHttpProvider implements Provider
private $apiKey;
/**
* @param HttpAdapterInterface $adapter An HTTP adapter
* @param string $apiKey An API key
* @param HttpClient $client An HTTP adapter
* @param string $apiKey An API key
*/
public function __construct(HttpAdapterInterface $adapter, $apiKey)
public function __construct(HttpClient $client, $apiKey)
{
parent::__construct($adapter);
parent::__construct($client);
$this->apiKey = $apiKey;
}
@@ -107,7 +107,8 @@ public function getName()
*/
private function executeQuery($query)
{
$content = (string) $this->getAdapter()->get($query)->getBody();
$request = $this->getMessageFactory()->createRequest('GET', $query);
$content = (string) $this->getHttpClient()->sendRequest($request)->getBody();
if (empty($content)) {
throw new NoResult(sprintf('Invalid response from GeoIPs API for query "%s".', $query));
@@ -62,7 +62,8 @@ public function getName()
*/
private function executeQuery($query)
{
$content = (string) $this->getAdapter()->get($query)->getBody();
$request = $this->getMessageFactory()->createRequest('GET', $query);
$content = (string) $this->getHttpClient()->sendRequest($request)->getBody();
if (empty($content)) {
throw new NoResult(sprintf('Could not execute query "%s".', $query));
@@ -14,7 +14,7 @@
use Geocoder\Exception\NoResult;
use Geocoder\Exception\UnsupportedOperation;
use Geocoder\Model\AdminLevelCollection;
use Ivory\HttpAdapter\HttpAdapterInterface;
use Http\Client\HttpClient;
/**
* @author Giovanni Pirrotta <giovanni.pirrotta@gmail.com>
@@ -39,13 +39,13 @@ class Geonames extends AbstractHttpProvider implements LocaleAwareProvider
private $username;
/**
* @param HttpAdapterInterface $adapter An HTTP adapter
* @param string $username Username login (Free registration at http://www.geonames.org/login)
* @param string $locale A locale (optional)
* @param HttpClient $client An HTTP adapter
* @param string $username Username login (Free registration at http://www.geonames.org/login)
* @param string $locale A locale (optional)
*/
public function __construct(HttpAdapterInterface $adapter, $username, $locale = null)
public function __construct(HttpClient $client, $username, $locale = null)
{
parent::__construct($adapter);
parent::__construct($client);
$this->username = $username;
$this->locale = $locale;
@@ -102,7 +102,8 @@ private function executeQuery($query)
$query = sprintf('%s&lang=%s', $query, substr($this->getLocale(), 0, 2));
}
$content = (string) $this->getAdapter()->get($query)->getBody();
$request = $this->getMessageFactory()->createRequest('GET', $query);
$content = (string) $this->getHttpClient()->sendRequest($request)->getBody();
if (empty($content)) {
throw new NoResult(sprintf('Could not execute query "%s".', $query));
@@ -15,7 +15,7 @@
use Geocoder\Exception\NoResult;
use Geocoder\Exception\QuotaExceeded;
use Geocoder\Exception\UnsupportedOperation;
use Ivory\HttpAdapter\HttpAdapterInterface;
use Http\Client\HttpClient;
/**
* @author William Durand <william.durand1@gmail.com>
@@ -50,15 +50,15 @@ class GoogleMaps extends AbstractHttpProvider implements LocaleAwareProvider
private $apiKey;
/**
* @param HttpAdapterInterface $adapter An HTTP adapter
* @param string $locale A locale (optional)
* @param string $region Region biasing (optional)
* @param bool $useSsl Whether to use an SSL connection (optional)
* @param string $apiKey Google Geocoding API key (optional)
* @param HttpClient $client An HTTP adapter
* @param string $locale A locale (optional)
* @param string $region Region biasing (optional)
* @param bool $useSsl Whether to use an SSL connection (optional)
* @param string $apiKey Google Geocoding API key (optional)
*/
public function __construct(HttpAdapterInterface $adapter, $locale = null, $region = null, $useSsl = false, $apiKey = null)
public function __construct(HttpClient $client, $locale = null, $region = null, $useSsl = false, $apiKey = null)
{
parent::__construct($adapter);
parent::__construct($client);
$this->locale = $locale;
$this->region = $region;
@@ -136,7 +136,8 @@ protected function buildQuery($query)
private function executeQuery($query)
{
$query = $this->buildQuery($query);
$content = (string) $this->getAdapter()->get($query)->getBody();
$request = $this->getMessageFactory()->createRequest('GET', $query);
$content = (string) $this->getHttpClient()->sendRequest($request)->getBody();
// Throw exception if invalid clientID and/or privateKey used with GoogleMapsBusinessProvider
if (strpos($content, "Provided 'signature' is not valid for the provided client ID") !== false) {
Oops, something went wrong.

0 comments on commit 9ea2bcb

Please sign in to comment.