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

Enable specifying SendDecorators on a client #506

Merged
merged 2 commits into from
Feb 19, 2020

Conversation

jhendrixMSFT
Copy link
Member

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:

  • I've tested my changes, adding unit tests if applicable.
  • I've added Apache 2.0 Headers to the top of any new source files.
  • I'm submitting this PR to the dev branch, except in the case of urgent bug fixes warranting their own release.
  • If I'm targeting master, I've updated CHANGELOG.md to address the changes I'm making.

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.
Copy link
Member

@catalinaperalta catalinaperalta left a comment

Choose a reason for hiding this comment

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

lgtm

@jhendrixMSFT
Copy link
Member Author

@feiskyer PTAL, will wait for your approval before merging.

@jhendrixMSFT
Copy link
Member Author

jhendrixMSFT commented Feb 14, 2020

Accompanying code generator change: Azure/autorest.go#261

@feiskyer
Copy link
Member

This looks great.

@jhendrixMSFT could you share an example how could it be used for Azure Go SDK clients?

@jhendrixMSFT
Copy link
Member Author

jhendrixMSFT commented Feb 15, 2020

@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 ...
    }
}

@feiskyer
Copy link
Member

@jhendrixMSFT cool, thanks for the excellent example

@jhendrixMSFT jhendrixMSFT merged commit 327ff6c into Azure:master Feb 19, 2020
@jhendrixMSFT jhendrixMSFT deleted the senddecorators branch February 19, 2020 05:02
alrs pushed a commit to alrs/go-autorest that referenced this pull request Mar 6, 2020
* 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
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.

3 participants