Skip to content

Commit

Permalink
feat: Polish OpenFgaClient check(...) and expand(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
booniepepper committed Sep 27, 2023
1 parent b75b41e commit ee57ba2
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 63 deletions.
4 changes: 4 additions & 0 deletions .openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,17 @@ src/main/java/dev/openfga/sdk/api/auth/CredentialsFlowResponse.java
src/main/java/dev/openfga/sdk/api/auth/OAuth2Client.java
src/main/java/dev/openfga/sdk/api/client/ApiClient.java
src/main/java/dev/openfga/sdk/api/client/ApiResponse.java
src/main/java/dev/openfga/sdk/api/client/ClientCheckRequest.java
src/main/java/dev/openfga/sdk/api/client/ClientExpandRequest.java
src/main/java/dev/openfga/sdk/api/client/ClientReadRequest.java
src/main/java/dev/openfga/sdk/api/client/ClientWriteRequest.java
src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java
src/main/java/dev/openfga/sdk/api/configuration/ApiToken.java
src/main/java/dev/openfga/sdk/api/configuration/BaseConfiguration.java
src/main/java/dev/openfga/sdk/api/configuration/ClientCheckOptions.java
src/main/java/dev/openfga/sdk/api/configuration/ClientConfiguration.java
src/main/java/dev/openfga/sdk/api/configuration/ClientCredentials.java
src/main/java/dev/openfga/sdk/api/configuration/ClientExpandOptions.java
src/main/java/dev/openfga/sdk/api/configuration/ClientReadOptions.java
src/main/java/dev/openfga/sdk/api/configuration/ClientWriteOptions.java
src/main/java/dev/openfga/sdk/api/configuration/Configuration.java
Expand Down
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,16 @@ var request = new ClientReadRequest()
._object("document:roadmap");

// Read all stored relationship tuples
var request = new ClientReadRequest()
var request = new ClientReadRequest();

var options = new ClientReadOptions()
.pageSize(10)
.continuationToken("...");

var response = fgaClient.read(request).get();
var response = fgaClient.read(request, options).get();

// In all the above situations, the response will be of the form:
// response = { Tuples: [{ Key: { User, Relation, Object }, Timestamp }, ...]}
// response = { tuples: [{ key: { user, relation, object }, timestamp }, ...]}
```

##### Write (Create and Delete) Relationship Tuples
Expand Down Expand Up @@ -374,6 +376,16 @@ Check if a user has a particular relation with an object.
[API Documentation](https://openfga.dev/api/service#/Relationship%20Queries/Check)

```java
var request = new ClientCheckRequest()
.user("user:81684243-9356-4421-8fbf-a4f8d36aa31b")
.relation("writer")
._object("document:roadmap");
var options = new ClientCheckOptions()
// You can rely on the model id set in the configuration or override it for this specific request
.authorizationModelId("01GXSA8YR785C4FYS3C0RTG7B1");

var response = fgaClient.check(request, options).get();
// response.Allowed = true
```

##### Batch Check
Expand All @@ -382,6 +394,7 @@ Run a set of [checks](#check). Batch Check will return `allowed: false` if it en
If 429s or 5xxs are encountered, the underlying check will retry up to 15 times before giving up.

```java
// Coming soon
```

##### Expand
Expand Down
58 changes: 58 additions & 0 deletions src/main/java/dev/openfga/sdk/api/client/ClientCheckRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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;

public class ClientCheckRequest {
private String user;
private String relation;
private String _object;

public ClientCheckRequest _object(String _object) {
this._object = _object;
return this;
}

/**
* Get _object
* @return _object
**/
public String getObject() {
return _object;
}

public ClientCheckRequest relation(String relation) {
this.relation = relation;
return this;
}

/**
* Get relation
* @return relation
**/
public String getRelation() {
return relation;
}

public ClientCheckRequest user(String user) {
this.user = user;
return this;
}

/**
* Get user
* @return user
**/
public String getUser() {
return user;
}
}
58 changes: 58 additions & 0 deletions src/main/java/dev/openfga/sdk/api/client/ClientExpandRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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;

public class ClientExpandRequest {
private String user;
private String relation;
private String _object;

public ClientExpandRequest _object(String _object) {
this._object = _object;
return this;
}

/**
* Get _object
* @return _object
**/
public String getObject() {
return _object;
}

public ClientExpandRequest relation(String relation) {
this.relation = relation;
return this;
}

/**
* Get relation
* @return relation
**/
public String getRelation() {
return relation;
}

public ClientExpandRequest user(String user) {
this.user = user;
return this;
}

/**
* Get user
* @return user
**/
public String getUser() {
return user;
}
}
62 changes: 58 additions & 4 deletions src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,37 @@ public CompletableFuture<Object> deleteTuples(TupleKeys tupleKeys) throws FgaInv
*
* @throws FgaInvalidParameterException When the Store ID is null, empty, or whitespace
*/
public CompletableFuture<CheckResponse> check(CheckRequest request) throws FgaInvalidParameterException {
public CompletableFuture<CheckResponse> check(ClientCheckRequest request) throws FgaInvalidParameterException {
return check(request, null);
}

/**
* Check - Check if a user has a particular relation with an object (evaluates)
*
* @throws FgaInvalidParameterException When the Store ID is null, empty, or whitespace
*/
public CompletableFuture<CheckResponse> check(ClientCheckRequest request, ClientCheckOptions options)
throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
return call(() -> api.check(storeId, request));

CheckRequest body = new CheckRequest();

if (request != null) {
body.tupleKey(new TupleKey()
.user(request.getUser())
.relation(request.getRelation())
._object(request.getObject()));
}

if (options != null && !isNullOrWhitespace(options.getAuthorizationModelId())) {
body.authorizationModelId(options.getAuthorizationModelId());
} else {
String authorizationModelId = configuration.getAuthorizationModelId();
body.authorizationModelId(authorizationModelId);
}

return call(() -> api.check(storeId, body));
}

/**
Expand All @@ -323,10 +350,37 @@ public CompletableFuture<CheckResponse> check(CheckRequest request) throws FgaIn
*
* @throws FgaInvalidParameterException When the Store ID is null, empty, or whitespace
*/
public CompletableFuture<ExpandResponse> expand(ExpandRequest request) throws FgaInvalidParameterException {
public CompletableFuture<ExpandResponse> expand(ClientExpandRequest request) throws FgaInvalidParameterException {
return expand(request, null);
}

/**
* Expand - Expands the relationships in userset tree format (evaluates)
*
* @throws FgaInvalidParameterException When the Store ID is null, empty, or whitespace
*/
public CompletableFuture<ExpandResponse> expand(ClientExpandRequest request, ClientExpandOptions options)
throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
return call(() -> api.expand(storeId, request));

ExpandRequest body = new ExpandRequest();

if (request != null) {
body.tupleKey(new TupleKey()
.user(request.getUser())
.relation(request.getRelation())
._object(request.getObject()));
}

if (options != null && !isNullOrWhitespace(options.getAuthorizationModelId())) {
body.authorizationModelId(options.getAuthorizationModelId());
} else {
String authorizationModelId = configuration.getAuthorizationModelId();
body.authorizationModelId(authorizationModelId);
}

return call(() -> api.expand(storeId, body));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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 ClientCheckOptions {
private String authorizationModelId;

public ClientCheckOptions authorizationModelId(String authorizationModelId) {
this.authorizationModelId = authorizationModelId;
return this;
}

public String getAuthorizationModelId() {
return authorizationModelId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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 ClientExpandOptions {
private String authorizationModelId;

public ClientExpandOptions authorizationModelId(String authorizationModelId) {
this.authorizationModelId = authorizationModelId;
return this;
}

public String getAuthorizationModelId() {
return authorizationModelId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ public void write_and_check() throws Exception {
String authModelId = writeAuthModel(storeId);
fga.setAuthorizationModelId(authModelId);
ClientWriteRequest writeRequest = new ClientWriteRequest().writes(List.of(DEFAULT_TUPLE_KEY));
CheckRequest checkRequest = new CheckRequest()
.tupleKey(new TupleKey().user(DEFAULT_USER).relation("reader")._object(DEFAULT_DOC));
ClientCheckRequest checkRequest =
new ClientCheckRequest().user(DEFAULT_USER).relation("reader")._object(DEFAULT_DOC);

// When
fga.write(writeRequest).get();
Expand All @@ -238,8 +238,8 @@ public void write_and_expand() throws Exception {
String authModelId = writeAuthModel(storeId);
fga.setAuthorizationModelId(authModelId);
ClientWriteRequest writeRequest = new ClientWriteRequest().writes(List.of(DEFAULT_TUPLE_KEY));
ExpandRequest expandRequest =
new ExpandRequest().tupleKey(new TupleKey()._object(DEFAULT_DOC).relation("reader"));
ClientExpandRequest expandRequest =
new ClientExpandRequest()._object(DEFAULT_DOC).relation("reader");

// When
fga.write(writeRequest).get();
Expand Down
Loading

0 comments on commit ee57ba2

Please sign in to comment.