Skip to content

Add new service apibundle.io #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion config/geoip.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
| Here you may specify the default storage driver that should be used
| by the framework.
|
| Supported: "maxmind_database", "maxmind_api", "ipapi"
| Supported: "maxmind_database", "maxmind_api", "ipapi", "ipgeolocation",
| "ipdata", "ipfinder", "apibundle"
|
*/

Expand Down Expand Up @@ -94,6 +95,13 @@
'locales' => ['en'],
],

'apibundle' => [
'class' => \Torann\GeoIP\Services\ApiBundle::class,
'key' => env('APIBUNDLE_API_KEY'),
'secure' => true,
'lang' => 'en',
],

],

/*
Expand Down
57 changes: 57 additions & 0 deletions src/Services/ApiBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Torann\GeoIP\Services;

use Exception;
use Torann\GeoIP\Support\HttpClient;

/**
* Class GeoIP
* @package Torann\GeoIP\Services
*/
class ApiBundle extends AbstractService
{
/**
* Http client instance.
*
* @var HttpClient
*/
protected $client;

/**
* The "booting" method of the service.
*
* @return void
*/
public function boot()
{
$base = [
'base_uri' => 'https://api.apibundle.io/ip-lookup?apikey=' . $this->config('key'),
];

if ($this->config('lang')) {
$base['base_uri'] .= '&language=' . $this->config('lang');
}

$this->client = new HttpClient($base);
}

/**
* {@inheritdoc}
* @throws Exception
*/
public function locate($ip)
{
// Get data from client
$data = $this->client->get('&ip=' . $ip);

// Verify server response
if ($this->client->getErrors() !== null || empty($data[0])) {
throw new Exception('Request failed (' . $this->client->getErrors() . ')');
}

$json = json_decode($data[0], true);

return $this->hydrate($json);
}
}