Skip to content

Commit

Permalink
Added multiple tries
Browse files Browse the repository at this point in the history
  • Loading branch information
zgelici committed Aug 5, 2019
1 parent f1bd90f commit 791b3ab
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/Services/IPApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ class IPApi extends AbstractService
*/
protected $continents;


/**
* Values to try content.
*
* @var integer
*/
protected $tries = 3;


/**
* The "booting" method of the service.
*
Expand Down Expand Up @@ -70,8 +79,34 @@ public function locate($ip)
// Parse body content
$json = json_decode($data[0]);


# Has data
if(!$json) {
$this->tries = $this->tries-1; #2(1) 1(2)
if($this->tries < 0) {

# requests fails, give empty values back
return $this->hydrate([
'ip' => $ip,
'iso_code' => null,
'country' => null,
'city' => null,
'state' => null,
'state_name' => null,
'postal_code' => null,
'lat' => null,
'lon' => null,
'timezone' => null,
'continent' => $this->getContinent(null)
]);
}

# try same request again
return $this->locate($ip);
}

// Verify response status
if ($json && $json->status !== 'success') {
if ($json->status !== 'success') {
throw new Exception('Request failed (' . $json->message . ')');
}

Expand Down

1 comment on commit 791b3ab

@huesoamz
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a nice fix!, why its not in the master branch?

Please sign in to comment.