Skip to content

Commit

Permalink
add currency, calling code to location class and fix exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
renanbr committed Jan 17, 2024
1 parent c67807e commit 459855b
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 16 deletions.
16 changes: 9 additions & 7 deletions src/Provider/IpApi/IpApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class IpApi extends AbstractHttpProvider
{
private const URL = '{host_prefix}ip-api.com/json/{ip}';

private const FIELDS = 'status,message,lat,lon,city,district,zip,country,countryCode,timezone,regionName,region,proxy,hosting';
private const FIELDS = 'status,message,lat,lon,city,district,zip,country,countryCode,timezone,regionName,region,currency,callingCode,proxy,hosting';

private string|null $apiKey;

Expand Down Expand Up @@ -78,7 +78,7 @@ public function getName(): string
return 'ip-api';
}

public function buildUrl(string $ip, string|null $locale): string
private function buildUrl(string $ip, string|null $locale): string
{
$baseUrl = strtr(self::URL, [
'{host_prefix}' => $this->apiKey ? 'https://pro.' : 'http://',
Expand Down Expand Up @@ -121,6 +121,8 @@ private function buildLocation(array $data): IpApiLocation
$location = $builder->build(IpApiLocation::class);

return $location
->withCurrency($data['currency'] ?? null)
->withCallingCode($data['callingCode'] ?? null)
->withIsProxy($data['proxy'])
->withIsHosting($data['hosting']);
}
Expand All @@ -134,16 +136,16 @@ private function throwError(string $message)
{
if (
in_array($message, ['private range', 'reserved range', 'invalid query'], true)
|| str_contains('Origin restriction', $message)
|| str_contains('IP range restriction', $message)
|| str_contains('Calling IP restriction', $message)
|| str_contains($message, 'Origin restriction')
|| str_contains($message, 'IP range restriction')
|| str_contains($message, 'Calling IP restriction')
) {
throw new InvalidArgument($message);
}

if (
str_contains('invalid/expired ke', $message)
|| str_contains('no API key supplied', $message)
str_contains($message, 'invalid/expired key')
|| str_contains($message, 'no API key supplied')
) {
throw new InvalidCredentials($message);
}
Expand Down
30 changes: 30 additions & 0 deletions src/Provider/IpApi/Model/IpApiLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

final class IpApiLocation extends Address
{
private string|null $currency;

private string|null $callingCode;

private bool $isProxy;

private bool $isHosting;
Expand All @@ -25,6 +29,32 @@ public function isProxy(): bool
return $this->isProxy;
}

public function withCurrency(string|null $currency): self
{
$new = clone $this;
$new->currency = $currency;

return $new;
}

public function getCurrency(): string|null
{
return $this->currency;
}

public function withCallingCode(string|null $callingCode): self
{
$new = clone $this;
$new->callingCode = $callingCode;

return $new;
}

public function getCallingCode(): string|null
{
return $this->callingCode;
}

public function withIsProxy(bool $isProxy): self
{
$new = clone $this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
s:230:"{"status":"success","country":"United States","countryCode":"US","region":"OK","regionName":"Oklahoma","city":"Tulsa","district":"","zip":"","lat":36.15398,"lon":-95.99277,"timezone":"America/Chicago","proxy":false,"hosting":true}";
s:247:"{"status":"success","country":"United States","countryCode":"US","region":"OK","regionName":"Oklahoma","city":"Tulsa","district":"","zip":"","lat":36.15398,"lon":-95.99277,"timezone":"America/Chicago","currency":"USD","proxy":false,"hosting":true}";
Original file line number Diff line number Diff line change
@@ -1 +1 @@
s:230:"{"status":"success","country":"United States","countryCode":"US","region":"OK","regionName":"Oklahoma","city":"Tulsa","district":"","zip":"","lat":36.15398,"lon":-95.99277,"timezone":"America/Chicago","proxy":false,"hosting":true}";
s:247:"{"status":"success","country":"United States","countryCode":"US","region":"OK","regionName":"Oklahoma","city":"Tulsa","district":"","zip":"","lat":36.15398,"lon":-95.99277,"timezone":"America/Chicago","currency":"USD","proxy":false,"hosting":true}";

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
s:265:"{"callingCode":"1","city":"Tulsa","country":"United States","countryCode":"US","currency":"USD","district":"","hosting":true,"lat":36.15398,"lon":-95.99277,"proxy":false,"region":"OK","regionName":"Oklahoma","status":"success","timezone":"America/Chicago","zip":""}";

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
s:275:"{"callingCode":"46","city":"Karlskrona","country":"Sweden","countryCode":"SE","currency":"SEK","district":"","hosting":false,"lat":56.1625,"lon":15.5801,"proxy":false,"region":"K","regionName":"Blekinge County","status":"success","timezone":"Europe/Stockholm","zip":"371 37"}";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
s:92:"{"status":"fail","message":"invalid/expired key, renew at https://members.ip-api.com/order"}";

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
s:265:"{"callingCode":"1","city":"Tulsa","country":"United States","countryCode":"US","currency":"USD","district":"","hosting":true,"lat":36.15398,"lon":-95.99277,"proxy":false,"region":"OK","regionName":"Oklahoma","status":"success","timezone":"America/Chicago","zip":""}";
22 changes: 18 additions & 4 deletions src/Provider/IpApi/Tests/IpApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Geocoder\Provider\IpApi\Tests;

use Geocoder\Exception\InvalidServerResponse;
use Geocoder\Exception\InvalidCredentials;
use Geocoder\Exception\UnsupportedOperation;
use Geocoder\IntegrationTest\BaseTestCase;
use Geocoder\Location;
Expand All @@ -38,10 +38,10 @@ public function testGetName(): void

public function testInvalidApiKey(): void
{
$provider = new IpApi($this->getMockedHttpClient(), 'InVaLiDkEy');
$provider = new IpApi($this->getHttpClient('InVaLiDkEy'), 'InVaLiDkEy');

$this->expectException(InvalidServerResponse::class);
$provider->geocodeQuery(GeocodeQuery::create('74.125.45.100'));
$this->expectException(InvalidCredentials::class);
$provider->geocodeQuery(GeocodeQuery::create('64.233.160.0'));
}

public function testGeocodeWithAddress(): void
Expand Down Expand Up @@ -108,6 +108,13 @@ public function testGeocodeWithRealIPv4(string|null $apiKey): void
$this->assertEquals('Oklahoma', $result->getAdminLevels()->get(1)->getName());
$this->assertEquals('United States', $result->getCountry()->getName());
$this->assertEquals('US', $result->getCountry()->getCode());
$this->assertFalse($result->isProxy());
$this->assertTrue($result->isHosting());

// Calling code is available only for authenticated calls
if (null !== $apiKey) {
$this->assertEquals('1', $result->getCallingCode());
}
}

/**
Expand All @@ -133,6 +140,13 @@ public function testGeocodeWithRealIPv6(string|null $apiKey): void
$this->assertEquals('Oklahoma', $result->getAdminLevels()->get(1)->getName());
$this->assertEquals('United States', $result->getCountry()->getName());
$this->assertEquals('US', $result->getCountry()->getCode());
$this->assertFalse($result->isProxy());
$this->assertTrue($result->isHosting());

// Calling code is available only for authenticated calls
if (null !== $apiKey) {
$this->assertEquals('1', $result->getCallingCode());
}
}

public function testReverse(): void
Expand Down

0 comments on commit 459855b

Please sign in to comment.