Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): implement batchCheck, listRelations, and non-transaction write #32

Merged
merged 15 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 27 additions & 1 deletion src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
import dev.openfga.sdk.api.configuration.*;
import dev.openfga.sdk.api.model.*;
import dev.openfga.sdk.errors.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executors;
import java.util.function.Consumer;

public class OpenFgaClient {
private final ApiClient apiClient;
Expand Down Expand Up @@ -383,7 +387,29 @@ public CompletableFuture<ClientCheckResponse> check(ClientCheckRequest request,
*
* @throws FgaInvalidParameterException When the Store ID is null, empty, or whitespace
*/
// TODO
public CompletableFuture<List<ClientCheckResponse>> batchCheck(
List<ClientCheckRequest> requests, ClientBatchCheckOptions options) {
int maxParallelRequests = options.getMaxParallelRequests() != null
? options.getMaxParallelRequests()
: DEFAULT_MAX_METHOD_PARALLEL_REQS;
var executor = Executors.newWorkStealingPool(maxParallelRequests);

var responses = new ConcurrentLinkedQueue<ClientCheckResponse>();
booniepepper marked this conversation as resolved.
Show resolved Hide resolved

final var clientCheckOptions = options.asClientCheckOptions();

Consumer<ClientCheckRequest> singleClientCheckRequest =
request -> call(() -> this.check(request, clientCheckOptions)).thenApply(responses::add);

requests.forEach(request -> executor.execute(() -> singleClientCheckRequest.accept(request)));

try {
executor.wait();
return CompletableFuture.completedFuture(new ArrayList<>(responses));
} catch (InterruptedException e) {
return CompletableFuture.failedFuture(e);
}
}

/**
* Expand - Expands the relationships in userset tree format (evaluates)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* OpenFGA
* A high performance and flexible authorization/permission engine built for developers and inspired by Google Zanzibar.
*
* The version of the OpenAPI document: 0.1
* Contact: [email protected]
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

package dev.openfga.sdk.api.configuration;

public class ClientBatchCheckOptions {
private Integer maxParallelRequests;
private String authorizationModelId;

public ClientBatchCheckOptions maxParallelRequests(Integer maxParallelRequests) {
this.maxParallelRequests = maxParallelRequests;
return this;
}

public Integer getMaxParallelRequests() {
return maxParallelRequests;
}

public ClientBatchCheckOptions authorizationModelId(String authorizationModelId) {
this.authorizationModelId = authorizationModelId;
return this;
}

public String getAuthorizationModelId() {
return authorizationModelId;
}

public ClientCheckOptions asClientCheckOptions() {
return new ClientCheckOptions().authorizationModelId(authorizationModelId);
}
}