Skip to content

Commit

Permalink
* ability to set an array of failover servers,
Browse files Browse the repository at this point in the history
* better doc strings and various cosmetic changes.


git-svn-id: http://svn.php.net/repository/pear/packages/Services_GeoNames/trunk@274374 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
izimobil committed Jan 23, 2009
1 parent aa1010e commit 73acbc5
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 68 deletions.
89 changes: 61 additions & 28 deletions Services/GeoNames.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
*
* @category Services
* @package Services_GeoNames
* @author David JEAN LOUIS <izimobil@gmail.com>
* @copyright 2008 David JEAN LOUIS
* @author David Jean Louis <izi@php.net>
* @copyright 2008-2009 David Jean Louis
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version SVN: $Id$
* @version CVS: $Id$
* @link http://pear.php.net/package/Services_GeoNames
* @link http://www.geonames.org/export/web-services.html
* @since File available since release 0.1.0
Expand All @@ -35,8 +35,8 @@
*
* @category Services
* @package Services_GeoNames
* @author David JEAN LOUIS <izimobil@gmail.com>
* @copyright 2008 David JEAN LOUIS
* @author David Jean Louis <izi@php.net>
* @copyright 2008-2009 David Jean Louis
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version Release: @package_version@
* @link http://pear.php.net/package/Services_GeoNames
Expand Down Expand Up @@ -106,11 +106,18 @@ class Services_GeoNames

/**
* Url of the GeoNames web service.
* This should not change but anyway we make it public.
*
* @var string $url
*/
public static $url = 'http://ws.geonames.org';
public $url = 'http://ws.geonames.org';

/**
* Array of failover servers.
*
* @var array $failoverServers
* @see Services_GeoNames::sendRequest()
*/
public $failoverServers = array();

/**
* The HTTP_Request2 instance, you can customize the request if you want to
Expand Down Expand Up @@ -230,7 +237,7 @@ public function __construct($username = null, $token = null)
public function __call($endpoint, $params = array())
{
// check that endpoint is supported
if (!in_array($endpoint, array_keys($this->endpoints))) {
if (!in_array($endpoint, $this->getSupportedEndpoints())) {
throw new Services_GeoNames_Exception(
'Unknown service endpoint "' . $endpoint . '"',
self::UNSUPPORTED_ENDPOINT
Expand All @@ -249,6 +256,8 @@ public function __call($endpoint, $params = array())
// we only do json
unset($params['type']);
}

// manage authentication to commercial webservice
if ($this->username !== null) {
$params['username'] = $this->username;
}
Expand All @@ -258,8 +267,8 @@ public function __call($endpoint, $params = array())

// build the url and retrieve the result
$qString = $this->formatQueryString($params);
$url = self::$url . '/' . $endpoint . 'JSON?' . $qString;
$ret = json_decode($this->sendRequest($url));
$urlPath = '/' . $endpoint . 'JSON?' . $qString;
$ret = json_decode($this->sendRequest($urlPath));

// check if we have a error response
if (isset($ret->status->message) && isset($ret->status->value)) {
Expand All @@ -270,7 +279,7 @@ public function __call($endpoint, $params = array())
}

// remove useless root property, to make the result more user friendly
if ($this->endpoints[$endpoint] !== false) {
if ($this->endpoints[$endpoint] !== false && $ret instanceof stdclass) {
$prop = $this->endpoints[$endpoint];
$ret = $ret->$prop;
}
Expand All @@ -283,33 +292,57 @@ public function __call($endpoint, $params = array())
/**
* Sends the request to the server using HTTP_Request2.
*
* @param string $url The full service url (url + endpoint + query string)
* @param string $urlPath The url path *without* the scheme://host
*
* @return string The response body
* @throws HTTP_Request2_Exception
* @throws Services_GeoNames_HTTPException When something goes wrong when
* building the request or
* requesting the server.
*/
protected function sendRequest($url)
protected function sendRequest($urlPath)
{
try {
$request = clone $this->getRequest();
$request->setUrl($url);
$response = $request->send();
} catch (HTTP_Request2_Exception $exc) {
throw new Services_GeoNames_HTTPException(
$exc->getMessage(),
$exc // the original exception cause
);
$exceptionStack = array();
$response = null;
array_unshift($this->failoverServers, $this->url);

foreach ($this->failoverServers as $server) {
try {
$request = clone $this->getRequest();
$request->setUrl(rtrim($server, '/') . $urlPath);
$response = $request->send();
} catch (Exception $exc) {
$exceptionStack[] = new Services_GeoNames_HTTPException(
$exc->getMessage(),
$exc
);
continue;
}
if ($response->getStatus() != 200) {
$exceptionStack[] = new Services_GeoNames_HTTPException(
$response->getReasonPhrase(),
$response->getStatus(),
$response
);
// reset the response variable since it's not a valid one
$response = null;
} else {
break;
}
}
if ($response->getStatus() != 200) {
throw new Services_GeoNames_HTTPException(
$response->getReasonPhrase(),
$response->getStatus(),
$response
);

if ($response == null && !empty($exceptionStack)) {
$lastException = $exceptionStack[count($exceptionStack)-1];
if (count($exceptionStack) == 1) {
throw $lastException;
} else {
throw new Services_GeoNames_HTTPException(
$lastException->getMessage(),
$exceptionStack
);
}
}

return $response->getBody();
}

Expand Down
14 changes: 7 additions & 7 deletions Services/GeoNames/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
*
* @category Services
* @package Services_GeoNames
* @author David JEAN LOUIS <izimobil@gmail.com>
* @copyright 2008 David JEAN LOUIS
* @author David Jean Louis <izi@php.net>
* @copyright 2008-2009 David Jean Louis
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version SVN: $Id$
* @version CVS: $Id$
* @link http://pear.php.net/package/Services_GeoNames
* @link http://www.geonames.org/export/webservice-exception.html
* @since File available since release 0.1.0
Expand All @@ -33,8 +33,8 @@
*
* @category Services
* @package Services_GeoNames
* @author David JEAN LOUIS <izimobil@gmail.com>
* @copyright 2008 David JEAN LOUIS
* @author David Jean Louis <izi@php.net>
* @copyright 2008-2009 David Jean Louis
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version Release: @package_version@
* @link http://pear.php.net/package/Services_GeoNames
Expand Down Expand Up @@ -64,8 +64,8 @@ class Services_GeoNames_Exception extends PEAR_Exception
*
* @category Services
* @package Services_GeoNames
* @author David JEAN LOUIS <izimobil@gmail.com>
* @copyright 2008 David JEAN LOUIS
* @author David Jean Louis <izi@php.net>
* @copyright 2008-2009 David Jean Louis
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version Release: @package_version@
* @link http://pear.php.net/package/Services_GeoNames
Expand Down
6 changes: 3 additions & 3 deletions examples/examples1.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
*
* @category Services
* @package Services_GeoNames
* @author David JEAN LOUIS <izimobil@gmail.com>
* @copyright 2008 David JEAN LOUIS
* @author David Jean Louis <izi@php.net>
* @copyright 2008-2009 David Jean Louis
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version SVN: $Id$
* @version CVS: $Id$
* @link http://pear.php.net/package/Services_GeoNames
* @link http://www.geonames.org/export/webservice-exception.html
* @since File available since release 0.1.0
Expand Down
59 changes: 53 additions & 6 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,46 @@ For more information please visit:
<email>izimobil@gmail.com</email>
<active>yes</active>
</lead>
<date>2008-12-19</date>
<date>2009-01-23</date>
<version>
<release>0.2.2</release>
<api>0.2.2</api>
<release>0.2.3</release>
<api>0.2.3</api>
</version>
<stability>
<release>beta</release>
<api>beta</api>
</stability>
<license uri="http://www.opensource.org/licenses/mit-license.html">MIT</license>
<notes>
* added getter/setter for request (now protected),
* better exceptions management,
* better tests layout/coverage.
<![CDATA[
* clone the HTTP_Request2 instance instead of the reusing the same instance,
* ability to set an array of failover servers,
* better doc strings and various cosmetic changes.
Important note
==============
From http://www.geonames.org/export/ :
"We have temporarily removed the domain ws.geonames.org from the dns setting to
protect the server from exessive use by an iphone application. You can access
the server with the domain ws5.geonames.org"
So until ws.geonames.org is not restored, you'll have to do either:
<?php
require_once 'Services/GeoNames.php';
$geo = new Services_GeoNames();
$geo->url = 'http://ws5.geonames.org';
?>
or:
<?php
require_once 'Services/GeoNames.php';
$geo = new Services_GeoNames();
$geo->failoverServers[] = 'http://ws5.geonames.org';
?>
]]>
</notes>
<contents>
<dir name="/">
Expand Down Expand Up @@ -92,6 +118,8 @@ For more information please visit:
<file name="tests/test_other_03.phpt" role="test" />
<file name="tests/test_other_04.phpt" role="test" />
<file name="tests/test_other_05.phpt" role="test" />
<file name="tests/test_other_06.phpt" role="test" />
<file name="tests/test_other_07.phpt" role="test" />
<file name="tests/test_postalcodecountryinfo.phpt" role="test" />
<file name="tests/test_postalcodelookup.phpt" role="test" />
<file name="tests/test_postalcodesearch.phpt" role="test" />
Expand Down Expand Up @@ -123,6 +151,7 @@ For more information please visit:
<file name="tests/data/test_neighbourhood.dat" role="test" />
<file name="tests/data/test_neighbours.dat" role="test" />
<file name="tests/data/test_other_03.dat" role="test" />
<file name="tests/data/test_other_06.dat" role="test" />
<file name="tests/data/test_postalcodecountryinfo.dat" role="test" />
<file name="tests/data/test_postalcodelookup.dat" role="test" />
<file name="tests/data/test_postalcodesearch.dat" role="test" />
Expand Down Expand Up @@ -206,5 +235,23 @@ For more information please visit:
</notes>
</release>

<!-- version 0.2.2 -->
<release>
<version>
<release>0.2.2</release>
<api>0.2.2</api>
</version>
<stability>
<release>beta</release>
<api>beta</api>
</stability>
<license uri="http://www.opensource.org/licenses/mit-license.html">MIT</license>
<notes>
* added getter/setter for request (now protected),
* better exceptions management,
* better tests layout/coverage.
</notes>
</release>

</changelog>
</package>
10 changes: 5 additions & 5 deletions tests/AllTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
*
* @category Services
* @package Services_GeoNames
* @author David JEAN LOUIS <izimobil@gmail.com>
* @copyright 2007 David JEAN LOUIS
* @author David Jean Louis <izi@php.net>
* @copyright 2008-2009 David Jean Louis
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version SVN: $Id$
* @version CVS: $Id$
* @link http://pear.php.net/package/Services_GeoNames
* @since File available since release 0.1.0
*/
Expand All @@ -39,8 +39,8 @@
*
* @category Services
* @package Services_GeoNames
* @author David JEAN LOUIS <izimobil@gmail.com>
* @copyright 2007 David JEAN LOUIS
* @author David Jean Louis <izi@php.net>
* @copyright 2008-2009 David Jean Louis
* @license http://opensource.org/licenses/mit-license.php MIT License
* @version Release: @package_version@
* @link http://pear.php.net/package/Services_GeoNames
Expand Down
6 changes: 3 additions & 3 deletions tests/data/test_get.dat
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"countryName": "",
"adminCode1": "20",
"fclName": "parks,area, ...",
"lng": 9.00266826152802,
"lng": 0,
"adminName2": "",
"adminName3": "",
"fcodeName": "",
Expand All @@ -106,7 +106,7 @@
"name": "Globe",
"fcode": "",
"geonameId": 6295630,
"lat": 46.01308817645,
"lat": 0,
"population": 0,
"adminName1": "null.20"
}
}
8 changes: 4 additions & 4 deletions tests/data/test_hierarchy.dat
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"lang": "de"
},
{
"name": "Mundo",
"name": "el planeta",
"lang": "es"
},
{
Expand All @@ -60,7 +60,7 @@
],
"countryName": "",
"fclName": "parks,area, ...",
"lng": 9.00266826152802,
"lng": 0,
"adminName2": "",
"adminName3": "",
"fcodeName": "",
Expand All @@ -74,7 +74,7 @@
"name": "Globe",
"fcode": "",
"geonameId": 6295630,
"lat": 46.01308817645,
"lat": 0,
"population": 0,
"adminName1": ""
},
Expand Down Expand Up @@ -818,4 +818,4 @@
"population": 341730,
"adminName1": "Zürich"
}
]}
]}
1 change: 1 addition & 0 deletions tests/data/test_other_06.dat

Large diffs are not rendered by default.

Loading

0 comments on commit 73acbc5

Please sign in to comment.