Skip to content

Commit 20e4f19

Browse files
authored
Merge pull request #68 from ipinfo/silvano/eng-297-add-lite-api-support-to-ipinfolaravel
Add support for Lite API
2 parents 6978c2a + 159d8a0 commit 20e4f19

9 files changed

Lines changed: 569 additions & 17 deletions

File tree

.github/workflows/tests.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
strategy:
15+
matrix:
16+
php: ["8.1", "8.2", "8.3", "8.4"]
17+
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v3
21+
22+
- name: Setup PHP
23+
uses: shivammathur/setup-php@v2
24+
with:
25+
php-version: ${{ matrix.php }}
26+
coverage: pcov
27+
28+
- name: Validate composer.json
29+
run: composer validate --strict
30+
31+
- name: Install dependencies
32+
run: composer install --no-interaction --prefer-dist --optimize-autoloader
33+
34+
- name: Run tests
35+
run: composer test

README.md

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# [<img src="https://ipinfo.io/static/ipinfo-small.svg" alt="IPinfo" width="24"/>](https://ipinfo.io/) IPinfo Laravel Client Library
22

33
This is the official Laravel client library for the [IPinfo.io](https://ipinfo.io) IP address API, allowing you to look up your own IP address, or get any of the following details for an IP:
4-
- [IP geolocation](https://ipinfo.io/ip-geolocation-api) (city, region, country, postal code, latitude, and longitude)
5-
- [ASN details](https://ipinfo.io/asn-api) (ISP or network operator, associated domain name, and type, such as business, hosting, or company)
6-
- [Company information](https://ipinfo.io/ip-company-api) (the name and domain of the business that uses the IP address)
7-
- [Carrier details](https://ipinfo.io/ip-carrier-api) (the name of the mobile carrier and MNC and MCC for that carrier if the IP is used exclusively for mobile traffic)
4+
5+
- [IP geolocation](https://ipinfo.io/ip-geolocation-api) (city, region, country, postal code, latitude, and longitude)
6+
- [ASN details](https://ipinfo.io/asn-api) (ISP or network operator, associated domain name, and type, such as business, hosting, or company)
7+
- [Company information](https://ipinfo.io/ip-company-api) (the name and domain of the business that uses the IP address)
8+
- [Carrier details](https://ipinfo.io/ip-carrier-api) (the name of the mobile carrier and MNC and MCC for that carrier if the IP is used exclusively for mobile traffic)
89

910
Check all the data we have for your IP address [here](https://ipinfo.io/what-is-my-ip).
1011

@@ -14,7 +15,7 @@ You'll need an IPinfo API access token, which you can get by signing up for a fr
1415

1516
The free plan is limited to 50,000 requests per month, and doesn't include some of the data fields such as IP type and company data. To enable all the data fields and additional request volumes see [https://ipinfo.io/pricing](https://ipinfo.io/pricing).
1617

17-
⚠️ Note: This library does not currently support our newest free API https://ipinfo.io/lite. If you’d like to use IPinfo Lite, you can call the [endpoint directly](https://ipinfo.io/developers/lite-api) using your preferred HTTP client. Developers are also welcome to contribute support for Lite by submitting a pull request.
18+
The library also supports the Lite API, see the [Lite API section](#lite-api) for more info.
1819

1920
#### Installation
2021

@@ -122,8 +123,8 @@ In-memory caching of `Details` data is provided by default via Laravel's file-ba
122123

123124
Default cache TTL and maximum size can be changed by setting values in the `$settings` argument array.
124125

125-
* Default maximum cache size: 4096 (multiples of 2 are recommended to increase efficiency)
126-
* Default TTL: 24 hours (in minutes)
126+
- Default maximum cache size: 4096 (multiples of 2 are recommended to increase efficiency)
127+
- Default TTL: 24 hours (in minutes)
127128

128129
```php
129130
'ipinfo' => [
@@ -223,10 +224,10 @@ By default, `ipinfolaravel` filters out requests that have `bot` or `spider` in
223224
],
224225
```
225226

226-
To set your own filtering rules, *thereby replacing the default filter*, you can set `ipinfo.config` to your own, custom callable function which satisfies the following rules:
227+
To set your own filtering rules, _thereby replacing the default filter_, you can set `ipinfo.config` to your own, custom callable function which satisfies the following rules:
227228

228-
* Accepts one request.
229-
* Returns *True to filter out, False to allow lookup*
229+
- Accepts one request.
230+
- Returns _True to filter out, False to allow lookup_
230231

231232
To use your own filter function:
232233

@@ -259,6 +260,7 @@ object will be equal to `null`.
259260
### Trying test application with Laravel Sail
260261

261262
Install Laravel Sail with:
263+
262264
```bash
263265
cd testapp
264266

@@ -277,6 +279,7 @@ echo "IPINFO_TOKEN=<YOUR_TOKEN>" > app/.env
277279
```
278280

279281
Visit http://0.0.0.0:80. You should see a message similar to:
282+
280283
```
281284
Hello world!
282285
@@ -289,6 +292,21 @@ To run tests within `testapp` while Sail is up:
289292
./vendor/bin/sail phpunit
290293
```
291294

295+
## Lite API
296+
297+
The library gives the possibility to use the [Lite API](https://ipinfo.io/developers/lite-api) too, authentication with your token is still required.
298+
299+
The returned details are slightly different from the Core API, but it has the same configurations options.
300+
301+
Add the following to the `Kernel::middleware` property:
302+
303+
```php
304+
protected $middleware = [
305+
...
306+
\ipinfo\ipinfolaravel\lite\ipinfolitelaravel::class,
307+
];
308+
```
309+
292310
### Other Libraries
293311

294312
There are official IPinfo client libraries available for many languages including PHP, Python, Go, Java, Ruby, and many popular frameworks such as Django, Rails, and Laravel. There are also many third-party libraries and integrations available for our API.

composer.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@
1313
"homepage": "https://github.com/ipinfo/ipinfolaravel",
1414
"keywords": ["Laravel", "ipinfolaravel"],
1515
"require": {
16-
"illuminate/support": ">=7",
17-
"ipinfo/ipinfo": "^3.1.0"
16+
"illuminate/support": "^7.0|^8.0|^9.0|^10.0|^11.0",
17+
"ipinfo/ipinfo": "^3.2.0"
1818
},
1919
"require-dev": {
20-
"phpunit/phpunit": ">=8",
20+
"phpunit/phpunit": "^9.6",
2121
"mockery/mockery": "^1.4.2",
22-
"orchestra/testbench": ">=6.4.0",
22+
"orchestra/testbench": "^8.36",
2323
"sempro/phpunit-pretty-print": "^1.3.0",
2424
"squizlabs/php_codesniffer": "^3.5.8"
2525
},
2626
"autoload": {
2727
"psr-4": {
28-
"ipinfo\\ipinfolaravel\\": "src/"
28+
"ipinfo\\ipinfolaravel\\": "src/",
29+
"ipinfo\\ipinfolaravel\\lite\\": "src/"
2930
}
3031
},
3132
"autoload-dev": {
@@ -35,7 +36,8 @@
3536
},
3637
"scripts": {
3738
"check-style": "phpcs -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src config",
38-
"fix-style": "phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src config"
39+
"fix-style": "phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src config",
40+
"test": "phpunit --colors=always --coverage-html=coverage"
3941
},
4042
"extra": {
4143
"laravel": {

config/ipinfolitelaravel.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
//
5+
];

src/ipinfolaravel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function handle($request, Closure $next)
6464
}
6565
}
6666

67-
$request->merge(['ipinfo' => $details]);
67+
$request->attributes->set('ipinfo', $details);
6868

6969
return $next($request);
7070
}

src/lite/ipinfolitelaravel.php

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
namespace ipinfo\ipinfolaravel\lite;
4+
5+
use Closure;
6+
use ipinfo\ipinfo\IPinfoLite as IPinfoLiteClient;
7+
use ipinfo\ipinfolaravel\DefaultCache;
8+
use ipinfo\ipinfolaravel\iphandler\DefaultIPSelector;
9+
10+
class ipinfolitelaravel
11+
{
12+
/**
13+
* IPinfo API access token.
14+
* @var string
15+
*/
16+
public $access_token = null;
17+
18+
/**
19+
* IPinfo client object settings.
20+
* @var array
21+
*/
22+
public $settings = [];
23+
24+
/**
25+
* Return true to skip IPinfo lookup, otherwise return false.
26+
* @var function
27+
*/
28+
public $filter = null;
29+
30+
/**
31+
* Provides ip.
32+
* @var ipinfo\ipinfolaravel\iphandler\IPHandlerInterface
33+
*/
34+
public $ip_selector = null;
35+
36+
const CACHE_MAXSIZE = 4096;
37+
const CACHE_TTL = 60 * 24;
38+
39+
/**
40+
* Handle an incoming request.
41+
* @param \Illuminate\Http\Request $request
42+
* @param \Closure $next
43+
* @return mixed
44+
*/
45+
public function handle($request, Closure $next)
46+
{
47+
$this->configure();
48+
49+
if ($this->filter && call_user_func($this->filter, $request)) {
50+
$details = null;
51+
} else {
52+
try {
53+
$details = $this->ipinfo->getDetails(
54+
$this->ip_selector->getIP($request),
55+
);
56+
} catch (\Exception $e) {
57+
$details = null;
58+
59+
// users can't catch this exception with their own wrapper
60+
// middleware unfortunately, so we catch it for them. but for
61+
// backwards-compatibility, we throw the exception again unless
62+
// they've told us not to.
63+
if ($this->no_except != true) {
64+
throw $e;
65+
}
66+
}
67+
}
68+
69+
$request->attributes->set("ipinfo", $details);
70+
71+
return $next($request);
72+
}
73+
74+
/**
75+
* Determine settings based on user-defined configs or use defaults.
76+
*/
77+
public function configure()
78+
{
79+
$this->access_token = config("services.ipinfo.access_token", null);
80+
$this->filter = config("services.ipinfo.filter", [
81+
$this,
82+
"defaultFilter",
83+
]);
84+
$this->no_except = config("services.ipinfo.no_except", false);
85+
$this->ip_selector = config(
86+
"services.ipinfo.ip_selector",
87+
new DefaultIPSelector(),
88+
);
89+
90+
if (
91+
$custom_countries = config("services.ipinfo.countries_file", null)
92+
) {
93+
$this->settings["countries_file"] = $custom_countries;
94+
}
95+
96+
if ($custom_cache = config("services.ipinfo.cache", null)) {
97+
$this->settings["cache"] = $custom_cache;
98+
} else {
99+
$maxsize = config(
100+
"services.ipinfo.cache_maxsize",
101+
self::CACHE_MAXSIZE,
102+
);
103+
$ttl = config("services.ipinfo.cache_ttl", self::CACHE_TTL);
104+
$this->settings["cache"] = new DefaultCache($maxsize, $ttl);
105+
}
106+
107+
$this->ipinfo = new IPinfoLiteClient(
108+
$this->access_token,
109+
$this->settings,
110+
);
111+
}
112+
113+
/**
114+
* Should IP lookup be skipped.
115+
* @param Request $request Request object.
116+
* @return bool Whether or not to filter out.
117+
*/
118+
public function defaultFilter($request)
119+
{
120+
$user_agent = $request->header("user-agent");
121+
if ($user_agent) {
122+
$lower_user_agent = strtolower($user_agent);
123+
124+
$is_spider = strpos($lower_user_agent, "spider") !== false;
125+
$is_bot = strpos($lower_user_agent, "bot") !== false;
126+
127+
return $is_spider || $is_bot;
128+
}
129+
130+
return false;
131+
}
132+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace ipinfo\ipinfolaravel\lite;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class ipinfolitelaravelServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Perform post-registration booting of services.
11+
* @return void
12+
*/
13+
public function boot()
14+
{
15+
// Publishing is only necessary when using the CLI.
16+
if ($this->app->runningInConsole()) {
17+
$this->bootForConsole();
18+
}
19+
}
20+
21+
/**
22+
* Register any package services.
23+
* @return void
24+
*/
25+
public function register()
26+
{
27+
$this->mergeConfigFrom(
28+
__DIR__ . "/../../config/ipinfolitelaravel.php",
29+
"ipinfolitelaravel",
30+
);
31+
32+
// Register the service the package provides.
33+
$this->app->singleton(
34+
"ipinfolitelaravel",
35+
fn($app) => new ipinfolitelaravel(),
36+
);
37+
}
38+
39+
/**
40+
* Get the services provided by the provider.
41+
* @return array
42+
*/
43+
public function provides()
44+
{
45+
return ["ipinfolitelaravel"];
46+
}
47+
48+
/**
49+
* Console-specific booting.
50+
* @return void
51+
*/
52+
protected function bootForConsole()
53+
{
54+
// Publishing the configuration file.
55+
$this->publishes(
56+
[
57+
__DIR__ . "/../../config/ipinfolitelaravel.php" => config_path(
58+
"ipinfolitelaravel.php",
59+
),
60+
],
61+
"ipinfolitelaravel.config",
62+
);
63+
}
64+
}

0 commit comments

Comments
 (0)