Skip to content

Commit

Permalink
fix: retry function
Browse files Browse the repository at this point in the history
  • Loading branch information
wtrocki committed Mar 27, 2024
1 parent 82401e4 commit fba8682
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
17 changes: 17 additions & 0 deletions examples/go.sum
@@ -0,0 +1,17 @@
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-test/deep v1.1.0/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-retryablehttp v0.7.5 h1:bJj+Pj19UZMIweq/iie+1u5YCdGrnxCT9yvm0e+Nd5M=
github.com/hashicorp/go-retryablehttp v0.7.5/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
github.com/mongodb-forks/digest v1.1.0 h1:7eUdsR1BtqLv0mdNm4OXs6ddWvR4X2/OsLwdKksrOoc=
github.com/mongodb-forks/digest v1.1.0/go.mod h1:rb+EX8zotClD5Dj4NdgxnJXG9nwrlx3NWKJ8xttz1Dg=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.mongodb.org/atlas-sdk/v20231115008 v20231115008.1.0 h1:WKPkreeCuIh2KKxfEP6KWIFNdNYaXnfSg79v2zAaSjw=
go.mongodb.org/atlas-sdk/v20231115008 v20231115008.1.0/go.mod h1:BHskDmYvvANe+s/HMkRhvK3GudGEZuzjYSbp1fBssdc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
22 changes: 14 additions & 8 deletions examples/retry/retry.go
Expand Up @@ -8,7 +8,6 @@ import (
"context"

"go.mongodb.org/atlas-sdk/v20231115008/admin"
"go.mongodb.org/atlas-sdk/v20231115008/examples"

retryablehttp "github.com/hashicorp/go-retryablehttp"
"github.com/mongodb-forks/digest"
Expand Down Expand Up @@ -37,34 +36,41 @@ func main() {
retryClient := retryablehttp.NewClient()
retryClient.RetryMax = 3

retryableClient := newRetryableClient(retryClient, apiKey, apiSecret)
retryableClient, err := newRetryableClient(retryClient, apiKey, apiSecret)
if err != nil{
log.Fatal("Cannot instantiate client")
}
sdk, err := admin.NewClient(
admin.UseHTTPClient(retryableClient),
admin.UseBaseURL(url),
admin.UseDebug(false))
examples.HandleErr(err, nil)
if err != nil{
log.Fatal(err)
}

request := sdk.ProjectsApi.ListProjectsWithParams(ctx,
&admin.ListProjectsApiParams{
ItemsPerPage: admin.PtrInt(1),
IncludeCount: admin.PtrBool(true),
PageNum: admin.PtrInt(1),
})
projects, response, err := request.Execute()
examples.HandleErr(err, response)
projects, _, err := request.Execute()
if err != nil{
log.Fatal(err)
}

if projects.GetTotalCount() == 0 {
log.Fatal("account should have at least single project")
}

}

func newRetryableClient(retryClient *retryablehttp.Client, apiKey string, apiSecret string) *http.Client {
func newRetryableClient(retryClient *retryablehttp.Client, apiKey string, apiSecret string) (*http.Client, error) {
var transport http.RoundTripper = &retryablehttp.RoundTripper{Client: retryClient}
digestRetryAbleTransport := digest.NewTransportWithHTTPRoundTripper(apiKey, apiSecret,transport)
retryableClient, err := digestRetryAbleTransport.Client()
if err != nil {
log.Fatal("Cannot instantiate client")
return nil, err;
}
return retryableClient
return retryableClient, nil
}

0 comments on commit fba8682

Please sign in to comment.