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

Split getUrlContents #864

Merged
merged 2 commits into from Jun 27, 2018
Merged

Split getUrlContents #864

merged 2 commits into from Jun 27, 2018

Conversation

Stadly
Copy link
Contributor

@Stadly Stadly commented Jun 27, 2018

As requested in #829. Since getUrlContents() takes the url as a string argument, it is not possible to set headers, such as Accept-Language, for the request. Therefore I created getRequest() to generate the request based on the url string, but without fetching the contents. Then headers can be added to the request, before the request is passed to getRequestContents() which fetches the content.

Since `getUrlContents()` takes the url as a string argument, it is not possible to set headers, such as `Accept-Language`, for the request. Therefore I created `getRequest()` to generate the request based on the url string, but without fetching the contents. Then headers can be added to the request, before the request is passed to `getRequestContents()` which fetches the content.
Copy link
Member

@Nyholm Nyholm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great. I like this. Just some minor things.

$request = $this->getMessageFactory()->createRequest('GET', $url);
$request = $this->getRequest($url);

return $this->getRequestContents($request);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe rename this to parseResponse.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my reply to your other comment.

* @throws InvalidServerResponse
*/
protected function getRequestContents(RequestInterface $request): string
{
$response = $this->getHttpClient()->sendRequest($request);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think this function should send the request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could change getRequestContents(RequestInterface $request) to parseResponse(ResponseInterface $response), and call $response = $this->getHttpClient()->sendRequest($request) inside getUrlContents before calling parseResponse. The drawback would be that the request url would not be available inside parseResponse, so the method would require an extra parameter $url in order to throw InvalidServerResponse.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. I see. Hm. You could throw the exception and the catch it...

If you decide to still send the request in this function, I still think you should rename it. Maybe to “getparsedResponse” or something similar. A request does not really have “content”. It has a body of it is a POST request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I'll rename it. If a provider needs to access the response, it would be possible to move $response = $this->getHttpClient()->sendRequest($request); into a separate method sendRequest, that could be overridden by the provider.

Copy link
Member

@Nyholm Nyholm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@Nyholm Nyholm merged commit 7aea443 into geocoder-php:master Jun 27, 2018
@Stadly Stadly deleted the split-getUrlContents branch June 27, 2018 14:56
Nyholm pushed a commit that referenced this pull request Jun 28, 2018
* Use AbstractHttpProvider in FreeGeoIp

Use `getRequest` and `getParsedResponse` from `AbstractHttpProvider` instead of duplicating code in `FreeGeoIp`. See #864.

* Remove unnecessary uses

* Update dependencies
Nyholm pushed a commit to geocoder-php/free-geoip-provider that referenced this pull request Jun 28, 2018
* Use AbstractHttpProvider in FreeGeoIp

Use `getRequest` and `getParsedResponse` from `AbstractHttpProvider` instead of duplicating code in `FreeGeoIp`. See geocoder-php/Geocoder#864.

* Remove unnecessary uses

* Update dependencies
jbelien pushed a commit to geocoder-php/free-geoip-provider that referenced this pull request Jul 16, 2023
* Use AbstractHttpProvider in FreeGeoIp

Use `getRequest` and `getParsedResponse` from `AbstractHttpProvider` instead of duplicating code in `FreeGeoIp`. See geocoder-php/Geocoder#864.

* Remove unnecessary uses

* Update dependencies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants