diff --git a/src/main/java/dev/openfga/sdk/api/client/ClientBatchCheckResponse.java b/src/main/java/dev/openfga/sdk/api/client/ClientBatchCheckResponse.java index 8b0922f..2f14a78 100644 --- a/src/main/java/dev/openfga/sdk/api/client/ClientBatchCheckResponse.java +++ b/src/main/java/dev/openfga/sdk/api/client/ClientBatchCheckResponse.java @@ -14,7 +14,6 @@ import dev.openfga.sdk.api.model.CheckResponse; import dev.openfga.sdk.errors.FgaError; - import java.util.List; import java.util.Map; import java.util.function.BiFunction; diff --git a/src/main/java/dev/openfga/sdk/api/client/ClientListRelationsResponse.java b/src/main/java/dev/openfga/sdk/api/client/ClientListRelationsResponse.java new file mode 100644 index 0000000..6a85969 --- /dev/null +++ b/src/main/java/dev/openfga/sdk/api/client/ClientListRelationsResponse.java @@ -0,0 +1,23 @@ +package dev.openfga.sdk.api.client; + +import java.util.List; +import java.util.stream.Collectors; + +public class ClientListRelationsResponse { + private final List relations; + + public ClientListRelationsResponse(List relations) { + this.relations = relations; + } + + public List getRelations() { + return relations; + } + + public static ClientListRelationsResponse fromBatchCheckResponses(List responses) { + return new ClientListRelationsResponse(responses.stream() + .filter(ClientBatchCheckResponse::getAllowed) + .map(batchCheckResponse -> batchCheckResponse.getRequest().getRelation()) + .collect(Collectors.toList())); + } +} 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 2bcbcfb..b1504df 100644 --- a/src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java +++ b/src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java @@ -22,6 +22,7 @@ import java.util.List; import java.util.concurrent.*; import java.util.function.Consumer; +import java.util.stream.Collectors; public class OpenFgaClient { private final ApiClient apiClient; @@ -491,9 +492,24 @@ public CompletableFuture listObjects( } /* - * ListRelations - List all the relations a user has with an object (evaluates) + * ListRelations - List allowed relations a user has with an object (evaluates) */ - // TODO + public CompletableFuture listRelations( + ClientListRelationsRequest request, ClientBatchCheckOptions options) throws FgaInvalidParameterException { + if (request.getRelations().isEmpty()) { + throw new FgaInvalidParameterException( + "At least 1 relation to check has to be provided when calling ListRelations"); + } + + var batchCheckRequests = request.getRelations().stream() + .map(relation -> new ClientCheckRequest() + .user(request.getUser()) + .relation(relation) + ._object(request.getObject())) + .collect(Collectors.toList()); + + return batchCheck(batchCheckRequests, options).thenApply(ClientListRelationsResponse::fromBatchCheckResponses); + } /* ************ * Assertions * diff --git a/src/main/java/dev/openfga/sdk/errors/FgaInvalidParameterException.java b/src/main/java/dev/openfga/sdk/errors/FgaInvalidParameterException.java index 49ad99d..1819330 100644 --- a/src/main/java/dev/openfga/sdk/errors/FgaInvalidParameterException.java +++ b/src/main/java/dev/openfga/sdk/errors/FgaInvalidParameterException.java @@ -1,6 +1,10 @@ package dev.openfga.sdk.errors; public class FgaInvalidParameterException extends Exception { + public FgaInvalidParameterException(String message) { + super(message); + } + public FgaInvalidParameterException(String paramName, String functionName) { super(message(paramName, functionName)); }