Skip to content

Commit b38e829

Browse files
committed
feat: calling other endpoints section
1 parent f8360be commit b38e829

1 file changed

Lines changed: 70 additions & 42 deletions

File tree

README.md

Lines changed: 70 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ This is an autogenerated Java SDK for OpenFGA. It provides a wrapper around the
4646
- [Assertions](#assertions)
4747
- [Read Assertions](#read-assertions)
4848
- [Write Assertions](#write-assertions)
49-
- [Raw API](#raw-api)
49+
- [Calling Other Endpoints](#calling-other-endpoints)
5050
- [Retries](#retries)
5151
- [API Endpoints](#api-endpoints)
5252
- [Models](#models)
@@ -1168,57 +1168,85 @@ try {
11681168
}
11691169
```
11701170

1171-
### Raw API
1171+
### Calling Other Endpoints
11721172

1173-
The Raw API allows execution of HTTP requests to OpenFGA endpoints using the SDK's configured HTTP client. This is useful for accessing API endpoints that do not yet have typed SDK method implementations.
1173+
In certain cases you may want to call other APIs not yet wrapped by the SDK. You can do so by using the APIExecutor available from the `fgaClient`. The APIExecutor allows you to make raw HTTP calls to any OpenFGA endpoint by specifying the operation name, HTTP method, path, parameters, body, and headers, while still honoring the client configuration (authentication, telemetry, retries, and error handling).
11741174

1175-
#### Usage
1175+
This is useful when:
1176+
- you want to call a new endpoint that is not yet supported by the SDK
1177+
- you are using an earlier version of the SDK that doesn't yet support a particular endpoint
1178+
- you have a custom endpoint deployed that extends the OpenFGA API
11761179

1177-
```java
1178-
import dev.openfga.sdk.api.client.OpenFgaClient;
1179-
import dev.openfga.sdk.api.client.RawRequestBuilder;
1180-
import dev.openfga.sdk.api.configuration.ClientConfiguration;
1181-
import java.util.Map;
1182-
1183-
// Response type
1184-
class CustomResponse {
1185-
public boolean success;
1186-
public int count;
1187-
}
1180+
In all cases, you initialize the SDK the same way as usual, and then get the APIExecutor from the `fgaClient` instance.
11881181

1189-
// Client configuration
1182+
```java
1183+
// Initialize the client, same as above
11901184
ClientConfiguration config = new ClientConfiguration()
11911185
.apiUrl("http://localhost:8080")
11921186
.storeId("01YCP46JKYM8FJCQ37NMBYHE5X");
1193-
OpenFgaClient client = new OpenFgaClient(config);
1194-
1195-
// Request execution
1196-
RawRequestBuilder request = RawRequestBuilder.builder("POST", "/stores/{store_id}/endpoint")
1197-
.pathParam("store_id", client.getStoreId())
1198-
.queryParam("param", "value")
1199-
.body(Map.of("key", "value"));
1200-
1201-
// Typed response
1202-
client.raw().send(request, CustomResponse.class)
1203-
.thenAccept(response -> {
1204-
System.out.println("Status: " + response.getStatusCode());
1205-
System.out.println("Data: " + response.getData());
1206-
});
1207-
1208-
// Raw JSON response
1209-
client.raw().send(request)
1210-
.thenAccept(response -> {
1211-
System.out.println("JSON: " + response.getRawResponse());
1212-
});
1187+
OpenFgaClient fgaClient = new OpenFgaClient(config);
1188+
1189+
// Get the generic API executor
1190+
APIExecutor executor = fgaClient.getAPIExecutor();
1191+
1192+
// Custom new endpoint that doesn't exist in the SDK yet
1193+
Map<String, Object> requestBody = Map.of(
1194+
"user", "user:bob",
1195+
"action", "custom_action",
1196+
"resource", "resource:123"
1197+
);
1198+
1199+
// Build the request
1200+
APIExecutorRequest request = new APIExecutorRequestBuilder("CustomEndpoint", "POST", "/stores/{store_id}/custom-endpoint")
1201+
.withPathParameter("store_id", storeId)
1202+
.withQueryParameter("page_size", "20")
1203+
.withQueryParameter("continuation_token", "eyJwayI6...")
1204+
.withBody(requestBody)
1205+
.withHeader("X-Experimental-Feature", "enabled")
1206+
.build();
1207+
```
1208+
1209+
#### Example: Calling a new "Custom Endpoint" endpoint and handling raw response
1210+
1211+
```java
1212+
// Get raw response without automatic decoding
1213+
APIExecutorResponse rawResponse = executor.execute(request).get();
1214+
1215+
// Manually decode the response
1216+
ObjectMapper mapper = new ObjectMapper();
1217+
Map<String, Object> result = mapper.readValue(rawResponse.getBody(), new TypeReference<Map<String, Object>>() {});
1218+
1219+
System.out.println("Response: " + result);
1220+
1221+
// You can access fields like headers, status code, etc. from rawResponse:
1222+
System.out.println("Status Code: " + rawResponse.getStatusCode());
1223+
System.out.println("Headers: " + rawResponse.getHeaders());
12131224
```
12141225

1215-
#### Features
1226+
#### Example: Calling a new "Custom Endpoint" endpoint and decoding response into a struct
12161227

1217-
Requests automatically include:
1218-
- Authentication credentials from configuration
1219-
- Retry logic for 5xx errors with exponential backoff
1220-
- Error handling and exception mapping
1221-
- Configured timeouts and headers
1228+
```java
1229+
// Define a class to hold the response
1230+
class CustomEndpointResponse {
1231+
private boolean allowed;
1232+
private String reason;
1233+
1234+
public boolean isAllowed() { return allowed; }
1235+
public void setAllowed(boolean allowed) { this.allowed = allowed; }
1236+
public String getReason() { return reason; }
1237+
public void setReason(String reason) { this.reason = reason; }
1238+
}
1239+
1240+
// Get raw response decoded into CustomEndpointResponse class
1241+
APIExecutorResponse rawResponse = executor.executeWithDecode(request, CustomEndpointResponse.class).get();
1242+
1243+
CustomEndpointResponse customEndpointResponse = rawResponse.getData();
1244+
System.out.println("Response: " + customEndpointResponse);
1245+
1246+
// You can access fields like headers, status code, etc. from rawResponse:
1247+
System.out.println("Status Code: " + rawResponse.getStatusCode());
1248+
System.out.println("Headers: " + rawResponse.getHeaders());
1249+
```
12221250

12231251
#### Documentation
12241252

0 commit comments

Comments
 (0)