Skip to content
Open
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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,10 @@ var request = new ClientCheckRequest()
var options = new ClientCheckOptions()
.additionalHeaders(Map.of("Some-Http-Header", "Some value"))
// You can rely on the model id set in the configuration or override it for this specific request
.authorizationModelId("01GXSA8YR785C4FYS3C0RTG7B1");
.authorizationModelId("01GXSA8YR785C4FYS3C0RTG7B1")
// You can rely on the store id set in the configuration or override it for this specific request,
// e.g. to use one client for several stores. Every method that works on a store has this option.
.storeId("01YCP46JKYM8FJCQ37NMBYHE5X");

var response = fgaClient.check(request, options).get();
// response.getAllowed() = true
Expand Down
52 changes: 31 additions & 21 deletions src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public CompletableFuture<ClientGetStoreResponse> getStore() throws FgaInvalidPar
public CompletableFuture<ClientGetStoreResponse> getStore(ClientGetStoreOptions options)
throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
String storeId = getStoreIdChecked(options != null ? options.getStoreId() : null);
var overrides = new ConfigurationOverride().addHeaders(options);
return call(() -> api.getStore(storeId, overrides)).thenApply(ClientGetStoreResponse::new);
}
Expand All @@ -197,7 +197,7 @@ public CompletableFuture<ClientDeleteStoreResponse> deleteStore() throws FgaInva
public CompletableFuture<ClientDeleteStoreResponse> deleteStore(ClientDeleteStoreOptions options)
throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
String storeId = getStoreIdChecked(options != null ? options.getStoreId() : null);
var overrides = new ConfigurationOverride().addHeaders(options);
return call(() -> api.deleteStore(storeId, overrides)).thenApply(ClientDeleteStoreResponse::new);
}
Expand All @@ -224,7 +224,7 @@ public CompletableFuture<ClientReadAuthorizationModelsResponse> readAuthorizatio
public CompletableFuture<ClientReadAuthorizationModelsResponse> readAuthorizationModels(
ClientReadAuthorizationModelsOptions options) throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
String storeId = getStoreIdChecked(options != null ? options.getStoreId() : null);

Integer pageSize;
String continuationToken;
Expand Down Expand Up @@ -263,7 +263,7 @@ public CompletableFuture<ClientWriteAuthorizationModelResponse> writeAuthorizati
WriteAuthorizationModelRequest request, ClientWriteAuthorizationModelOptions options)
throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
String storeId = getStoreIdChecked(options != null ? options.getStoreId() : null);
var overrides = new ConfigurationOverride().addHeaders(options);
return call(() -> api.writeAuthorizationModel(storeId, request, overrides))
.thenApply(ClientWriteAuthorizationModelResponse::new);
Expand Down Expand Up @@ -291,7 +291,7 @@ public CompletableFuture<ClientReadAuthorizationModelResponse> readAuthorization
public CompletableFuture<ClientReadAuthorizationModelResponse> readAuthorizationModel(
ClientReadAuthorizationModelOptions options) throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
String storeId = getStoreIdChecked(options != null ? options.getStoreId() : null);
// Set authorizationModelId from options if available; otherwise, require a valid configuration value
String authorizationModelId;
if (options != null && !isNullOrWhitespace(options.getAuthorizationModelId())) {
Expand Down Expand Up @@ -322,7 +322,7 @@ public CompletableFuture<ClientReadAuthorizationModelResponse> readLatestAuthori
public CompletableFuture<ClientReadAuthorizationModelResponse> readLatestAuthorizationModel(
ClientReadLatestAuthorizationModelOptions options) throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
String storeId = getStoreIdChecked(options != null ? options.getStoreId() : null);
var overrides = new ConfigurationOverride().addHeaders(options);
return call(() -> api.readAuthorizationModels(storeId, 1, null, overrides))
.thenApply(ClientReadAuthorizationModelResponse::latestOf);
Expand Down Expand Up @@ -351,7 +351,7 @@ public CompletableFuture<ClientReadChangesResponse> readChanges(
ClientReadChangesRequest request, ClientReadChangesOptions readChangesOptions)
throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
String storeId = getStoreIdChecked(readChangesOptions != null ? readChangesOptions.getStoreId() : null);
var options = readChangesOptions != null ? readChangesOptions : new ClientReadChangesOptions();
var overrides = new ConfigurationOverride().addHeaders(options);
return call(() -> api.readChanges(
Expand Down Expand Up @@ -381,7 +381,7 @@ public CompletableFuture<ClientReadResponse> read(ClientReadRequest request) thr
public CompletableFuture<ClientReadResponse> read(ClientReadRequest request, ClientReadOptions options)
throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
String storeId = getStoreIdChecked(options != null ? options.getStoreId() : null);

ReadRequest body = new ReadRequest();

Expand Down Expand Up @@ -512,7 +512,7 @@ public CompletableFuture<ClientWriteResponse> write(ClientWriteRequest request)
public CompletableFuture<ClientWriteResponse> write(ClientWriteRequest request, ClientWriteOptions options)
throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
String storeId = getStoreIdChecked(options != null ? options.getStoreId() : null);

if (options != null && !options.isTransactionsEnabled()) {
return writeNonTransaction(storeId, request, options);
Expand Down Expand Up @@ -780,7 +780,7 @@ public CompletableFuture<ClientWriteResponse> writeTuples(List<ClientTupleKey> t
public CompletableFuture<ClientWriteResponse> writeTuples(
List<ClientTupleKey> tupleKeys, ClientWriteTuplesOptions options) throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
String storeId = getStoreIdChecked(options != null ? options.getStoreId() : null);

var body = new WriteRequest();

Expand Down Expand Up @@ -821,7 +821,7 @@ public CompletableFuture<ClientWriteResponse> deleteTuples(
List<ClientTupleKeyWithoutCondition> tupleKeys, ClientDeleteTuplesOptions options)
throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
String storeId = getStoreIdChecked(options != null ? options.getStoreId() : null);

var body = new WriteRequest();

Expand Down Expand Up @@ -870,7 +870,7 @@ public CompletableFuture<ClientCheckResponse> check(ClientCheckRequest request)
public CompletableFuture<ClientCheckResponse> check(ClientCheckRequest request, ClientCheckOptions options)
throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
String storeId = getStoreIdChecked(options != null ? options.getStoreId() : null);

CheckRequest body = request.asCheckRequest();
if (options != null) {
Expand Down Expand Up @@ -911,7 +911,7 @@ public CompletableFuture<List<ClientBatchCheckClientResponse>> clientBatchCheck(
List<ClientCheckRequest> requests, ClientBatchCheckClientOptions batchCheckOptions)
throws FgaInvalidParameterException {
configuration.assertValid();
configuration.assertValidStoreId();
getStoreIdChecked(batchCheckOptions != null ? batchCheckOptions.getStoreId() : null);

var options = batchCheckOptions != null
? batchCheckOptions
Expand Down Expand Up @@ -964,7 +964,7 @@ public CompletableFuture<ClientBatchCheckResponse> batchCheck(
ClientBatchCheckRequest requests, ClientBatchCheckOptions batchCheckOptions)
throws FgaInvalidParameterException, FgaValidationError {
configuration.assertValid();
configuration.assertValidStoreId();
String storeId = getStoreIdChecked(batchCheckOptions != null ? batchCheckOptions.getStoreId() : null);

var options = batchCheckOptions != null
? batchCheckOptions
Expand Down Expand Up @@ -1048,7 +1048,7 @@ public CompletableFuture<ClientBatchCheckResponse> batchCheck(
body.authorizationModelId(authorizationModelId);
}

return api.batchCheck(configuration.getStoreId(), body, override);
return api.batchCheck(storeId, body, override);
})
.<Void>handle((batchCheckResponseApiResponse, throwable) -> {
if (throwable != null) {
Expand Down Expand Up @@ -1103,7 +1103,7 @@ public CompletableFuture<ClientExpandResponse> expand(ClientExpandRequest reques
public CompletableFuture<ClientExpandResponse> expand(ClientExpandRequest request, ClientExpandOptions options)
throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
String storeId = getStoreIdChecked(options != null ? options.getStoreId() : null);

ExpandRequest body = new ExpandRequest();

Expand Down Expand Up @@ -1149,7 +1149,7 @@ public CompletableFuture<ClientListObjectsResponse> listObjects(ClientListObject
public CompletableFuture<ClientListObjectsResponse> listObjects(
ClientListObjectsRequest request, ClientListObjectsOptions options) throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
String storeId = getStoreIdChecked(options != null ? options.getStoreId() : null);

ListObjectsRequest body = new ListObjectsRequest();

Expand Down Expand Up @@ -1301,7 +1301,7 @@ public CompletableFuture<Void> streamedListObjects(
throw new FgaInvalidParameterException("consumer", "streamedListObjects");
}
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
String storeId = getStoreIdChecked(options != null ? options.getStoreId() : null);

ListObjectsRequest body = new ListObjectsRequest();

Expand Down Expand Up @@ -1404,7 +1404,7 @@ public CompletableFuture<ClientListUsersResponse> listUsers(ClientListUsersReque
public CompletableFuture<ClientListUsersResponse> listUsers(
ClientListUsersRequest request, ClientListUsersOptions options) throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
String storeId = getStoreIdChecked(options != null ? options.getStoreId() : null);

ListUsersRequest body = new ListUsersRequest();

Expand Down Expand Up @@ -1460,7 +1460,7 @@ public CompletableFuture<ClientReadAssertionsResponse> readAssertions() throws F
public CompletableFuture<ClientReadAssertionsResponse> readAssertions(ClientReadAssertionsOptions options)
throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
String storeId = getStoreIdChecked(options != null ? options.getStoreId() : null);

String authorizationModelId;
if (options != null && options.hasValidAuthorizationModelId()) {
Expand Down Expand Up @@ -1494,7 +1494,7 @@ public CompletableFuture<ClientWriteAssertionsResponse> writeAssertions(
List<ClientAssertion> assertions, ClientWriteAssertionsOptions options)
throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
String storeId = getStoreIdChecked(options != null ? options.getStoreId() : null);

String authorizationModelId;
if (options != null && options.hasValidAuthorizationModelId()) {
Expand All @@ -1511,6 +1511,16 @@ public CompletableFuture<ClientWriteAssertionsResponse> writeAssertions(
.thenApply(ClientWriteAssertionsResponse::new);
}

/**
* Returns the store ID to send a request to: the per-request override when it is set, otherwise the store ID
* from the client configuration.
*
* @throws FgaInvalidParameterException When neither is set
*/
private String getStoreIdChecked(String storeIdOverride) throws FgaInvalidParameterException {
return !isNullOrWhitespace(storeIdOverride) ? storeIdOverride : configuration.getStoreIdChecked();
}

/**
* A {@link FunctionalInterface} for calling a low-level API from {@link OpenFgaApi}. It wraps exceptions
* encountered with {@link CompletableFuture#failedFuture(Throwable)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ClientStreamedListObjectsOptions implements AdditionalHeadersSuppli
private String authorizationModelId;
private ConsistencyPreference consistency;
private Map<String, String> additionalHeaders;
private String storeId;

public ClientStreamedListObjectsOptions authorizationModelId(String authorizationModelId) {
this.authorizationModelId = authorizationModelId;
Expand Down Expand Up @@ -53,4 +54,16 @@ public ClientStreamedListObjectsOptions additionalHeaders(Map<String, String> ad
public Map<String, String> getAdditionalHeaders() {
return additionalHeaders;
}

/**
* Sets the store to send this request to, instead of the store in the client configuration.
*/
public ClientStreamedListObjectsOptions storeId(String storeId) {
this.storeId = storeId;
return this;
}

public String getStoreId() {
return storeId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

public class ClientBatchCheckClientOptions implements AdditionalHeadersSupplier {
private Map<String, String> additionalHeaders;
private String storeId;
private Integer maxParallelRequests;
private String authorizationModelId;
private ConsistencyPreference consistency;
Expand Down Expand Up @@ -51,6 +52,19 @@ public ClientCheckOptions asClientCheckOptions() {
return new ClientCheckOptions()
.additionalHeaders(additionalHeaders != null ? new HashMap<>(additionalHeaders) : null)
.authorizationModelId(authorizationModelId)
.storeId(storeId)
.consistency(consistency);
}

/**
* Sets the store to send this request to, instead of the store in the client configuration.
*/
public ClientBatchCheckClientOptions storeId(String storeId) {
this.storeId = storeId;
return this;
}

public String getStoreId() {
return storeId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

public class ClientBatchCheckOptions implements AdditionalHeadersSupplier {
private Map<String, String> additionalHeaders;
private String storeId;
private Integer maxParallelRequests;
private Integer maxBatchSize;
private String authorizationModelId;
Expand Down Expand Up @@ -55,4 +56,16 @@ public ClientBatchCheckOptions consistency(ConsistencyPreference consistency) {
public ConsistencyPreference getConsistency() {
return consistency;
}

/**
* Sets the store to send this request to, instead of the store in the client configuration.
*/
public ClientBatchCheckOptions storeId(String storeId) {
this.storeId = storeId;
return this;
}

public String getStoreId() {
return storeId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

public class ClientCheckOptions implements AdditionalHeadersSupplier {
private Map<String, String> additionalHeaders;
private String storeId;
private String authorizationModelId;
private ConsistencyPreference consistency;

Expand Down Expand Up @@ -35,4 +36,16 @@ public ClientCheckOptions consistency(ConsistencyPreference consistency) {
public ConsistencyPreference getConsistency() {
return consistency;
}

/**
* Sets the store to send this request to, instead of the store in the client configuration.
*/
public ClientCheckOptions storeId(String storeId) {
this.storeId = storeId;
return this;
}

public String getStoreId() {
return storeId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

public class ClientDeleteStoreOptions implements AdditionalHeadersSupplier {
private Map<String, String> additionalHeaders;
private String storeId;

public ClientDeleteStoreOptions additionalHeaders(Map<String, String> additionalHeaders) {
this.additionalHeaders = additionalHeaders;
Expand All @@ -14,4 +15,16 @@ public ClientDeleteStoreOptions additionalHeaders(Map<String, String> additional
public Map<String, String> getAdditionalHeaders() {
return this.additionalHeaders;
}

/**
* Sets the store to send this request to, instead of the store in the client configuration.
*/
public ClientDeleteStoreOptions storeId(String storeId) {
this.storeId = storeId;
return this;
}

public String getStoreId() {
return storeId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

public class ClientDeleteTuplesOptions implements AdditionalHeadersSupplier {
private Map<String, String> additionalHeaders;
private String storeId;
private WriteRequestDeletes.OnMissingEnum onMissing;

public ClientDeleteTuplesOptions additionalHeaders(Map<String, String> additionalHeaders) {
Expand All @@ -25,4 +26,16 @@ public ClientDeleteTuplesOptions onMissing(WriteRequestDeletes.OnMissingEnum onM
public WriteRequestDeletes.OnMissingEnum getOnMissing() {
return onMissing;
}

/**
* Sets the store to send this request to, instead of the store in the client configuration.
*/
public ClientDeleteTuplesOptions storeId(String storeId) {
this.storeId = storeId;
return this;
}

public String getStoreId() {
return storeId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

public class ClientExpandOptions implements AdditionalHeadersSupplier {
private Map<String, String> additionalHeaders;
private String storeId;
private String authorizationModelId;
private ConsistencyPreference consistency;

Expand Down Expand Up @@ -35,4 +36,16 @@ public ClientExpandOptions consistency(ConsistencyPreference consistency) {
public ConsistencyPreference getConsistency() {
return consistency;
}

/**
* Sets the store to send this request to, instead of the store in the client configuration.
*/
public ClientExpandOptions storeId(String storeId) {
this.storeId = storeId;
return this;
}

public String getStoreId() {
return storeId;
}
}
Loading