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

allow custom http client #27

Merged
merged 6 commits into from
Apr 6, 2018
Merged

Conversation

darshanime
Copy link
Contributor

@darshanime darshanime commented Mar 20, 2018

this pr is an attempt at allowing custom http clients (#20)

We can set a custom http client now using SetCustomHTTPClient

	client.SetCustomHTTPClient(&myHTTPClient{
		client: http.Client{Timeout: 25 * time.Millisecond}})

The custom client needs to implement the Doer interface which is also implemented by the
http.Client in stdlib.

@darshanime darshanime changed the title wip - allow custom http client allow custom http client Mar 21, 2018
@darshanime
Copy link
Contributor Author

will update documentation once the approach looks correct
gentle request for review @rShetty

@@ -16,4 +22,5 @@ type Client interface {

SetRetryCount(count int)
SetRetrier(retrier Retriable)
SetCustomHTTPClient(customHTTPClient Doer)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any specific reason to accept any Doer instead of just *http.Client ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rShetty this is so that one can use a custom HTTPClient here, not just the one defined in one's code, but one that is defined in any imported library etc.

This blog post talks about the advantages of accepting Doer over *http.Client (tl;dr composability). Also, I have seen packages use Doer to accept custom clients like fetchbot

Copy link
Contributor

Choose a reason for hiding this comment

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

👍


func (c *myHTTPClient) Do(request *http.Request) (*http.Response, error) {
request.Header.Set("foo", "bar")
response, err := c.client.Do(request)
Copy link
Contributor

Choose a reason for hiding this comment

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

You can just do return c.client.Do(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.

yup, made the change 👍

@rShetty
Copy link
Contributor

rShetty commented Mar 23, 2018

@darshanime Thanks for this commit, have added a couple of questions to the PR. Rest all looks good to me

@rShetty
Copy link
Contributor

rShetty commented Mar 27, 2018

@darshanime This looks good, can we go ahead and add a section in documentation of the usage. And also an example in examples folder

@darshanime
Copy link
Contributor Author

@rShetty please let me know if the documentation looks okay!

@rShetty
Copy link
Contributor

rShetty commented Apr 6, 2018

@darshanime Docs look fine. Thanks for the PR. Merging

@rShetty rShetty merged commit badd722 into gojek:master Apr 6, 2018
@darshanime darshanime deleted the custom_http_client branch April 6, 2018 08:33
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.

2 participants