Filled in Client code

This commit is contained in:
Gregory Oschwald 2013-05-07 13:13:11 -07:00
parent 2de2cb779f
commit 76a40bb2a1
4 changed files with 94 additions and 12 deletions

View File

@ -0,0 +1,14 @@
<?php
namespace GeoIP2\Exception;
class HttpException extends \Exception
{
public function __construct($message, $code, $uri,
Exception $previous = null)
{
parent::__construct($message, $code, $previous);
}
}

View File

@ -5,4 +5,8 @@ namespace GeoIP2\Model;
class City
{
public function __construct($raw, $language)
{
}
}

View File

@ -3,7 +3,7 @@
namespace GeoIP2\Webservice;
use GeoIP2\Error\Generic;
use GeoIP2\Error\HTTP;
use GeoIP2\Exception\HttpException;
use GeoIP2\Error\Webservice;
use GeoIP2\Model\City;
use GeoIP2\Model\CityISPOrg;
@ -16,58 +16,122 @@ class Client
private $user_id;
private $license_key;
private $language;
private $base_uri = 'https://geoip.maxmind.com/geoip/v2.0';
function __construct($user_id, $license_key)
function __construct($user_id, $license_key, $language='en')
{
$this->user_id = $user_id;
$this->license_key = $license_key;
$this->language = $language;
}
public function city($ip_address = 'me')
{
return $this->response_for('city', $ip_address);
return $this->response_for('city', 'City', $ip_address);
}
public function country($ip_address = 'me')
{
return $this->response_for('country', $ip_address);
return $this->response_for('country', 'Country', $ip_address);
}
public function cityISPOrg($ip_address = 'me')
{
return $this->response_for('city_isp_org', $ip_address);
return $this->response_for('city_isp_org', 'CityISPOrg', $ip_address);
}
public function omni($ip_address = 'me')
{
return $this->response_for('omni', $ip_address);
return $this->response_for('omni', 'Omni', $ip_address);
}
private function response_for($path, $ip_address)
private function response_for($path, $class, $ip_address)
{
$uri = implode('/', array($this->base_uri, $path, $ip_address));
$client = new GuzzleClient();
$request = $client->get($uri, array('Accept' => 'application/json'));
$request->setAuth($this->user_id, $this->license_key);
$ua = $request->getHeader('User-Agent');
$ua = "GeoIP2 PHP API ($ua)";
$request->setHeader('User-Agent', $ua);
$response = $request->send();
echo $response->getBody();
if ($response->isSuccessful()) {
$body = $this->handleSuccess($response, $uri);
$class = "GeoIP2\\Model\\" . $class;
return new $class($body, $this->language);
}
}
private function handle_success($response, $uri)
private function handleSuccess($response, $uri)
{
// XXX - handle exceptions
try {
return $response->json();
}
// XXX - figure out what sort of exception to catch
catch (Exception $e) {
throw new GenericException("Received a 200 response for $uri but could not decode the response as JSON: " . $e->getMessage());
}
}
private function handle_error($response, $uri)
private function handleError($response, $uri)
{
$status = $response->getStatusCode();
if ($status >= 400 && $status <= 499) {
$this->handle4xx($response, $uri);
}
elseif ($status >= 500 && $status <= 599){
$this->handle5xx($response, $uri);
}
else {
$this->hanldeNon200($reponse, $uri);
}
}
private function handle_4xx($response, $uri)
private function handle4xx($response, $uri)
{
if ( $response->getContentLength() > 0 ) {
if( strstr($response->getContentType(), 'json')) {
try {
$body = $response->json();
if (!$body['code'] || $body['error'] ){
throw new GenericException('Response contains JSON but it does not specify code or error keys');
}
}
// XXX - don't catch all exceptions
catch (Exception $e){
throw new HttpException("Received a $status error for $uri but it did not include the expected JSON body: " . $e->getMessage(), $status, $uri);
}
}
else {
throw new HttpException("Received a $status error for $uri with the following body: $content",
$status, $uri);
}
}
else {
throw new HttpException("Received a $status error for $uri with no body",
$status, $uri);
}
throw new WebserviceException($body['error'], $status, $uri);
}
private function handle_5xx($response, $uri)
private function handle5xx($response, $uri)
{
throw new HttpException("Received a server error ($status) for $uri",
$status,$uri);
}
private function handleNon200($response, $uri)
{
throw new HttpException("Received a very surprising HTTP status " .
"($status) for $uri",
$status, $uri);
}
}