From aac16f7a973c736257e7661b767e5743d9e35655 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Fri, 27 Sep 2024 23:35:10 +0000 Subject: [PATCH 1/3] chore(internal): codegen related update (#6) --- CONTRIBUTING.md | 8 +-- client_test.go | 74 +++++++++++++++++++++++-- internal/requestconfig/requestconfig.go | 11 +++- 3 files changed, 84 insertions(+), 9 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e1bf1fd..d4ef26f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -6,13 +6,13 @@ Install go by following relevant directions [here](https://go.dev/doc/install). ## Modifying/Adding code -Most of the SDK is generated code, and any modified code will be overridden on the next generation. The -`examples/` directory is an exception and will never be overridden. +Most of the SDK is generated code. Modifications to code will be persisted between generations, but may +result in merge conflicts between manual patches and changes from the generator. The generator will never +modify the contents of the `lib/` and `examples/` directories. ## Adding and running examples -All files in the `examples/` directory are not modified by the Stainless generator and can be freely edited or -added to. +All files in the `examples/` directory are not modified by the generator and can be freely edited or added to. ```bash # add an example to examples//main.go diff --git a/client_test.go b/client_test.go index 9fada93..eae64e5 100644 --- a/client_test.go +++ b/client_test.go @@ -6,6 +6,7 @@ import ( "context" "fmt" "net/http" + "reflect" "testing" "time" @@ -43,12 +44,12 @@ func TestUserAgentHeader(t *testing.T) { } func TestRetryAfter(t *testing.T) { - attempts := 0 + retryCountHeaders := make([]string, 0) client := onebusaway.NewClient( option.WithHTTPClient(&http.Client{ Transport: &closureTransport{ fn: func(req *http.Request) (*http.Response, error) { - attempts++ + retryCountHeaders = append(retryCountHeaders, req.Header.Get("X-Stainless-Retry-Count")) return &http.Response{ StatusCode: http.StatusTooManyRequests, Header: http.Header{ @@ -63,8 +64,73 @@ func TestRetryAfter(t *testing.T) { if err == nil || res != nil { t.Error("Expected there to be a cancel error and for the response to be nil") } - if want := 3; attempts != want { - t.Errorf("Expected %d attempts, got %d", want, attempts) + + attempts := len(retryCountHeaders) + if attempts != 3 { + t.Errorf("Expected %d attempts, got %d", 3, attempts) + } + + expectedRetryCountHeaders := []string{"0", "1", "2"} + if !reflect.DeepEqual(retryCountHeaders, expectedRetryCountHeaders) { + t.Errorf("Expected %v retry count headers, got %v", expectedRetryCountHeaders, retryCountHeaders) + } +} + +func TestDeleteRetryCountHeader(t *testing.T) { + retryCountHeaders := make([]string, 0) + client := onebusaway.NewClient( + option.WithHTTPClient(&http.Client{ + Transport: &closureTransport{ + fn: func(req *http.Request) (*http.Response, error) { + retryCountHeaders = append(retryCountHeaders, req.Header.Get("X-Stainless-Retry-Count")) + return &http.Response{ + StatusCode: http.StatusTooManyRequests, + Header: http.Header{ + http.CanonicalHeaderKey("Retry-After"): []string{"0.1"}, + }, + }, nil + }, + }, + }), + option.WithHeaderDel("X-Stainless-Retry-Count"), + ) + res, err := client.CurrentTime.Get(context.Background()) + if err == nil || res != nil { + t.Error("Expected there to be a cancel error and for the response to be nil") + } + + expectedRetryCountHeaders := []string{"", "", ""} + if !reflect.DeepEqual(retryCountHeaders, expectedRetryCountHeaders) { + t.Errorf("Expected %v retry count headers, got %v", expectedRetryCountHeaders, retryCountHeaders) + } +} + +func TestOverwriteRetryCountHeader(t *testing.T) { + retryCountHeaders := make([]string, 0) + client := onebusaway.NewClient( + option.WithHTTPClient(&http.Client{ + Transport: &closureTransport{ + fn: func(req *http.Request) (*http.Response, error) { + retryCountHeaders = append(retryCountHeaders, req.Header.Get("X-Stainless-Retry-Count")) + return &http.Response{ + StatusCode: http.StatusTooManyRequests, + Header: http.Header{ + http.CanonicalHeaderKey("Retry-After"): []string{"0.1"}, + }, + }, nil + }, + }, + }), + option.WithHeader("X-Stainless-Retry-Count", "42"), + ) + res, err := client.CurrentTime.Get(context.Background()) + if err == nil || res != nil { + t.Error("Expected there to be a cancel error and for the response to be nil") + } + + expectedRetryCountHeaders := []string{"42", "42", "42"} + if !reflect.DeepEqual(retryCountHeaders, expectedRetryCountHeaders) { + t.Errorf("Expected %v retry count headers, got %v", expectedRetryCountHeaders, retryCountHeaders) } } diff --git a/internal/requestconfig/requestconfig.go b/internal/requestconfig/requestconfig.go index 2d71285..8ba0275 100644 --- a/internal/requestconfig/requestconfig.go +++ b/internal/requestconfig/requestconfig.go @@ -137,6 +137,7 @@ func NewRequestConfig(ctx context.Context, method string, u string, body interfa } req.Header.Set("Accept", "application/json") + req.Header.Set("X-Stainless-Retry-Count", "0") for k, v := range getDefaultHeaders() { req.Header.Add(k, v) } @@ -331,6 +332,9 @@ func (cfg *RequestConfig) Execute() (err error) { handler = applyMiddleware(cfg.Middlewares[i], handler) } + // Don't send the current retry count in the headers if the caller modified the header defaults. + shouldSendRetryCount := cfg.Request.Header.Get("X-Stainless-Retry-Count") == "0" + var res *http.Response for retryCount := 0; retryCount <= cfg.MaxRetries; retryCount += 1 { ctx := cfg.Request.Context() @@ -340,7 +344,12 @@ func (cfg *RequestConfig) Execute() (err error) { defer cancel() } - res, err = handler(cfg.Request.Clone(ctx)) + req := cfg.Request.Clone(ctx) + if shouldSendRetryCount { + req.Header.Set("X-Stainless-Retry-Count", strconv.Itoa(retryCount)) + } + + res, err = handler(req) if ctx != nil && ctx.Err() != nil { return ctx.Err() } From dea4f082959691ff34f9501625ff6fc16d095c3b Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Fri, 27 Sep 2024 23:35:37 +0000 Subject: [PATCH 2/3] release: 0.1.0-alpha.3 --- CHANGELOG.md | 8 ++++++++ internal/version.go | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ad4d41..4efceb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## 0.1.0-alpha.3 (2024-09-27) + +Full Changelog: [v0.1.0-alpha.2...v0.1.0-alpha.3](https://github.com/OneBusAway/go-sdk/compare/v0.1.0-alpha.2...v0.1.0-alpha.3) + +### Chores + +* **internal:** codegen related update ([#6](https://github.com/OneBusAway/go-sdk/issues/6)) ([aac16f7](https://github.com/OneBusAway/go-sdk/commit/aac16f7a973c736257e7661b767e5743d9e35655)) + ## 0.1.0-alpha.2 (2024-09-12) Full Changelog: [v0.1.0-alpha.1...v0.1.0-alpha.2](https://github.com/OneBusAway/go-sdk/compare/v0.1.0-alpha.1...v0.1.0-alpha.2) diff --git a/internal/version.go b/internal/version.go index d6f40b3..2d1d85e 100644 --- a/internal/version.go +++ b/internal/version.go @@ -2,4 +2,4 @@ package internal -const PackageVersion = "0.1.0-alpha.2" // x-release-please-version +const PackageVersion = "0.1.0-alpha.3" // x-release-please-version From c57e49bb7730fd91f70e7f0fabf0c27c2a1d3138 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Fri, 27 Sep 2024 23:35:39 +0000 Subject: [PATCH 3/3] release: 0.1.0-alpha.3 --- .release-please-manifest.json | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.release-please-manifest.json b/.release-please-manifest.json index f14b480..aaf968a 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "0.1.0-alpha.2" + ".": "0.1.0-alpha.3" } \ No newline at end of file diff --git a/README.md b/README.md index b17e14f..6d65b55 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Or to pin the version: ```sh -go get -u 'github.com/OneBusAway/go-sdk@v0.1.0-alpha.2' +go get -u 'github.com/OneBusAway/go-sdk@v0.1.0-alpha.3' ```