Skip to content

Commit 947eddf

Browse files
committed
feat: refactor add typed resp in example
1 parent 32868d9 commit 947eddf

3 files changed

Lines changed: 46 additions & 21 deletions

File tree

examples/raw-api/src/main/java/dev/openfga/sdk/example/RawApiExample.java

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import dev.openfga.sdk.api.client.OpenFgaClient;
44
import dev.openfga.sdk.api.client.RawRequestBuilder;
55
import dev.openfga.sdk.api.configuration.ClientConfiguration;
6-
import dev.openfga.sdk.api.model.CreateStoreRequest;
6+
import dev.openfga.sdk.api.model.CreateStoreResponse;
77
import dev.openfga.sdk.api.model.ListStoresResponse;
88
import dev.openfga.sdk.api.model.Store;
99
import java.util.List;
1010
import java.util.Map;
11+
import java.util.UUID;
1112

1213
/**
1314
* Example demonstrating Raw API usage.
@@ -16,6 +17,11 @@
1617
* that are not yet wrapped by the SDK's typed methods.
1718
*
1819
* 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
1925
*/
2026
public class RawApiExample {
2127

@@ -94,26 +100,14 @@ private static String listStoresExample(OpenFgaClient fgaClient) {
94100
* Helper method to create a store for examples.
95101
*/
96102
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);
98104
RawRequestBuilder request = RawRequestBuilder.builder("POST", "/stores")
99105
.body(Map.of("name", storeName));
100106

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();
103109
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();
117111
}
118112

119113
/**
@@ -169,10 +163,10 @@ private static void listStoresWithPaginationExample(OpenFgaClient fgaClient) {
169163
*/
170164
private static void createStoreWithHeadersExample(OpenFgaClient fgaClient) {
171165
try {
172-
String storeName = "raw-api-custom-headers-" + System.currentTimeMillis();
166+
String storeName = "raw-api-custom-headers-" + UUID.randomUUID().toString().substring(0, 8);
173167
RawRequestBuilder request = RawRequestBuilder.builder("POST", "/stores")
174168
.header("X-Example-Header", "custom-value")
175-
.header("X-Request-ID", "req-" + System.currentTimeMillis())
169+
.header("X-Request-ID", "req-" + UUID.randomUUID())
176170
.body(Map.of("name", storeName));
177171

178172
var response = fgaClient.raw().send(request).get();

src/main/java/dev/openfga/sdk/api/client/RawRequestBuilder.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.HashMap;
44
import java.util.Map;
5+
import java.util.Set;
56

67
/**
78
* Fluent builder for constructing HTTP requests to OpenFGA API endpoints.
@@ -17,6 +18,9 @@
1718
* }</pre>
1819
*/
1920
public class RawRequestBuilder {
21+
private static final Set<String> VALID_HTTP_METHODS =
22+
Set.of("GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS");
23+
2024
private final String method;
2125
private final String path;
2226
private final Map<String, String> pathParams;
@@ -36,9 +40,10 @@ private RawRequestBuilder(String method, String path) {
3640
/**
3741
* Creates a new RawRequestBuilder instance.
3842
*
39-
* @param method HTTP method (GET, POST, PUT, DELETE, etc.)
43+
* @param method HTTP method (GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS)
4044
* @param path API path with optional placeholders like {store_id}
4145
* @return New RawRequestBuilder instance
46+
* @throws IllegalArgumentException if method or path is invalid
4247
*/
4348
public static RawRequestBuilder builder(String method, String path) {
4449
if (method == null || method.trim().isEmpty()) {
@@ -47,7 +52,14 @@ public static RawRequestBuilder builder(String method, String path) {
4752
if (path == null || path.trim().isEmpty()) {
4853
throw new IllegalArgumentException("Path cannot be null or empty");
4954
}
50-
return new RawRequestBuilder(method.toUpperCase(), path);
55+
56+
String upperMethod = method.toUpperCase();
57+
if (!VALID_HTTP_METHODS.contains(upperMethod)) {
58+
throw new IllegalArgumentException(
59+
"Invalid HTTP method: " + method + ". Valid methods: " + VALID_HTTP_METHODS);
60+
}
61+
62+
return new RawRequestBuilder(upperMethod, path);
5163
}
5264

5365
/**

src/test/java/dev/openfga/sdk/api/client/RawApiTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,25 @@ public void rawRequestBuilder_throwsExceptionForEmptyPath() {
134134
assertThrows(IllegalArgumentException.class, () -> RawRequestBuilder.builder("GET", ""));
135135
}
136136

137+
@Test
138+
public void rawRequestBuilder_throwsExceptionForInvalidHttpMethod() {
139+
IllegalArgumentException exception = assertThrows(
140+
IllegalArgumentException.class, () -> RawRequestBuilder.builder("INVALID", "/test"));
141+
assertTrue(exception.getMessage().contains("Invalid HTTP method"));
142+
}
143+
144+
@Test
145+
public void rawRequestBuilder_acceptsValidHttpMethods() {
146+
assertDoesNotThrow(() -> RawRequestBuilder.builder("GET", "/test"));
147+
assertDoesNotThrow(() -> RawRequestBuilder.builder("POST", "/test"));
148+
assertDoesNotThrow(() -> RawRequestBuilder.builder("PUT", "/test"));
149+
assertDoesNotThrow(() -> RawRequestBuilder.builder("PATCH", "/test"));
150+
assertDoesNotThrow(() -> RawRequestBuilder.builder("DELETE", "/test"));
151+
assertDoesNotThrow(() -> RawRequestBuilder.builder("HEAD", "/test"));
152+
assertDoesNotThrow(() -> RawRequestBuilder.builder("OPTIONS", "/test"));
153+
assertDoesNotThrow(() -> RawRequestBuilder.builder("get", "/test"));
154+
}
155+
137156
@Test
138157
public void rawApi_canSendGetRequestWithTypedResponse() throws Exception {
139158
// Setup mock server

0 commit comments

Comments
 (0)