Skip to content

Commit c0d6eeb

Browse files
fix(client): resolve lint errors in streaming tests
1 parent f3586bd commit c0d6eeb

File tree

3 files changed

+54
-20
lines changed

3 files changed

+54
-20
lines changed

README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@ import (
4949

5050
func main() {
5151
client := opencode.NewClient()
52-
events, err := client.Event.List(context.TODO())
52+
stream := client.Event.ListStreaming(context.TODO())
53+
for stream.Next() {
54+
fmt.Printf("%+v\n", stream.Current())
55+
}
56+
err := stream.Err()
5357
if err != nil {
5458
panic(err.Error())
5559
}
56-
fmt.Printf("%+v\n", events)
5760
}
5861

5962
```
@@ -171,14 +174,14 @@ When the API returns a non-success status code, we return an error with type
171174
To handle errors, we recommend that you use the `errors.As` pattern:
172175

173176
```go
174-
_, err := client.Event.List(context.TODO())
175-
if err != nil {
177+
stream := client.Event.ListStreaming(context.TODO())
178+
if stream.Err() != nil {
176179
var apierr *opencode.Error
177-
if errors.As(err, &apierr) {
180+
if errors.As(stream.Err(), &apierr) {
178181
println(string(apierr.DumpRequest(true))) // Prints the serialized HTTP request
179182
println(string(apierr.DumpResponse(true))) // Prints the serialized HTTP response
180183
}
181-
panic(err.Error()) // GET "/event": 400 Bad Request { ... }
184+
panic(stream.Err().Error()) // GET "/event": 400 Bad Request { ... }
182185
}
183186
```
184187

@@ -196,7 +199,7 @@ To set a per-retry timeout, use `option.WithRequestTimeout()`.
196199
// This sets the timeout for the request, including all the retries.
197200
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
198201
defer cancel()
199-
client.Event.List(
202+
client.Event.ListStreaming(
200203
ctx,
201204
// This sets the per-retry timeout
202205
option.WithRequestTimeout(20*time.Second),
@@ -231,7 +234,7 @@ client := opencode.NewClient(
231234
)
232235

233236
// Override per-request:
234-
client.Event.List(context.TODO(), option.WithMaxRetries(5))
237+
client.Event.ListStreaming(context.TODO(), option.WithMaxRetries(5))
235238
```
236239

237240
### Accessing raw response data (e.g. response headers)
@@ -242,8 +245,8 @@ you need to examine response headers, status codes, or other details.
242245
```go
243246
// Create a variable to store the HTTP response
244247
var response *http.Response
245-
events, err := client.Event.List(context.TODO(), option.WithResponseInto(&response))
246-
if err != nil {
248+
stream := client.Event.ListStreaming(context.TODO(), option.WithResponseInto(&response))
249+
if stream.Err() != nil {
247250
// handle error
248251
}
249252
fmt.Printf("%+v\n", events)

client_test.go

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestUserAgentHeader(t *testing.T) {
3838
},
3939
}),
4040
)
41-
client.Event.List(context.Background())
41+
client.Event.ListStreaming(context.Background())
4242
if userAgent != fmt.Sprintf("Opencode/Go %s", internal.PackageVersion) {
4343
t.Errorf("Expected User-Agent to be correct, but got: %#v", userAgent)
4444
}
@@ -61,7 +61,11 @@ func TestRetryAfter(t *testing.T) {
6161
},
6262
}),
6363
)
64-
_, err := client.Event.List(context.Background())
64+
stream := client.Event.ListStreaming(context.Background())
65+
for stream.Next() {
66+
// ...
67+
}
68+
err := stream.Err()
6569
if err == nil {
6670
t.Error("Expected there to be a cancel error")
6771
}
@@ -95,7 +99,11 @@ func TestDeleteRetryCountHeader(t *testing.T) {
9599
}),
96100
option.WithHeaderDel("X-Stainless-Retry-Count"),
97101
)
98-
_, err := client.Event.List(context.Background())
102+
stream := client.Event.ListStreaming(context.Background())
103+
for stream.Next() {
104+
// ...
105+
}
106+
err := stream.Err()
99107
if err == nil {
100108
t.Error("Expected there to be a cancel error")
101109
}
@@ -124,7 +132,11 @@ func TestOverwriteRetryCountHeader(t *testing.T) {
124132
}),
125133
option.WithHeader("X-Stainless-Retry-Count", "42"),
126134
)
127-
_, err := client.Event.List(context.Background())
135+
stream := client.Event.ListStreaming(context.Background())
136+
for stream.Next() {
137+
// ...
138+
}
139+
err := stream.Err()
128140
if err == nil {
129141
t.Error("Expected there to be a cancel error")
130142
}
@@ -152,7 +164,11 @@ func TestRetryAfterMs(t *testing.T) {
152164
},
153165
}),
154166
)
155-
_, err := client.Event.List(context.Background())
167+
stream := client.Event.ListStreaming(context.Background())
168+
for stream.Next() {
169+
// ...
170+
}
171+
err := stream.Err()
156172
if err == nil {
157173
t.Error("Expected there to be a cancel error")
158174
}
@@ -174,7 +190,11 @@ func TestContextCancel(t *testing.T) {
174190
)
175191
cancelCtx, cancel := context.WithCancel(context.Background())
176192
cancel()
177-
_, err := client.Event.List(cancelCtx)
193+
stream := client.Event.ListStreaming(cancelCtx)
194+
for stream.Next() {
195+
// ...
196+
}
197+
err := stream.Err()
178198
if err == nil {
179199
t.Error("Expected there to be a cancel error")
180200
}
@@ -193,7 +213,11 @@ func TestContextCancelDelay(t *testing.T) {
193213
)
194214
cancelCtx, cancel := context.WithTimeout(context.Background(), 2*time.Millisecond)
195215
defer cancel()
196-
_, err := client.Event.List(cancelCtx)
216+
stream := client.Event.ListStreaming(cancelCtx)
217+
for stream.Next() {
218+
// ...
219+
}
220+
err := stream.Err()
197221
if err == nil {
198222
t.Error("expected there to be a cancel error")
199223
}
@@ -218,7 +242,11 @@ func TestContextDeadline(t *testing.T) {
218242
},
219243
}),
220244
)
221-
_, err := client.Event.List(deadlineCtx)
245+
stream := client.Event.ListStreaming(deadlineCtx)
246+
for stream.Next() {
247+
// ...
248+
}
249+
err := stream.Err()
222250
if err == nil {
223251
t.Error("expected there to be a deadline error")
224252
}

usage_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ func TestUsage(t *testing.T) {
2323
client := opencode.NewClient(
2424
option.WithBaseURL(baseURL),
2525
)
26-
events, err := client.Event.List(context.TODO())
26+
stream := client.Event.ListStreaming(context.TODO())
27+
for stream.Next() {
28+
t.Logf("%+v\n", stream.Current())
29+
}
30+
err := stream.Err()
2731
if err != nil {
2832
t.Error(err)
2933
return
3034
}
31-
t.Logf("%+v\n", events)
3235
}

0 commit comments

Comments
 (0)