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

release: 0.1.0-alpha.3 #7

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.1.0-alpha.2"
".": "0.1.0-alpha.3"
}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
8 changes: 4 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<your-example>/main.go
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Or to pin the version:
<!-- x-release-please-start-version -->

```sh
go get -u 'github.com/OneBusAway/[email protected].2'
go get -u 'github.com/OneBusAway/[email protected].3'
```

<!-- x-release-please-end -->
Expand Down
74 changes: 70 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"fmt"
"net/http"
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -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{
Expand All @@ -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)
}
}

Expand Down
11 changes: 10 additions & 1 deletion internal/requestconfig/requestconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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()
Expand All @@ -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()
}
Expand Down
2 changes: 1 addition & 1 deletion internal/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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