|
3 | 3 | import dev.openfga.sdk.api.client.OpenFgaClient; |
4 | 4 | import dev.openfga.sdk.api.client.RawRequestBuilder; |
5 | 5 | import dev.openfga.sdk.api.configuration.ClientConfiguration; |
6 | | -import dev.openfga.sdk.api.model.CreateStoreRequest; |
| 6 | +import dev.openfga.sdk.api.model.CreateStoreResponse; |
7 | 7 | import dev.openfga.sdk.api.model.ListStoresResponse; |
8 | 8 | import dev.openfga.sdk.api.model.Store; |
9 | 9 | import java.util.List; |
10 | 10 | import java.util.Map; |
| 11 | +import java.util.UUID; |
11 | 12 |
|
12 | 13 | /** |
13 | 14 | * Example demonstrating Raw API usage. |
|
16 | 17 | * that are not yet wrapped by the SDK's typed methods. |
17 | 18 | * |
18 | 19 | * The example uses real OpenFGA endpoints to demonstrate actual functionality. |
| 20 | + * |
| 21 | + * Note: Examples use .get() to block for simplicity. In production, use async patterns: |
| 22 | + * - thenApply/thenAccept for chaining |
| 23 | + * - thenCompose for sequential async operations |
| 24 | + * - CompletableFuture.allOf for parallel operations |
19 | 25 | */ |
20 | 26 | public class RawApiExample { |
21 | 27 |
|
@@ -94,26 +100,14 @@ private static String listStoresExample(OpenFgaClient fgaClient) { |
94 | 100 | * Helper method to create a store for examples. |
95 | 101 | */ |
96 | 102 | private static String createStoreForExamples(OpenFgaClient fgaClient) throws Exception { |
97 | | - String storeName = "raw-api-example-" + System.currentTimeMillis(); |
| 103 | + String storeName = "raw-api-example-" + UUID.randomUUID().toString().substring(0, 8); |
98 | 104 | RawRequestBuilder request = RawRequestBuilder.builder("POST", "/stores") |
99 | 105 | .body(Map.of("name", storeName)); |
100 | 106 |
|
101 | | - var response = fgaClient.raw().send(request).get(); |
102 | | - String rawJson = response.getData(); |
| 107 | + // Use typed response instead of manual JSON parsing |
| 108 | + var response = fgaClient.raw().send(request, CreateStoreResponse.class).get(); |
103 | 109 | System.out.println(" Created store: " + storeName); |
104 | | - |
105 | | - // Extract store ID from JSON (simple parsing) |
106 | | - String idPrefix = "\"id\":\""; |
107 | | - int idStart = rawJson.indexOf(idPrefix); |
108 | | - if (idStart == -1) { |
109 | | - throw new RuntimeException("Could not find store ID in response: " + rawJson); |
110 | | - } |
111 | | - idStart += idPrefix.length(); |
112 | | - int idEnd = rawJson.indexOf("\"", idStart); |
113 | | - if (idEnd == -1) { |
114 | | - throw new RuntimeException("Could not parse store ID from response: " + rawJson); |
115 | | - } |
116 | | - return rawJson.substring(idStart, idEnd); |
| 110 | + return response.getData().getId(); |
117 | 111 | } |
118 | 112 |
|
119 | 113 | /** |
@@ -169,10 +163,10 @@ private static void listStoresWithPaginationExample(OpenFgaClient fgaClient) { |
169 | 163 | */ |
170 | 164 | private static void createStoreWithHeadersExample(OpenFgaClient fgaClient) { |
171 | 165 | try { |
172 | | - String storeName = "raw-api-custom-headers-" + System.currentTimeMillis(); |
| 166 | + String storeName = "raw-api-custom-headers-" + UUID.randomUUID().toString().substring(0, 8); |
173 | 167 | RawRequestBuilder request = RawRequestBuilder.builder("POST", "/stores") |
174 | 168 | .header("X-Example-Header", "custom-value") |
175 | | - .header("X-Request-ID", "req-" + System.currentTimeMillis()) |
| 169 | + .header("X-Request-ID", "req-" + UUID.randomUUID()) |
176 | 170 | .body(Map.of("name", storeName)); |
177 | 171 |
|
178 | 172 | var response = fgaClient.raw().send(request).get(); |
|
0 commit comments