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
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ This is an autogenerated Java SDK for OpenFGA. It provides a wrapper around the
- [Installation](#installation)
- [Getting Started](#getting-started)
- [Initializing the API Client](#initializing-the-api-client)
- [Custom Headers](#custom-headers)
- [Get your Store ID](#get-your-store-id)
- [Calling the API](#calling-the-api)
- [Stores](#stores)
Expand Down Expand Up @@ -236,6 +237,68 @@ public class Example {
}
```

### Custom Headers

#### Default Headers

You can set default headers to be sent with every request by using the `defaultHeaders` property of the `ClientConfiguration` class.

```java
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.openfga.sdk.api.client.OpenFgaClient;
import dev.openfga.sdk.api.configuration.ClientConfiguration;

import java.net.http.HttpClient;
import java.util.Map;

public class Example {
public static void main(String[] args) throws Exception {
var config = new ClientConfiguration()
.apiUrl(System.getenv("FGA_API_URL"))
.storeId(System.getenv("FGA_STORE_ID"))
.authorizationModelId(System.getenv("FGA_MODEL_ID"))
.defaultHeaders(Map.of(
"X-Custom-Header", "default-value",
"X-Request-Source", "my-app"
));

var fgaClient = new OpenFgaClient(config);
}
}
```

#### Per-request Headers

You can set custom headers to be sent with a specific request by using the `additionalHeaders` property of the options classes (e.g. `ClientReadOptions`, `ClientWriteOptions`, etc.).

```java
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.openfga.sdk.api.client.OpenFgaClient;
import dev.openfga.sdk.api.configuration.ClientConfiguration;
import java.net.http.HttpClient;

public class Example {
public static void main(String[] args) throws Exception {
var config = new ClientConfiguration()
.apiUrl(System.getenv("FGA_API_URL"))
.storeId(System.getenv("FGA_STORE_ID"))
.authorizationModelId(System.getenv("FGA_MODEL_ID"))
.defaultHeaders(Map.of(
"X-Custom-Header", "default-value",
"X-Request-Source", "my-app"
));

var fgaClient = new OpenFgaClient(config);
var options = new ClientReadOptions()
.additionalHeaders(Map.of(
"X-Request-Id", "123e4567-e89b-12d3-a456-426614174000",
"X-Custom-Header", "overridden-value" // this will override the default value for this request only
)
);
var response = fgaClient.read(request, options).get();
}
}
```

### Get your Store ID

Expand Down
59 changes: 34 additions & 25 deletions src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,13 @@ public CompletableFuture<ClientReadAuthorizationModelResponse> readAuthorization
ClientReadAuthorizationModelOptions options) throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
String authorizationModelId = options.getAuthorizationModelIdChecked();
// Set authorizationModelId from options if available; otherwise, require a valid configuration value
String authorizationModelId;
if (options != null && !isNullOrWhitespace(options.getAuthorizationModelId())) {
authorizationModelId = options.getAuthorizationModelIdChecked();
} else {
authorizationModelId = configuration.getAuthorizationModelIdChecked();
}
var overrides = new ConfigurationOverride().addHeaders(options);
return call(() -> api.readAuthorizationModel(storeId, authorizationModelId, overrides))
.thenApply(ClientReadAuthorizationModelResponse::new);
Expand Down Expand Up @@ -552,12 +558,12 @@ private CompletableFuture<ClientWriteResponse> writeNonTransaction(
? writeOptions
: new ClientWriteOptions().transactionChunkSize(DEFAULT_MAX_METHOD_PARALLEL_REQS);

if (options.getAdditionalHeaders() == null) {
options.additionalHeaders(new HashMap<>());
}
options.getAdditionalHeaders().putIfAbsent(CLIENT_METHOD_HEADER, "Write");
options.getAdditionalHeaders()
.putIfAbsent(CLIENT_BULK_REQUEST_ID_HEADER, randomUUID().toString());
HashMap<String, String> headers = options.getAdditionalHeaders() != null
? new HashMap<>(options.getAdditionalHeaders())
: new HashMap<>();
headers.putIfAbsent(CLIENT_METHOD_HEADER, "Write");
headers.putIfAbsent(CLIENT_BULK_REQUEST_ID_HEADER, randomUUID().toString());
options.additionalHeaders(headers);

int chunkSize = options.getTransactionChunkSize();

Expand Down Expand Up @@ -832,12 +838,13 @@ public CompletableFuture<List<ClientBatchCheckClientResponse>> clientBatchCheck(
var options = batchCheckOptions != null
? batchCheckOptions
: new ClientBatchCheckClientOptions().maxParallelRequests(DEFAULT_MAX_METHOD_PARALLEL_REQS);
if (options.getAdditionalHeaders() == null) {
options.additionalHeaders(new HashMap<>());
}
options.getAdditionalHeaders().putIfAbsent(CLIENT_METHOD_HEADER, "ClientBatchCheck");
options.getAdditionalHeaders()
.putIfAbsent(CLIENT_BULK_REQUEST_ID_HEADER, randomUUID().toString());

HashMap<String, String> headers = options.getAdditionalHeaders() != null
? new HashMap<>(options.getAdditionalHeaders())
: new HashMap<>();
headers.putIfAbsent(CLIENT_METHOD_HEADER, "ClientBatchCheck");
headers.putIfAbsent(CLIENT_BULK_REQUEST_ID_HEADER, randomUUID().toString());
options.additionalHeaders(headers);

int maxParallelRequests = options.getMaxParallelRequests() != null
? options.getMaxParallelRequests()
Expand Down Expand Up @@ -892,12 +899,13 @@ public CompletableFuture<ClientBatchCheckResponse> batchCheck(
: new ClientBatchCheckOptions()
.maxParallelRequests(DEFAULT_MAX_METHOD_PARALLEL_REQS)
.maxBatchSize(DEFAULT_MAX_BATCH_SIZE);
if (options.getAdditionalHeaders() == null) {
options.additionalHeaders(new HashMap<>());
}
options.getAdditionalHeaders().putIfAbsent(CLIENT_METHOD_HEADER, "BatchCheck");
options.getAdditionalHeaders()
.putIfAbsent(CLIENT_BULK_REQUEST_ID_HEADER, randomUUID().toString());

HashMap<String, String> headers = options.getAdditionalHeaders() != null
? new HashMap<>(options.getAdditionalHeaders())
: new HashMap<>();
headers.putIfAbsent(CLIENT_METHOD_HEADER, "BatchCheck");
headers.putIfAbsent(CLIENT_BULK_REQUEST_ID_HEADER, randomUUID().toString());
options.additionalHeaders(headers);

Map<String, ClientBatchCheckItem> correlationIdToCheck = new HashMap<>();

Expand Down Expand Up @@ -1124,12 +1132,13 @@ public CompletableFuture<ClientListRelationsResponse> listRelations(
var options = listRelationsOptions != null
? listRelationsOptions
: new ClientListRelationsOptions().maxParallelRequests(DEFAULT_MAX_METHOD_PARALLEL_REQS);
if (options.getAdditionalHeaders() == null) {
options.additionalHeaders(new HashMap<>());
}
options.getAdditionalHeaders().putIfAbsent(CLIENT_METHOD_HEADER, "ListRelations");
options.getAdditionalHeaders()
.putIfAbsent(CLIENT_BULK_REQUEST_ID_HEADER, randomUUID().toString());

HashMap<String, String> headers = options.getAdditionalHeaders() != null
? new HashMap<>(options.getAdditionalHeaders())
: new HashMap<>();
headers.putIfAbsent(CLIENT_METHOD_HEADER, "ListRelations");
headers.putIfAbsent(CLIENT_BULK_REQUEST_ID_HEADER, randomUUID().toString());
options.additionalHeaders(headers);

var batchCheckRequests = request.getRelations().stream()
.map(relation -> new ClientCheckRequest()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package dev.openfga.sdk.api.configuration;

import dev.openfga.sdk.api.model.ConsistencyPreference;
import java.util.HashMap;
import java.util.Map;

public class ClientBatchCheckClientOptions implements AdditionalHeadersSupplier {
Expand Down Expand Up @@ -60,7 +61,7 @@ public ConsistencyPreference getConsistency() {

public ClientCheckOptions asClientCheckOptions() {
return new ClientCheckOptions()
.additionalHeaders(additionalHeaders)
.additionalHeaders(additionalHeaders != null ? new HashMap<>(additionalHeaders) : null)
.authorizationModelId(authorizationModelId)
.consistency(consistency);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,10 @@ public ClientConfiguration telemetryConfiguration(TelemetryConfiguration telemet
super.telemetryConfiguration(telemetryConfiguration);
return this;
}

@Override
public ClientConfiguration defaultHeaders(java.util.Map<String, String> defaultHeaders) {
super.defaultHeaders(defaultHeaders);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package dev.openfga.sdk.api.configuration;

import dev.openfga.sdk.api.model.ConsistencyPreference;
import java.util.HashMap;
import java.util.Map;

public class ClientListRelationsOptions implements AdditionalHeadersSupplier {
Expand Down Expand Up @@ -61,6 +62,7 @@ public ConsistencyPreference getConsistency() {
public ClientBatchCheckClientOptions asClientBatchCheckClientOptions() {
return new ClientBatchCheckClientOptions()
.authorizationModelId(authorizationModelId)
.additionalHeaders(additionalHeaders != null ? new HashMap<>(additionalHeaders) : null)
.maxParallelRequests(maxParallelRequests)
.consistency(consistency);
}
Expand Down
Loading
Loading