-
Notifications
You must be signed in to change notification settings - Fork 175
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
Enable specifying SendDecorators on a client #506
Conversation
Added field SendDecorators to autorest.Client; this value will be preferred over the default SendDecorators. Added method Client.Send() which includes logic for using custom SendDecorator chains.
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.
lgtm
@feiskyer PTAL, will wait for your approval before merging. |
Accompanying code generator change: Azure/autorest.go#261 |
This looks great. @jhendrixMSFT could you share an example how could it be used for Azure Go SDK clients? |
@feiskyer here's how you would set a custom retry policy on a client. There are similar examples in the test added in this PR. func MyCustomRetryPolicy(attempts int, backoff time.Duration, codes ...int) autorest.SendDecorator {
return func(s autorest.Sender) autorest.Sender {
return autorest.SenderFunc(func(r *http.Request) (*http.Response, error) {
// do work before sending the request to the next SendDecorator here ...
// pass the request to the next SendDecorator
resp, err := s.Do(r)
// do something with the response
if resp.StatusCode == http.StatusTooManyRequests {
// being throttled, handle as required....
}
// return response/error to my caller
return resp, err
})
}
}
func main() {
a, err := auth.NewAuthorizerFromEnvironment()
if err != nil {
panic(err)
}
client := redis.NewClient(subscriptionID)
client.SendDecorators = []autorest.SendDecorator{MyCustomRetryPolicy(3, 5*time.Second, http.StatusInternalServerError)}
client.Authorizer = a
// sync example
result, err := client.CheckNameAvailability(context.Background(), redis.CheckNameAvailabilityParameters{})
if err != nil && result.IsHTTPStatus(http.StatusTooManyRequests) {
// being throttled ...
}
} |
@jhendrixMSFT cool, thanks for the excellent example |
* Enable specifying SendDecorators on a client Added field SendDecorators to autorest.Client; this value will be preferred over the default SendDecorators. Added method Client.Send() which includes logic for using custom SendDecorator chains. * fix test to work with older versions of Go
Added field SendDecorators to autorest.Client; this value will be
preferred over the default SendDecorators.
Added method Client.Send() which includes logic for using custom
SendDecorator chains.
Thank you for your contribution to Go-AutoRest! We will triage and review it as soon as we can.
As part of submitting, please make sure you can make the following assertions:
dev
branch, except in the case of urgent bug fixes warranting their own release.master
, I've updated CHANGELOG.md to address the changes I'm making.