Skip to content

Commit

Permalink
feat(client): implement listRelations
Browse files Browse the repository at this point in the history
  • Loading branch information
booniepepper committed Nov 14, 2023
1 parent eda3a36 commit 4b404f2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> relations;

public ClientListRelationsResponse(List<String> relations) {
this.relations = relations;
}

public List<String> getRelations() {
return relations;
}

public static ClientListRelationsResponse fromBatchCheckResponses(List<ClientBatchCheckResponse> responses) {
return new ClientListRelationsResponse(responses.stream()
.filter(ClientBatchCheckResponse::getAllowed)
.map(batchCheckResponse -> batchCheckResponse.getRequest().getRelation())
.collect(Collectors.toList()));
}
}
20 changes: 18 additions & 2 deletions src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -491,9 +492,24 @@ public CompletableFuture<ClientListObjectsResponse> 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<ClientListRelationsResponse> 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 *
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
Expand Down

0 comments on commit 4b404f2

Please sign in to comment.