Skip to content
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
10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

## [Unreleased](https://github.com/openfga/go-sdk/compare/v0.7.5...HEAD)

- feat!: add `ExecuteStreaming` to `APIExecutor` for streaming any OpenFGA endpoint via the generic executor. Check out the [documentation](./README.md#calling-other-endpoints)
- feat(telemetry): add `fga-client.request.count` metric to track total HTTP requests made by the SDK
- fix: The `fga-client.http_request.duration` metric is now disabled by default. Users can enable it via telemetry configuration if needed.

[!WARNING]
BREAKING CHANGE:
The default behavior changed, and fga-client.http_request.duration is now disabled unless explicitly enabled.
> [!WARNING]
> BREAKING CHANGES (pre-v1, semver allows):
> - `OpenFgaApi`: `ApiStreamedListObjectsRequest.Execute()` now returns `(*StreamedListObjectsChannel, error)` instead of `(StreamResultOfStreamedListObjectsResponse, *http.Response, error)`
> - `OpenFgaApi`: `ApiStreamedListObjectsRequest.Options()` now takes `StreamingRequestOptions` instead of `RequestOptions`
> - Removed `ExecuteStreamedListObjects`, `ExecuteStreamedListObjectsWithBufferSize`, and `ProcessStreamedListObjectsResponse` from the `openfga` package. Use `fgaClient.StreamedListObjects(ctx).Body(...).Execute()` or `APIExecutor.ExecuteStreaming` instead
> - `fga-client.http_request.duration` metric is now disabled by default; enable it via telemetry configuration if needed

## v0.7.5

Expand Down
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,70 @@ fmt.Printf("Status Code: %d\n", rawResponse.StatusCode)
fmt.Printf("Headers: %+v\n", rawResponse.Headers)
```

#### Example: Calling streaming endpoints (e.g., StreamedListObjects)

For streaming API endpoints, use the `ExecuteStreaming` method. This is useful for endpoints like `StreamedListObjects` that stream results as they are computed rather than waiting for all results before responding.

```go
// Get the generic API executor
executor := fgaClient.GetAPIExecutor()

// Build a streaming request for StreamedListObjects
request := openfga.NewAPIExecutorRequestBuilder("StreamedListObjects", http.MethodPost, "/stores/{store_id}/streamed-list-objects").
WithPathParameter("store_id", storeID).
WithBody(openfga.ListObjectsRequest{
AuthorizationModelId: openfga.PtrString(modelID),
Type: "document",
Relation: "viewer",
User: "user:alice",
}).
Build()

// Execute the streaming request
// The bufferSize parameter controls how many results can be buffered in the channel
channel, err := executor.ExecuteStreaming(ctx, request, openfga.DefaultStreamBufferSize)
if err != nil {
log.Fatalf("Streaming request failed: %v", err)
}
defer channel.Close() // Always close the channel when done

// Process results as they stream in
for {
select {
case result, ok := <-channel.Results:
if !ok {
// Results channel closed, stream completed
// Check for any final errors
select {
case err := <-channel.Errors:
if err != nil {
log.Fatalf("Stream error: %v", err)
}
default:
}
fmt.Println("Stream completed successfully")
return
}
// Decode the raw JSON bytes into a typed response
var response openfga.StreamedListObjectsResponse
if err := json.Unmarshal(result, &response); err != nil {
log.Fatalf("Failed to decode stream result: %v", err)
}
fmt.Printf("Received object: %s\n", response.Object)
case err := <-channel.Errors:
if err != nil {
log.Fatalf("Stream error: %v", err)
}
}
}
```

The `ExecuteStreaming` method returns an `APIExecutorStreamingChannel` with:
- `Results chan []byte`: Raw JSON bytes for each streamed result
- `Errors chan error`: Any errors that occur during streaming
- `Close()`: Method to cancel streaming and cleanup resources


### Retries

If a network request fails with a 429 or 5xx error from the server, the SDK will automatically retry the request up to 3 times with a minimum wait time of 100 milliseconds between each attempt.
Expand Down
Loading
Loading