From a323cc38cb0f99ae7dd50d5292c4a40a2f49a5da Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Thu, 19 Mar 2015 13:17:24 -0700 Subject: [PATCH] Adding ability to specify an HTTP timeout Also changing Guzzle link in readme to point to appropriate version --- README.md | 2 +- src/GeoIp2/WebService/Client.php | 19 ++++++++++++++++-- tests/GeoIp2/Test/WebService/ClientTest.php | 22 +++++++++++++++++++-- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index eb7e208..fef0ae3 100644 --- a/README.md +++ b/README.md @@ -305,7 +305,7 @@ supported. This library works and is tested with HHVM. -This library also relies on the [Guzzle HTTP client](http://guzzlephp.org/) +This library also relies on the [Guzzle3 HTTP client](https://github.com/guzzle/guzzle3) and the [MaxMind DB Reader](https://github.com/maxmind/MaxMind-DB-Reader-php). If you are using PHP 5.3 with an autoloader besides Composer, you must load diff --git a/src/GeoIp2/WebService/Client.php b/src/GeoIp2/WebService/Client.php index 45db4ae..6f04bee 100644 --- a/src/GeoIp2/WebService/Client.php +++ b/src/GeoIp2/WebService/Client.php @@ -51,6 +51,8 @@ class Client implements ProviderInterface private $locales; private $host; private $guzzleClient; + private $timeout; + private $connectTimeout; /** * Constructor. @@ -62,13 +64,17 @@ class Client implements ProviderInterface * @param string $host Optional host parameter * @param object $guzzleClient Optional Guzzle client to use (to facilitate * unit testing). + * @param string $timeout Total transaction timeout + * @param string $connectTimeout Initial connection timeout */ public function __construct( $userId, $licenseKey, $locales = array('en'), $host = 'geoip.maxmind.com', - $guzzleClient = null + $guzzleClient = null, + $timeout = null, + $connectTimeout = null ) { $this->userId = $userId; $this->licenseKey = $licenseKey; @@ -76,6 +82,8 @@ class Client implements ProviderInterface $this->host = $host; // To enable unit testing $this->guzzleClient = $guzzleClient; + $this->timeout = $timeout; + $this->connectTimeout = $connectTimeout; } /** @@ -180,7 +188,14 @@ class Client implements ProviderInterface $client = $this->guzzleClient ? $this->guzzleClient : new GuzzleClient(); - $request = $client->get($uri, array('Accept' => 'application/json')); + $options = array(); + if($this->timeout != null){ + $options['timeout'] = $this->timeout; + } + if($this->connectTimeout != null){ + $options['connect_timeout'] = $this->connectTimeout; + } + $request = $client->get($uri, array('Accept' => 'application/json'), $options); $request->setAuth($this->userId, $this->licenseKey); $this->setUserAgent($request); diff --git a/tests/GeoIp2/Test/WebService/ClientTest.php b/tests/GeoIp2/Test/WebService/ClientTest.php index 9270da8..558272e 100644 --- a/tests/GeoIp2/Test/WebService/ClientTest.php +++ b/tests/GeoIp2/Test/WebService/ClientTest.php @@ -419,7 +419,9 @@ class ClientTest extends \PHPUnit_Framework_TestCase 'abcdef123456', array('en'), 'geoip.maxmind.com', - $guzzleClient + $guzzleClient, + 27, + 72 ); $client->country('1.2.3.4'); @@ -446,7 +448,19 @@ class ClientTest extends \PHPUnit_Framework_TestCase $this->assertStringMatchesFormat( 'GeoIP2 PHP API (Guzzle%s)', $request->getHeader('User-Agent') . '', - 'request sets Accept header to application/json' + 'request sets Accept header to GeoIP2 PHP API (Guzzle%s)' + ); + + $this->assertEquals( + '27000', + $request->getCurlOptions()[CURLOPT_TIMEOUT_MS], + 'request sets Curl Option Timeout to 27 seconds' + ); + + $this->assertEquals( + '72000', + $request->getCurlOptions()[CURLOPT_CONNECTTIMEOUT_MS], + 'request sets Curl Option Connect Timeout to 72 seconds' ); } @@ -464,6 +478,10 @@ class ClientTest extends \PHPUnit_Framework_TestCase $locales, 'geoip.maxmind.com', $guzzleClient + // intentionally not specifying the below, to ensure backwards compatibility + //, + // 1, // optional timeout + // 1 // optional connect timeout ); return $client;