Skip to content

Commit

Permalink
feat(client): implement batchCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
booniepepper committed Nov 4, 2023
1 parent d790fc0 commit 31870f6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
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>();

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);
}
}

0 comments on commit 31870f6

Please sign in to comment.