-
Couldn't load subscription status.
- Fork 523
Add premium endpoint and token authentication support for Geonames provider #1261
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
AronNovak
wants to merge
4
commits into
geocoder-php:master
Choose a base branch
from
AronNovak:feature/geonames-premium-support
base: master
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 1 commit
Commits
Show all changes
4 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,17 +52,38 @@ final class Geonames extends AbstractHttpProvider implements Provider | |
| */ | ||
| private $username; | ||
|
|
||
| /** | ||
| * @var string|null | ||
| */ | ||
| private $token; | ||
|
|
||
| /** | ||
| * @var string | ||
| */ | ||
| private $baseUrl; | ||
|
|
||
| /** | ||
| * @param ClientInterface $client An HTTP adapter | ||
| * @param string $username Username login (Free registration at http://www.geonames.org/login) | ||
| * @param string|null $token Optional token for premium accounts | ||
| * @param bool $secure Use secure endpoint (https://secure.geonames.net) for premium accounts | ||
| */ | ||
| public function __construct(ClientInterface $client, string $username) | ||
| public function __construct(ClientInterface $client, string $username, ?string $token = null, bool $secure = false) | ||
| { | ||
| if (empty($username)) { | ||
| throw new InvalidCredentials('No username provided.'); | ||
| } | ||
|
|
||
| $this->username = $username; | ||
| $this->token = $token; | ||
|
|
||
| // Determine base URL based on secure flag | ||
| if ($secure) { | ||
| $this->baseUrl = 'https://secure.geonames.net'; | ||
| } else { | ||
| $this->baseUrl = 'http://api.geonames.org'; | ||
| } | ||
|
||
|
|
||
| parent::__construct($client); | ||
| } | ||
|
|
||
|
|
@@ -75,7 +96,15 @@ public function geocodeQuery(GeocodeQuery $query): Collection | |
| throw new UnsupportedOperation('The Geonames provider does not support IP addresses.'); | ||
| } | ||
|
|
||
| $url = sprintf(self::GEOCODE_ENDPOINT_URL, urlencode($address), $query->getLimit(), $this->username); | ||
| $url = sprintf( | ||
| '%s/searchJSON?q=%s&maxRows=%d&style=full&username=%s', | ||
| $this->baseUrl, | ||
| urlencode($address), | ||
| $query->getLimit(), | ||
| $this->username | ||
| ); | ||
|
|
||
| $url = $this->appendToken($url); | ||
|
|
||
| return $this->executeQuery($url, $query->getLocale()); | ||
| } | ||
|
|
@@ -86,7 +115,16 @@ public function reverseQuery(ReverseQuery $query): Collection | |
| $longitude = $coordinates->getLongitude(); | ||
| $latitude = $coordinates->getLatitude(); | ||
|
|
||
| $url = sprintf(self::REVERSE_ENDPOINT_URL, $latitude, $longitude, $query->getLimit(), $this->username); | ||
| $url = sprintf( | ||
| '%s/findNearbyPlaceNameJSON?lat=%F&lng=%F&style=full&maxRows=%d&username=%s', | ||
| $this->baseUrl, | ||
| $latitude, | ||
| $longitude, | ||
| $query->getLimit(), | ||
| $this->username | ||
| ); | ||
|
|
||
| $url = $this->appendToken($url); | ||
|
|
||
| return $this->executeQuery($url, $query->getLocale()); | ||
| } | ||
|
|
@@ -96,7 +134,7 @@ public function reverseQuery(ReverseQuery $query): Collection | |
| */ | ||
| public function getCountryInfo(?string $country = null, ?string $locale = null): array | ||
| { | ||
| $url = sprintf(self::BASE_ENDPOINT_URL, 'countryInfoJSON', $this->username); | ||
| $url = sprintf('%s/countryInfoJSON?username=%s', $this->baseUrl, $this->username); | ||
|
|
||
| if (isset($country)) { | ||
| $url = sprintf('%s&country=%s', $url, $country); | ||
|
|
@@ -109,6 +147,8 @@ public function getCountryInfo(?string $country = null, ?string $locale = null): | |
| $url = sprintf('%s&lang=%s', $url, substr($locale, 0, 2)); | ||
| } | ||
|
|
||
| $url = $this->appendToken($url); | ||
|
|
||
| $content = $this->getUrlContents($url); | ||
| if (null === $json = json_decode($content)) { | ||
| throw InvalidServerResponse::create($url); | ||
|
|
@@ -217,4 +257,16 @@ private function executeQuery(string $url, ?string $locale = null): AddressColle | |
|
|
||
| return new AddressCollection($results); | ||
| } | ||
|
|
||
| /** | ||
| * Append token parameter to URL if token is provided. | ||
| */ | ||
| private function appendToken(string $url): string | ||
| { | ||
| if (null !== $this->token && '' !== $this->token) { | ||
| $url = sprintf('%s&token=%s', $url, $this->token); | ||
| } | ||
|
|
||
| return $url; | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use strict comparison.