Skip to content

Commit f0d3293

Browse files
fix: disable automatic vault operation retries in SDKs
Stainless-Generated-From: 2730d6cb42d5bed79ac3cebd5429a13264e4eedb
1 parent bcf94cc commit f0d3293

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

vault_operation_retries_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package kernel_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
"net/http"
8+
"strings"
9+
"testing"
10+
11+
kernel "github.com/kernel/kernel-go-sdk"
12+
"github.com/kernel/kernel-go-sdk/option"
13+
)
14+
15+
type vaultRetryTransport func(*http.Request) (*http.Response, error)
16+
17+
func (f vaultRetryTransport) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
18+
19+
func TestVaultFillDoesNotRetry(t *testing.T) {
20+
for _, status := range []int{0, 409, 429, 500} {
21+
t.Run(fmt.Sprint(status), func(t *testing.T) {
22+
calls := 0
23+
client := kernel.NewClient(option.WithAPIKey("test"), option.WithMaxRetries(1), option.WithHTTPClient(&http.Client{Transport: vaultRetryTransport(func(r *http.Request) (*http.Response, error) {
24+
calls++
25+
if status == 0 {
26+
return nil, io.ErrUnexpectedEOF
27+
}
28+
return &http.Response{StatusCode: status, Header: http.Header{"Content-Type": {"application/json"}}, Body: io.NopCloser(strings.NewReader("{}")), Request: r}, nil
29+
})}))
30+
_, err := client.Vaults.Items.PerformOperation(context.Background(), "login", kernel.VaultItemPerformOperationParams{
31+
IDOrName: "vault", OfFill: &kernel.FillVaultItemOperationRequestParam{BrowserID: "browser", Fields: []kernel.VaultFillFieldParam{{Field: "password", Selector: "#password"}}},
32+
})
33+
if err == nil {
34+
t.Fatal("expected error")
35+
}
36+
if calls != 1 {
37+
t.Fatalf("got %d attempts, want 1", calls)
38+
}
39+
})
40+
}
41+
}
42+
43+
func TestVaultReadKeepsClientRetries(t *testing.T) {
44+
calls := 0
45+
client := kernel.NewClient(option.WithAPIKey("test"), option.WithMaxRetries(1), option.WithHTTPClient(&http.Client{Transport: vaultRetryTransport(func(r *http.Request) (*http.Response, error) {
46+
calls++
47+
return nil, io.ErrUnexpectedEOF
48+
})}))
49+
_, err := client.Vaults.Items.Get(context.Background(), "login", kernel.VaultItemGetParams{IDOrName: "vault"})
50+
if err == nil {
51+
t.Fatal("expected error")
52+
}
53+
if calls != 2 {
54+
t.Fatalf("got %d attempts, want 2", calls)
55+
}
56+
}

vaultitem.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (r *VaultItemService) Events(ctx context.Context, key string, params VaultI
161161
// `failed` or `unknown`, not an automatic-retry signal. A transport error may
162162
// leave the outcome unknown; do not automatically retry.
163163
func (r *VaultItemService) PerformOperation(ctx context.Context, key string, params VaultItemPerformOperationParams, opts ...option.RequestOption) (res *VaultItemOperationResponseUnion, err error) {
164-
opts = slices.Concat(r.Options, opts)
164+
opts = slices.Concat(r.Options, []option.RequestOption{option.WithMaxRetries(0)}, opts)
165165
if params.IDOrName == "" {
166166
err = errors.New("missing required id_or_name parameter")
167167
return nil, err

0 commit comments

Comments
 (0)