From 31870f6de06c3e8c03449117abcf5beadc79d9b8 Mon Sep 17 00:00:00 2001 From: "Justin \"J.R.\" Hill" Date: Fri, 3 Nov 2023 17:48:18 -0700 Subject: [PATCH] feat(client): implement batchCheck --- .../openfga/sdk/api/client/OpenFgaClient.java | 28 ++++++++++++- .../ClientBatchCheckOptions.java | 40 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/main/java/dev/openfga/sdk/api/configuration/ClientBatchCheckOptions.java diff --git a/src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java b/src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java index 9941726..3635e88 100644 --- a/src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java +++ b/src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java @@ -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; @@ -383,7 +387,29 @@ public CompletableFuture check(ClientCheckRequest request, * * @throws FgaInvalidParameterException When the Store ID is null, empty, or whitespace */ - // TODO + public CompletableFuture> batchCheck( + List requests, ClientBatchCheckOptions options) { + int maxParallelRequests = options.getMaxParallelRequests() != null + ? options.getMaxParallelRequests() + : DEFAULT_MAX_METHOD_PARALLEL_REQS; + var executor = Executors.newWorkStealingPool(maxParallelRequests); + + var responses = new ConcurrentLinkedQueue(); + + final var clientCheckOptions = options.asClientCheckOptions(); + + Consumer 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) diff --git a/src/main/java/dev/openfga/sdk/api/configuration/ClientBatchCheckOptions.java b/src/main/java/dev/openfga/sdk/api/configuration/ClientBatchCheckOptions.java new file mode 100644 index 0000000..abc4a80 --- /dev/null +++ b/src/main/java/dev/openfga/sdk/api/configuration/ClientBatchCheckOptions.java @@ -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: community@openfga.dev + * + * 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); + } +}