Skip to content
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

Add 7x as a Geolocation API Provider #34

Merged
merged 3 commits into from
Feb 7, 2024
Merged
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
310 changes: 309 additions & 1 deletion js/dist/admin.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/dist/admin.js.map

Large diffs are not rendered by default.

1,924 changes: 1,922 additions & 2 deletions js/dist/forum.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/dist/forum.js.map

Large diffs are not rendered by default.

94 changes: 74 additions & 20 deletions js/package-lock.json

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
"prettier": "@flarum/prettier-config",
"dependencies": {
"@flarum/prettier-config": "^1.0.0",
"flarum-tsconfig": "^1.0.2",
"flarum-webpack-config": "^2.0.0",
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4",
"country-code-emoji": "^1.0.0",
"external-load": "^1.0.0",
"flarum-tsconfig": "^1.0.2",
"flarum-webpack-config": "^2.0.0",
"linkify-lite": "^1.0.0",
"twemoji-basename": "^1.0.0"
"twemoji-basename": "^1.0.0",
"webpack": "^5.88.2"
},
"devDependencies": {
"prettier": "^3.0.3"
"prettier": "^3.0.3",
"webpack-cli": "^5.1.4"
},
"scripts": {
"dev": "webpack --mode development --watch",
Expand Down
3 changes: 1 addition & 2 deletions js/src/admin/components/ExtensionSettingsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export default class GeoipSettingsPage extends ExtensionPage {
label: app.translator.trans('fof-geoip.admin.settings.service_label'),
options: app.data['fof-geoip.services'].reduce((o, p) => {
o[p] = app.translator.trans(`fof-geoip.admin.settings.service_${p}_label`);

return o;
}, {}),
required: true,
Expand All @@ -44,7 +43,7 @@ export default class GeoipSettingsPage extends ExtensionPage {
)
: ''}

{['ipdata', 'ipapi-pro'].includes(service)
{['ipdata', 'ipapi-pro', 'ipsevenex'].includes(service)
? [
this.buildSettingComponent({
type: 'string',
Expand Down
3 changes: 3 additions & 0 deletions resources/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ fof-geoip:
service_iplocation_label: IP Location
service_iplocation_description: Use <b>https://www.iplocation.net/</b> to retrieve country and ISP data. Lookup rate limits are unknown.

service_ipsevenex_label: 7x Geolocation API
service_ipsevenex_description: Use <b>https://7x.ax</b> to get a free API key for up to 20 requests per minute. Paid plans for higher usage limits are also available.

access_key_label: Access Key
quota_label: Lookup quota

Expand Down
1 change: 1 addition & 0 deletions src/Api/GeoIP.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class GeoIP
'ipapi-pro' => Services\IPApiPro::class,
'ipdata' => Services\IPData::class,
'iplocation' => Services\IPLocation::class,
'ipsevenex' => Services\IPSevenEx::class,
];

private $prefix = 'fof-geoip.services';
Expand Down
87 changes: 87 additions & 0 deletions src/Api/Services/IPSevenEx.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

/*
* This file is part of fof/geoip.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\GeoIP\Api\Services;

use FoF\GeoIP\Api\GeoIP;
use FoF\GeoIP\Api\ServiceResponse;
use Psr\Http\Message\ResponseInterface;

class IPSevenEx extends BaseGeoService
{
protected $host = 'https://api.7x.ax';
protected $settingPrefix = 'fof-geoip.services.ipsevenex';

public function isRateLimited(): bool
{
return true;
}

protected function updateRateLimitsFromResponse(ResponseInterface $response, string $requestType = 'single'): void
{

}


protected function buildUrl(string $ip, ?string $apiKey): string
{
return "geolocate/v1/ip/$ip";
}

protected function buildBatchUrl(array $ips, ?string $apiKey): string
{
// Not supported by API
return '';
}

protected function getRequestOptions(?string $apiKey, array $ips = null): array
{
return [
'http_errors' => false,
'query' => [
'apikey' => $apiKey,
],
];
}

protected function hasError(ResponseInterface $response, object $body): bool
{
return $response->getStatusCode() > 200;
}

protected function handleError(ResponseInterface $response, object $body): ?ServiceResponse
{
return GeoIP::setError('ipsevenex', $body->data ?? json_encode($body));
}

protected function parseResponse(object $body): ServiceResponse
{

$response = new ServiceResponse($this->host);

$body = $body->data;

$response->setCountryCode($body->country->iso_code)
->setZipCode($body->postal->code)
->setLatitude($body->location->latitude)
->setLongitude($body->location->longitude)
->setIsp($body->traits->isp)
->setOrganization($body->traits->autonomous_system_organization);

return $response;
}

protected function parseBatchResponse(array $body): array
{
// Not supported by API
return [];
}
}
Loading