-
Notifications
You must be signed in to change notification settings - Fork 17
feat: option-to-pass-optional-resty-or-http-client #161
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
| } | ||
|
|
||
| for _, opt := range options { | ||
| name := getOptionQualifiedName(opt) |
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.
Basically here I detect client related options based on names. They need to be ran first because some options requires the client
e.g
func WithCustomHeaders(headers map[string]string) Option {
return func(c *Client) {
c.client.SetHeaders(headers)
}
}
A clean alternative would be to change the option type like
type Option {
apply: func(c *Client)
isInit: bool
}
It's quite a big refacto so let me know if it's worth going into this
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.
What do we think about throwing an error if someone sets a custom client with options like timeout, etc
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.
Good point. IMO timeout could be part of their custom client options that some users might like to keep.
I added a guard if no timeout specified here
but it could make sense to have a upper limit too (15sec) ?
Wdyt ?
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.
Sorry, I meant the user should not be able to set conflicting options. For example, if they are setting an HTTP client, they should not be able to set a Resty client and vice versa. Additionally, if they are passing a custom client, they should not be able to use any options that modify the client.
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.
Done, so now:
- Resty + Http client causes panic instead of prioritizing the resty client
- WithRequestTimeout / WithRetries / WithCustomHeaders / WithProxy will throw a panic if provided with custom clients
|
https://github.com/Flagsmith-Support/Axis/issues/11#issuecomment-2012242615 Also from this comment. When a custom client is provided, do we want to disable the following ? |
Yes, I think that makes sense. |
| } | ||
|
|
||
| for _, opt := range options { | ||
| name := getOptionQualifiedName(opt) |
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.
What do we think about throwing an error if someone sets a custom client with options like timeout, etc
| run: go test -v -race ./... | ||
| run: | | ||
| go test -v -race ./... | ||
| go test -tags=test ./... |
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.
Added this. Specific test cases in which I re-expose the resty client to test the values
| return http.DefaultTransport.RoundTrip(req) | ||
| } | ||
|
|
||
| func TestCustomHTTPClientIsUsed(t *testing.T) { |
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.
Nit: We follow the Given-When-Then comment pattern, which you are already using. However, it would be great if we explicitly mark those sections using comments.
Thanks for submitting a PR! Please check the boxes below:
docs/if required so people know about the feature!Changes
This PR aims to allow usage of custom client (HTTP or Resty) with priority over Resty client if both are provided.
WithRestyClientandWithHttpClientHow did you test this code?
Manual tests:
https://github.com/Zaimwa9/flagsmith-go-sandbox
feat/custom-clients-with-loggerse.g Test request logging with following round tripper configuration for custom HTTP Client
e.g Test request logging with following round tripper configuration for custom Resty Client