mirror of
https://github.com/anomalyco/opencode-sdk-go.git
synced 2026-05-18 23:53:40 +00:00
fix(client): resolve lint errors in streaming tests
This commit is contained in:
parent
f3586bd6d7
commit
4d36cb09fc
3 changed files with 54 additions and 20 deletions
23
README.md
23
README.md
|
|
@ -49,11 +49,14 @@ import (
|
|||
|
||||
func main() {
|
||||
client := opencode.NewClient()
|
||||
events, err := client.Event.List(context.TODO())
|
||||
stream := client.Event.ListStreaming(context.TODO())
|
||||
for stream.Next() {
|
||||
fmt.Printf("%+v\n", stream.Current())
|
||||
}
|
||||
err := stream.Err()
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
fmt.Printf("%+v\n", events)
|
||||
}
|
||||
|
||||
```
|
||||
|
|
@ -171,14 +174,14 @@ When the API returns a non-success status code, we return an error with type
|
|||
To handle errors, we recommend that you use the `errors.As` pattern:
|
||||
|
||||
```go
|
||||
_, err := client.Event.List(context.TODO())
|
||||
if err != nil {
|
||||
stream := client.Event.ListStreaming(context.TODO())
|
||||
if stream.Err() != nil {
|
||||
var apierr *opencode.Error
|
||||
if errors.As(err, &apierr) {
|
||||
if errors.As(stream.Err(), &apierr) {
|
||||
println(string(apierr.DumpRequest(true))) // Prints the serialized HTTP request
|
||||
println(string(apierr.DumpResponse(true))) // Prints the serialized HTTP response
|
||||
}
|
||||
panic(err.Error()) // GET "/event": 400 Bad Request { ... }
|
||||
panic(stream.Err().Error()) // GET "/event": 400 Bad Request { ... }
|
||||
}
|
||||
```
|
||||
|
||||
|
|
@ -196,7 +199,7 @@ To set a per-retry timeout, use `option.WithRequestTimeout()`.
|
|||
// This sets the timeout for the request, including all the retries.
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
||||
defer cancel()
|
||||
client.Event.List(
|
||||
client.Event.ListStreaming(
|
||||
ctx,
|
||||
// This sets the per-retry timeout
|
||||
option.WithRequestTimeout(20*time.Second),
|
||||
|
|
@ -231,7 +234,7 @@ client := opencode.NewClient(
|
|||
)
|
||||
|
||||
// Override per-request:
|
||||
client.Event.List(context.TODO(), option.WithMaxRetries(5))
|
||||
client.Event.ListStreaming(context.TODO(), option.WithMaxRetries(5))
|
||||
```
|
||||
|
||||
### Accessing raw response data (e.g. response headers)
|
||||
|
|
@ -242,8 +245,8 @@ you need to examine response headers, status codes, or other details.
|
|||
```go
|
||||
// Create a variable to store the HTTP response
|
||||
var response *http.Response
|
||||
events, err := client.Event.List(context.TODO(), option.WithResponseInto(&response))
|
||||
if err != nil {
|
||||
stream := client.Event.ListStreaming(context.TODO(), option.WithResponseInto(&response))
|
||||
if stream.Err() != nil {
|
||||
// handle error
|
||||
}
|
||||
fmt.Printf("%+v\n", events)
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ func TestUserAgentHeader(t *testing.T) {
|
|||
},
|
||||
}),
|
||||
)
|
||||
client.Event.List(context.Background())
|
||||
client.Event.ListStreaming(context.Background())
|
||||
if userAgent != fmt.Sprintf("Opencode/Go %s", internal.PackageVersion) {
|
||||
t.Errorf("Expected User-Agent to be correct, but got: %#v", userAgent)
|
||||
}
|
||||
|
|
@ -61,7 +61,11 @@ func TestRetryAfter(t *testing.T) {
|
|||
},
|
||||
}),
|
||||
)
|
||||
_, err := client.Event.List(context.Background())
|
||||
stream := client.Event.ListStreaming(context.Background())
|
||||
for stream.Next() {
|
||||
// ...
|
||||
}
|
||||
err := stream.Err()
|
||||
if err == nil {
|
||||
t.Error("Expected there to be a cancel error")
|
||||
}
|
||||
|
|
@ -95,7 +99,11 @@ func TestDeleteRetryCountHeader(t *testing.T) {
|
|||
}),
|
||||
option.WithHeaderDel("X-Stainless-Retry-Count"),
|
||||
)
|
||||
_, err := client.Event.List(context.Background())
|
||||
stream := client.Event.ListStreaming(context.Background())
|
||||
for stream.Next() {
|
||||
// ...
|
||||
}
|
||||
err := stream.Err()
|
||||
if err == nil {
|
||||
t.Error("Expected there to be a cancel error")
|
||||
}
|
||||
|
|
@ -124,7 +132,11 @@ func TestOverwriteRetryCountHeader(t *testing.T) {
|
|||
}),
|
||||
option.WithHeader("X-Stainless-Retry-Count", "42"),
|
||||
)
|
||||
_, err := client.Event.List(context.Background())
|
||||
stream := client.Event.ListStreaming(context.Background())
|
||||
for stream.Next() {
|
||||
// ...
|
||||
}
|
||||
err := stream.Err()
|
||||
if err == nil {
|
||||
t.Error("Expected there to be a cancel error")
|
||||
}
|
||||
|
|
@ -152,7 +164,11 @@ func TestRetryAfterMs(t *testing.T) {
|
|||
},
|
||||
}),
|
||||
)
|
||||
_, err := client.Event.List(context.Background())
|
||||
stream := client.Event.ListStreaming(context.Background())
|
||||
for stream.Next() {
|
||||
// ...
|
||||
}
|
||||
err := stream.Err()
|
||||
if err == nil {
|
||||
t.Error("Expected there to be a cancel error")
|
||||
}
|
||||
|
|
@ -174,7 +190,11 @@ func TestContextCancel(t *testing.T) {
|
|||
)
|
||||
cancelCtx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
_, err := client.Event.List(cancelCtx)
|
||||
stream := client.Event.ListStreaming(cancelCtx)
|
||||
for stream.Next() {
|
||||
// ...
|
||||
}
|
||||
err := stream.Err()
|
||||
if err == nil {
|
||||
t.Error("Expected there to be a cancel error")
|
||||
}
|
||||
|
|
@ -193,7 +213,11 @@ func TestContextCancelDelay(t *testing.T) {
|
|||
)
|
||||
cancelCtx, cancel := context.WithTimeout(context.Background(), 2*time.Millisecond)
|
||||
defer cancel()
|
||||
_, err := client.Event.List(cancelCtx)
|
||||
stream := client.Event.ListStreaming(cancelCtx)
|
||||
for stream.Next() {
|
||||
// ...
|
||||
}
|
||||
err := stream.Err()
|
||||
if err == nil {
|
||||
t.Error("expected there to be a cancel error")
|
||||
}
|
||||
|
|
@ -218,7 +242,11 @@ func TestContextDeadline(t *testing.T) {
|
|||
},
|
||||
}),
|
||||
)
|
||||
_, err := client.Event.List(deadlineCtx)
|
||||
stream := client.Event.ListStreaming(deadlineCtx)
|
||||
for stream.Next() {
|
||||
// ...
|
||||
}
|
||||
err := stream.Err()
|
||||
if err == nil {
|
||||
t.Error("expected there to be a deadline error")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,10 +23,13 @@ func TestUsage(t *testing.T) {
|
|||
client := opencode.NewClient(
|
||||
option.WithBaseURL(baseURL),
|
||||
)
|
||||
events, err := client.Event.List(context.TODO())
|
||||
stream := client.Event.ListStreaming(context.TODO())
|
||||
for stream.Next() {
|
||||
t.Logf("%+v\n", stream.Current())
|
||||
}
|
||||
err := stream.Err()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
t.Logf("%+v\n", events)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue