|
| 1 | +# Streamed List Objects Example |
| 2 | + |
| 3 | +This example demonstrates how to use the Streamed ListObjects API in the OpenFGA Java SDK. |
| 4 | + |
| 5 | +## What is Streamed ListObjects? |
| 6 | + |
| 7 | +The StreamedListObjects API is similar to the regular ListObjects API, but with key differences: |
| 8 | + |
| 9 | +1. **Streaming Response**: Instead of collecting all objects before returning a response, it streams them to the client as they are collected |
| 10 | +2. **No Result Limit**: The number of results returned is only limited by the execution timeout specified in the server configuration (`OPENFGA_LIST_OBJECTS_DEADLINE`) |
| 11 | +3. **Immediate Processing**: You can start processing results as they arrive, without waiting for the entire result set |
| 12 | + |
| 13 | +## When to Use Streamed ListObjects |
| 14 | + |
| 15 | +Use the Streamed ListObjects API when: |
| 16 | + |
| 17 | +- You expect a large number of results that would take a long time to collect |
| 18 | +- You want to start processing results immediately rather than waiting for the complete set |
| 19 | +- You might not need all results (e.g., you want to stop after finding a certain number) |
| 20 | +- You want to avoid timeout issues with very large result sets |
| 21 | + |
| 22 | +## Running the Example |
| 23 | + |
| 24 | +### Prerequisites |
| 25 | + |
| 26 | +- A running OpenFGA server (or use the [OpenFGA Playground](https://play.fga.dev/)) |
| 27 | + |
| 28 | +### Environment Variables |
| 29 | + |
| 30 | +Set the following environment variables: |
| 31 | + |
| 32 | +```bash |
| 33 | +# Required |
| 34 | +export FGA_API_URL=http://localhost:8080 # Your OpenFGA server URL |
| 35 | + |
| 36 | +# Optional - for authenticated servers |
| 37 | +export FGA_CLIENT_ID=your_client_id |
| 38 | +export FGA_CLIENT_SECRET=your_client_secret |
| 39 | +export FGA_API_TOKEN_ISSUER=your_token_issuer |
| 40 | +export FGA_API_AUDIENCE=your_audience |
| 41 | +``` |
| 42 | + |
| 43 | +### Running |
| 44 | + |
| 45 | +```bash |
| 46 | +make run |
| 47 | +``` |
| 48 | + |
| 49 | +## Code Examples |
| 50 | + |
| 51 | +### Basic Usage |
| 52 | + |
| 53 | +```java |
| 54 | +// Create a request |
| 55 | +var request = new ClientListObjectsRequest() |
| 56 | + .type("document") |
| 57 | + .relation("owner") |
| 58 | + .user("user:anne"); |
| 59 | + |
| 60 | +// Call the streaming API |
| 61 | +var objectStream = fgaClient.streamedListObjects(request).get(); |
| 62 | + |
| 63 | +// Collect all results |
| 64 | +List<String> objects = objectStream |
| 65 | + .map(StreamedListObjectsResponse::getObject) |
| 66 | + .collect(Collectors.toList()); |
| 67 | +``` |
| 68 | + |
| 69 | +### Early Termination |
| 70 | + |
| 71 | +```java |
| 72 | +// Get only the first 10 results |
| 73 | +var objectStream = fgaClient.streamedListObjects(request).get(); |
| 74 | +List<String> firstTen = objectStream |
| 75 | + .map(StreamedListObjectsResponse::getObject) |
| 76 | + .limit(10) |
| 77 | + .collect(Collectors.toList()); |
| 78 | +``` |
| 79 | + |
| 80 | +### Process as You Go |
| 81 | + |
| 82 | +```java |
| 83 | +// Process each object immediately as it arrives |
| 84 | +var objectStream = fgaClient.streamedListObjects(request).get(); |
| 85 | +objectStream |
| 86 | + .map(StreamedListObjectsResponse::getObject) |
| 87 | + .forEach(obj -> { |
| 88 | + // Do something with each object |
| 89 | + System.out.println("Processing: " + obj); |
| 90 | + }); |
| 91 | +``` |
| 92 | + |
| 93 | +### With Options |
| 94 | + |
| 95 | +```java |
| 96 | +// Use options to specify consistency preference |
| 97 | +var options = new ClientListObjectsOptions() |
| 98 | + .consistency(ConsistencyPreference.HIGHER_CONSISTENCY) |
| 99 | + .authorizationModelId("01GXSXXXXXXXXXXXXXXXX"); |
| 100 | + |
| 101 | +var objectStream = fgaClient.streamedListObjects(request, options).get(); |
| 102 | +``` |
| 103 | + |
| 104 | +## Comparison with Regular ListObjects |
| 105 | + |
| 106 | +| Feature | ListObjects | Streamed ListObjects | |
| 107 | +|---------|-------------|---------------------| |
| 108 | +| Result Collection | Waits for all results | Streams results as computed | |
| 109 | +| Result Limit | Limited by server pagination | Limited only by execution timeout | |
| 110 | +| Processing | Must wait for complete response | Can process immediately | |
| 111 | +| Use Case | Small to medium result sets | Large result sets, immediate processing | |
0 commit comments