Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,67 @@ 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
)
);
}
}
```

### Get your Store ID

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,11 @@ 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, use the default from configuration
String authorizationModelId = !isNullOrWhitespace(options.getAuthorizationModelId())
? options.getAuthorizationModelIdChecked()
: configuration.getAuthorizationModelId();

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
var overrides = new ConfigurationOverride().addHeaders(options);
return call(() -> api.readAuthorizationModel(storeId, authorizationModelId, overrides))
.thenApply(ClientReadAuthorizationModelResponse::new);
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 @@ -61,6 +61,7 @@ public ConsistencyPreference getConsistency() {
public ClientBatchCheckClientOptions asClientBatchCheckClientOptions() {
return new ClientBatchCheckClientOptions()
.authorizationModelId(authorizationModelId)
.additionalHeaders(additionalHeaders)
.maxParallelRequests(maxParallelRequests)
.consistency(consistency);
Comment thread
ewanharris marked this conversation as resolved.
}
Expand Down
Loading
Loading