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 8 commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* 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.client;

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;

public class ClientBatchCheckResponse extends CheckResponse {
private final ClientCheckRequest request;
private final Throwable throwable;
private final Integer statusCode;
private final Map<String, List<String>> headers;
private final String rawResponse;

public ClientBatchCheckResponse(
ClientCheckRequest request, ClientCheckResponse clientCheckResponse, Throwable throwable) {
this.request = request;
this.throwable = throwable;

if (clientCheckResponse != null) {
this.statusCode = clientCheckResponse.getStatusCode();
this.headers = clientCheckResponse.getHeaders();
this.rawResponse = clientCheckResponse.getRawResponse();
this.setAllowed(clientCheckResponse.getAllowed());
this.setResolution(clientCheckResponse.getResolution());
} else if (throwable instanceof FgaError) {
FgaError error = (FgaError) throwable;
this.statusCode = error.getStatusCode();
this.headers = error.getResponseHeaders().map();
this.rawResponse = error.getResponseData();
} else {
// Should be unreachable, but required for type completion
this.statusCode = null;
this.headers = null;
this.rawResponse = null;
}
}

public ClientCheckRequest getRequest() {
return request;
}

/**
* Returns the result of the check.
* <p>
* If the HTTP request was unsuccessful, this result will be null. If this is the case, you can examine the
* original request with {@link ClientBatchCheckResponse#getRequest()} and the exception with
* {@link ClientBatchCheckResponse#getThrowable()}.
*
* @return the check result. Is null if the HTTP request was unsuccessful.
*/
@Override
public Boolean getAllowed() {
return super.getAllowed();
}

/**
* Returns the caught exception if the HTTP request was unsuccessful.
* <p>
* If the HTTP request was unsuccessful, this result will be null. If this is the case, you can examine the
* original request with {@link ClientBatchCheckResponse#getRequest()} and the exception with
* {@link ClientBatchCheckResponse#getThrowable()}.
*
* @return the caught exception. Is null if the HTTP request was successful.
*/
public Throwable getThrowable() {
return throwable;
}

public int getStatusCode() {
return statusCode;
}

public Map<String, List<String>> getHeaders() {
return headers;
}

public String getRawResponse() {
return rawResponse;
}

public String getRelation() {
return request == null ? null : request.getRelation();
}

public static BiFunction<ClientCheckResponse, Throwable, ClientBatchCheckResponse> asyncHandler(
ClientCheckRequest request) {
return (response, throwable) -> new ClientBatchCheckResponse(request, response, throwable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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)
throws Throwable {
// If any response ultimately failed (with retries) we throw the first exception encountered.
var failedResponse = responses.stream()
.filter(response -> response.getThrowable() != null)
.findFirst();
if (failedResponse.isPresent()) {
throw failedResponse.get().getThrowable();
}

var relations = responses.stream()
.filter(ClientBatchCheckResponse::getAllowed)
.map(ClientBatchCheckResponse::getRelation)
.collect(Collectors.toList());
return new ClientListRelationsResponse(relations);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import dev.openfga.sdk.api.model.TupleKey;
import dev.openfga.sdk.api.model.TupleKeys;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class ClientTupleKey {
Expand Down Expand Up @@ -66,12 +67,12 @@ public TupleKey asTupleKey() {
return new TupleKey().user(user).relation(relation)._object(_object);
}

public static TupleKeys asTupleKeys(List<ClientTupleKey> clientTupleKeys) {
public static Optional<TupleKeys> asTupleKeys(List<ClientTupleKey> clientTupleKeys) {
if (clientTupleKeys == null || clientTupleKeys.size() == 0) {
return new TupleKeys();
return Optional.empty();
}

return new TupleKeys().tupleKeys(asListOfTupleKey(clientTupleKeys));
return Optional.of(new TupleKeys().tupleKeys(asListOfTupleKey(clientTupleKeys)));
}

public static ContextualTupleKeys asContextualTupleKeys(List<ClientTupleKey> clientTupleKeys) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public class ClientWriteRequest {
private List<ClientTupleKey> writes;
private List<ClientTupleKey> deletes;

public static ClientWriteRequest ofWrites(List<ClientTupleKey> writes) {
return new ClientWriteRequest().writes(writes);
}

public ClientWriteRequest writes(List<ClientTupleKey> writes) {
this.writes = writes;
return this;
Expand All @@ -27,6 +31,10 @@ public List<ClientTupleKey> getWrites() {
return writes;
}

public static ClientWriteRequest ofDeletes(List<ClientTupleKey> deletes) {
return new ClientWriteRequest().deletes(deletes);
}

public ClientWriteRequest deletes(List<ClientTupleKey> deletes) {
this.deletes = deletes;
return this;
Expand Down
Loading