-
Notifications
You must be signed in to change notification settings - Fork 523
Locale-aware FreeGeoIp provider #829
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
Conversation
Support for locales in the FreeGeoIp provider. Based on #744
|
Travis fails when running tests with the message Is it possible that the tests are run using the latest version of Geocoder, without the changes done in this PR? I see that |
No, I think you're right ! |
|
mmh that would be a bit weird not to use the patched code on travis-ci. |
For the high/low test, yes. But not for the "normal" tests. You need to make sure to save response cache after you run the tests locally. |
|
Now the "normal" tests work :) |
|
Im reviewing this PR more carefully now. Why are these extra helper functions needed in the AbstractHttpProvider? |
|
Because |
|
Yeah, but coudn't you just use the request factory and send the request without |
|
Do you mean to duplicate the code from
|
|
At the moment we have 30+ provider and it is not useful for any of them. But of course it could be revisited in the future. I think it would be cleaner not to edit the AbstractProvider at all. |
|
Ok. Should I change the PR to leave the |
|
Yes, it is just one or two rows that you duplicate. |
|
Sorry, then I'm not sure I understand what your idea is. I was thinking to make public function geocodeQuery(GeocodeQuery $query): Collection
{
$address = $query->getText();
if (!filter_var($address, FILTER_VALIDATE_IP)) {
throw new UnsupportedOperation('The FreeGeoIp provider does not support street addresses.');
}
if (in_array($address, ['127.0.0.1', '::1'])) {
return new AddressCollection([$this->getLocationForLocalhost()]);
}
$uri = sprintf($this->baseUrl, $address);
$request = $this->getMessageFactory()->createRequest('GET', $uri);
if (null !== $query->getLocale()) {
$request = $request->withHeader('Accept-Language', $query->getLocale());
}
// Start code duplication
$response = $this->getHttpClient()->sendRequest($request);
$statusCode = $response->getStatusCode();
if (401 === $statusCode || 403 === $statusCode) {
throw new InvalidCredentials();
} elseif (429 === $statusCode) {
throw new QuotaExceeded();
} elseif ($statusCode >= 300) {
throw InvalidServerResponse::create($uri, $statusCode);
}
$content = (string) $response->getBody();
if (empty($content)) {
throw InvalidServerResponse::emptyResponse($uri);
}
// End code duplication
$data = json_decode($content, true);
$builder = new AddressBuilder($this->getName());
if (!empty($data['region_name'])) {
$builder->addAdminLevel(1, $data['region_name'], $data['region_code'] ?? null);
}
if (0 !== $data['latitude'] || 0 !== $data['longitude']) {
$builder->setCoordinates($data['latitude'] ?? null, $data['longitude'] ?? null);
}
$builder->setLocality(empty($data['city']) ? null : $data['city']);
$builder->setPostalCode(empty($data['zip_code']) ? null : $data['zip_code']);
$builder->setCountry(empty($data['country_name']) ? null : $data['country_name']);
$builder->setCountryCode(empty($data['country_code']) ? null : $data['country_code']);
$builder->setTimezone(empty($data['time_zone']) ? null : $data['time_zone']);
return new AddressCollection([$builder->build()]);
} |
|
Just let me know if the solution in the comment above is preferable to what is currently in the PR, or if you have another solution in mind. |
|
Friendly ping :) |
|
@Stadly is right, the The method
@Stadly here split it in two, by making I had a similar problem in #840 where I needed to be able to do POST request as well as GET requests. So I spilt the method And it is not just a couple of lines of code duplication, but around 16-17 lines. And if @Nyholm and @willdurand decides that we are not allowed to change We need a decision from either @Nyholm or @willdurand. Are we allowed to change |
Reverting all changes to common files, so that all changes are done within the FreeGeoIp provider. This leads to some code duplication, but seems to be the preferred solution for now.
…oder into LocaleAwareFreeGeoIp
|
Based on the feedback both here and in #840, I have changed this PR to only alter FreeGeoIp files, and not touch anything else. So hopefully this PR can now be merged :) |
Nyholm
left a comment
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.
Thank you
|
Thank you for this PR. As @TerjeBr mentioned and as @Stadly initially wrote, it is better to modify the I will merge this PR now as it is super simple to review since you only modified one provider. Could you create a PR that only modifies that |
Support for locales in the FreeGeoIp provider.
Based on #744