44
55require_once (__DIR__ .'/cache/Default.php ' );
66
7- use GuzzleHttp \Exception \TransferException ;
7+ use Exception ;
8+ use GuzzleHttp \Exception \GuzzleException ;
89use ipinfo \ipinfo \Details ;
10+ use ipinfo \ipinfo \IPinfoException ;
911
1012/**
1113 * Exposes the IPinfo library to client code.
@@ -14,7 +16,7 @@ class IPinfo
1416{
1517 const API_URL = 'https://ipinfo.io ' ;
1618 const CACHE_MAXSIZE = 4096 ;
17- const CACHE_TTL = 60 * 60 * 24 ;
19+ const CACHE_TTL = 86400 ; // 24 hours as seconds
1820 const COUNTRIES_FILE_DEFAULT = __DIR__ . '/countries.json ' ;
1921 const REQUEST_TYPE_GET = 'GET ' ;
2022 const STATUS_CODE_QUOTA_EXCEEDED = 429 ;
@@ -45,6 +47,7 @@ public function __construct($access_token = null, $settings = [])
4547 * Get formatted details for an IP address.
4648 * @param string|null $ip_address IP address to look up.
4749 * @return Details Formatted IPinfo data.
50+ * @throws IPinfoException
4851 */
4952 public function getDetails ($ ip_address = null )
5053 {
@@ -79,6 +82,7 @@ public function formatDetailsObject($details = [])
7982 * Get details for a specific IP address.
8083 * @param string $ip_address IP address to query API for.
8184 * @return array IP response data.
85+ * @throws IPinfoException
8286 */
8387 public function getRequestDetails (string $ ip_address )
8488 {
@@ -88,16 +92,22 @@ public function getRequestDetails(string $ip_address)
8892 $ url .= "/ $ ip_address " ;
8993 }
9094
91- $ response = $ this ->http_client ->request (
92- self ::REQUEST_TYPE_GET ,
93- $ url ,
94- $ this ->buildHeaders ()
95- );
95+ try {
96+ $ response = $ this ->http_client ->request (
97+ self ::REQUEST_TYPE_GET ,
98+ $ url ,
99+ $ this ->buildHeaders ()
100+ );
101+ } catch (GuzzleException $ e ) {
102+ throw new IPinfoException ($ e ->getMessage ());
103+ } catch (Exception $ e ) {
104+ throw new IPinfoException ($ e ->getMessage ());
105+ }
96106
97107 if ($ response ->getStatusCode () == self ::STATUS_CODE_QUOTA_EXCEEDED ) {
98- throw new Exception ('IPinfo request quota exceeded. ' );
108+ throw new IPinfoException ('IPinfo request quota exceeded. ' );
99109 } elseif ($ response ->getStatusCode () >= 400 ) {
100- throw new Exception ('Exception: ' . json_encode ([
110+ throw new IPinfoException ('Exception: ' . json_encode ([
101111 'status ' => $ response ->getStatusCode (),
102112 'reason ' => $ response ->getReasonPhrase (),
103113 ]));
0 commit comments