-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add(Service): Add
IpInfo
as ApiProvider
- Loading branch information
1 parent
ffefeb6
commit 4a2b6ec
Showing
10 changed files
with
86 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,4 +115,5 @@ out | |
.yarn/build-state.yml | ||
.yarn/install-state.gz | ||
.pnp.* | ||
yarn.lock | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
/* | ||
* This file is part of GBCLStudio Project. | ||
* | ||
* Copyright (c) 2023 GBCLStudio PHP Project Team. | ||
* | ||
* For the full copyright and license information, please view the LICENSE.md | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace GBCLStudio\GeoIp\Api\Service; | ||
|
||
use Flarum\Settings\SettingsRepositoryInterface; | ||
use GBCLStudio\GeoIp\Api\GeoIpInterface; | ||
use GBCLStudio\GeoIp\ServiceResponse; | ||
use GuzzleHttp\Client; | ||
|
||
class IpInfo implements GeoIpInterface | ||
{ | ||
|
||
private Client $client; | ||
|
||
public function __construct(protected SettingsRepositoryInterface $settings) | ||
{ | ||
$this->client = new Client([ | ||
'base_uri' => 'https://ipinfo.io/' | ||
]); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function name(): string | ||
{ | ||
return 'ipinfo'; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function get(string $ip): ServiceResponse | ||
{ | ||
$apiKey = $this->settings->get("gbcl-userip.service.ipinfo.key"); | ||
if (is_null($apiKey)) throw new \InvalidArgumentException('gbcl-userip.service.ipinfo.key is null, are you set the ipinfo-key label?'); | ||
|
||
$res = $this->client->get("/$ip/json?token=$apiKey"); | ||
$body = json_decode($res->getBody()); | ||
|
||
// if not, the isp label will be longer than except | ||
preg_match('/^AS(\d+)\s+/', $body->org, $ispASN); | ||
|
||
return (new ServiceResponse()) | ||
->setCountryCode($body->country) | ||
->setRegion($body->region) | ||
->setIsp($ispASN) | ||
->setAddress($ip); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters