-
Notifications
You must be signed in to change notification settings - Fork 1
Add reusable GeoIP client #7
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
premtsd-code
wants to merge
2
commits into
appwrite:main
Choose a base branch
from
premtsd-code:feat-geoip-client
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,55 @@ | ||
| <?php | ||
|
|
||
| namespace Appwrite\Geo; | ||
|
|
||
| use Throwable; | ||
| use Utopia\Fetch\Client; | ||
|
|
||
| final readonly class GeoIp | ||
| { | ||
| private Client $client; | ||
|
|
||
| public function __construct( | ||
| private string $endpoint, | ||
| private string $secret, | ||
| ?Client $client = null, | ||
| ) { | ||
| $this->client = $client ?? new Client(); | ||
| $this->client | ||
| ->addHeader('Accept', Client::CONTENT_TYPE_APPLICATION_JSON) | ||
| ->setTimeout(1000); | ||
| } | ||
|
|
||
| public function getCountryCode(string $ip): ?string | ||
| { | ||
| $geo = $this->get($ip); | ||
|
|
||
| return $geo?->getCountryCode(); | ||
| } | ||
|
|
||
| public function get(string $ip): ?GeoRecord | ||
| { | ||
| if ($this->endpoint === '' || $this->secret === '') { | ||
| return null; | ||
| } | ||
|
|
||
| try { | ||
| $response = $this->client | ||
| ->addHeader('Authorization', 'Bearer ' . $this->secret) | ||
| ->fetch( | ||
| \rtrim($this->endpoint, '/') . '/v1/ips/' . \rawurlencode($ip), | ||
| Client::METHOD_GET, | ||
| ); | ||
|
|
||
| if ($response->getStatusCode() >= 400) { | ||
| return null; | ||
| } | ||
|
|
||
| $body = \json_decode((string) $response->getBody(), true); | ||
| } catch (Throwable) { | ||
| return null; | ||
| } | ||
|
|
||
| return \is_array($body) ? GeoRecord::fromArray($body) : null; | ||
| } | ||
| } |
This file contains hidden or 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,44 @@ | ||
| <?php | ||
|
|
||
| namespace Appwrite\Geo; | ||
|
|
||
| final readonly class GeoRecord | ||
| { | ||
| /** | ||
| * @param array<string, mixed> $data | ||
| */ | ||
| public function __construct( | ||
| private array $data, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, mixed> $data | ||
| */ | ||
| public static function fromArray(array $data): self | ||
| { | ||
| return new self($data); | ||
| } | ||
|
|
||
| public function getCountryCode(): ?string | ||
| { | ||
| $countryCode = $this->data['countryCode'] ?? null; | ||
|
|
||
| return \is_string($countryCode) && $countryCode !== '' | ||
| ? $countryCode | ||
| : null; | ||
| } | ||
|
|
||
| public function get(string $key): mixed | ||
| { | ||
| return $this->data[$key] ?? null; | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function toArray(): array | ||
| { | ||
| return $this->data; | ||
| } | ||
| } |
This file contains hidden or 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,134 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Unit; | ||
|
|
||
| use Appwrite\Geo\GeoIp; | ||
| use Appwrite\Geo\GeoRecord; | ||
| use PHPUnit\Framework\TestCase; | ||
| use RuntimeException; | ||
| use Utopia\Fetch\Client as FetchClient; | ||
| use Utopia\Fetch\Response; | ||
|
|
||
| final class GeoIpTest extends TestCase | ||
| { | ||
| public function testGetReturnsGeoRecord(): void | ||
| { | ||
| $client = $this->createMock(FetchClient::class); | ||
| $client | ||
| ->expects($this->exactly(2)) | ||
| ->method('addHeader') | ||
| ->willReturnCallback(function (string $key, string $value) use ($client): FetchClient { | ||
| if ($key === 'Authorization') { | ||
| $this->assertSame('Bearer secret', $value); | ||
| return $client; | ||
| } | ||
|
|
||
| $this->assertSame('Accept', $key); | ||
| $this->assertSame(FetchClient::CONTENT_TYPE_APPLICATION_JSON, $value); | ||
|
|
||
| return $client; | ||
| }); | ||
| $client | ||
| ->expects($this->once()) | ||
| ->method('setTimeout') | ||
| ->with(1000) | ||
| ->willReturn($client); | ||
| $client | ||
| ->expects($this->once()) | ||
| ->method('fetch') | ||
| ->with('http://geo/v1/ips/8.8.8.8', FetchClient::METHOD_GET) | ||
| ->willReturn(new Response(200, '{"countryCode":"US","city":{"en":"Mountain View"}}', [])); | ||
|
|
||
| $geo = new GeoIp('http://geo/', 'secret', $client); | ||
| $record = $geo->get('8.8.8.8'); | ||
|
|
||
| $this->assertInstanceOf(GeoRecord::class, $record); | ||
| $this->assertSame('US', $record->getCountryCode()); | ||
| $this->assertSame(['en' => 'Mountain View'], $record->get('city')); | ||
| $this->assertSame([ | ||
| 'countryCode' => 'US', | ||
| 'city' => [ | ||
| 'en' => 'Mountain View', | ||
| ], | ||
| ], $record->toArray()); | ||
| } | ||
|
|
||
| public function testGetCountryCodeReturnsCountryCode(): void | ||
| { | ||
| $geo = new GeoIp('http://geo', 'secret', $this->clientReturning(new Response(200, '{"countryCode":"US"}', []))); | ||
|
|
||
| $this->assertSame('US', $geo->getCountryCode('8.8.8.8')); | ||
| } | ||
|
|
||
| public function testGetCountryCodeReturnsNullWhenCountryCodeIsMissing(): void | ||
| { | ||
| $geo = new GeoIp('http://geo', 'secret', $this->clientReturning(new Response(200, '{}', []))); | ||
|
|
||
| $this->assertNull($geo->getCountryCode('8.8.8.8')); | ||
| } | ||
|
|
||
| public function testGetReturnsNullOnHttpErrors(): void | ||
| { | ||
| $geo = new GeoIp('http://geo', 'secret', $this->clientReturning(new Response(500, '{}', []))); | ||
|
|
||
| $this->assertNull($geo->get('8.8.8.8')); | ||
| } | ||
|
|
||
| public function testGetReturnsNullOnInvalidJson(): void | ||
| { | ||
| $geo = new GeoIp('http://geo', 'secret', $this->clientReturning(new Response(200, 'not-json', []))); | ||
|
|
||
| $this->assertNull($geo->get('8.8.8.8')); | ||
| } | ||
|
|
||
| public function testGetReturnsNullOnFetchExceptions(): void | ||
| { | ||
| $client = $this->createStub(FetchClient::class); | ||
| $client | ||
| ->method('addHeader') | ||
| ->willReturn($client); | ||
| $client | ||
| ->method('setTimeout') | ||
| ->willReturn($client); | ||
| $client | ||
| ->method('fetch') | ||
| ->willThrowException(new RuntimeException('timeout')); | ||
|
|
||
| $geo = new GeoIp('http://geo', 'secret', $client); | ||
|
|
||
| $this->assertNull($geo->get('8.8.8.8')); | ||
| } | ||
|
|
||
| public function testGetDoesNotCallClientWithoutEndpointOrSecret(): void | ||
| { | ||
| $client = $this->createMock(FetchClient::class); | ||
| $client | ||
| ->method('addHeader') | ||
| ->willReturn($client); | ||
| $client | ||
| ->method('setTimeout') | ||
| ->willReturn($client); | ||
| $client | ||
| ->expects($this->never()) | ||
| ->method('fetch'); | ||
|
|
||
| $this->assertNull(new GeoIp('', 'secret', $client)->get('8.8.8.8')); | ||
| $this->assertNull(new GeoIp('http://geo', '', $client)->get('8.8.8.8')); | ||
| } | ||
|
|
||
| private function clientReturning(Response $response): FetchClient | ||
| { | ||
| $client = $this->createStub(FetchClient::class); | ||
| $client | ||
| ->method('addHeader') | ||
| ->willReturn($client); | ||
| $client | ||
| ->method('setTimeout') | ||
| ->willReturn($client); | ||
| $client | ||
| ->method('fetch') | ||
| ->willReturn($response); | ||
|
|
||
| return $client; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.