Skip to content

Commit

Permalink
feat(java-sdk): Add OpenFgaClient
Browse files Browse the repository at this point in the history
  • Loading branch information
booniepepper committed Sep 18, 2023
1 parent 7d9cacf commit aa94c70
Show file tree
Hide file tree
Showing 10 changed files with 512 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,13 @@ 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/ListStoresOptions.java
src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java
src/main/java/dev/openfga/sdk/api/client/ReadAuthorizationModelsOptions.java
src/main/java/dev/openfga/sdk/api/client/ReadChangesOptions.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/ClientConfiguration.java
src/main/java/dev/openfga/sdk/api/configuration/ClientCredentials.java
src/main/java/dev/openfga/sdk/api/configuration/Configuration.java
src/main/java/dev/openfga/sdk/api/configuration/ConfigurationOverride.java
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/dev/openfga/sdk/api/client/ListStoresOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 ListStoresOptions {
private Integer pageSize;
private String continuationToken;

public ListStoresOptions pageSize(Integer pageSize) {
this.pageSize = pageSize;
return this;
}

public Integer getPageSize() {
return pageSize;
}

public ListStoresOptions continuationToken(String continuationToken) {
this.continuationToken = continuationToken;
return this;
}

public String getContinuationToken() {
return continuationToken;
}
}
283 changes: 283 additions & 0 deletions src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
/*
* 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 static dev.openfga.sdk.util.StringUtil.isNullOrWhitespace;

import dev.openfga.sdk.api.*;
import dev.openfga.sdk.api.configuration.*;
import dev.openfga.sdk.api.model.*;
import dev.openfga.sdk.errors.*;
import java.util.concurrent.CompletableFuture;

public class OpenFgaClient {
private final ClientConfiguration configuration;
private final OpenFgaApi api;

private static final String CLIENT_BULK_REQUEST_ID_HEADER = "X-OpenFGA-Client-Bulk-Request-Id";
private static final String CLIENT_METHOD_HEADER = "X-OpenFGA-Client-Method";
private static final int DEFAULT_MAX_METHOD_PARALLEL_REQS = 10;

public OpenFgaClient(ApiClient apiClient, ClientConfiguration configuration) throws FgaInvalidParameterException {
this.configuration = configuration;
this.api = new OpenFgaApi(apiClient, configuration);
}

/* ***********
* Utilities *
*************/

public void setStoreId(String storeId) {
configuration.storeId(storeId);
}

public void setAuthorizationModelId(String authorizationModelId) {
configuration.authorizationModelId(authorizationModelId);
}

/* ********
* Stores *
**********/

/**
* ListStores - Get a paginated list of stores.
*/
public CompletableFuture<ListStoresResponse> listStores() throws FgaInvalidParameterException, ApiException {
return api.listStores(null, null);
}

public CompletableFuture<ListStoresResponse> listStores(ListStoresOptions options)
throws FgaInvalidParameterException, ApiException {
return api.listStores(options.getPageSize(), options.getContinuationToken());
}

/**
* CreateStore - Initialize a store
*/
public CompletableFuture<CreateStoreResponse> createStore(CreateStoreRequest request)
throws FgaInvalidParameterException, ApiException {
return api.createStore(request);
}

/**
* GetStore - Get information about the current store.
*
* @throws FgaInvalidParameterException When the Store ID is null, empty, or whitespace
*/
public CompletableFuture<GetStoreResponse> getStore() throws FgaInvalidParameterException, ApiException {
String storeId = configuration.getStoreIdChecked();
return api.getStore(storeId);
}

/**
* DeleteStore - Delete a store
*
* @throws FgaInvalidParameterException When the Store ID is null, empty, or whitespace
*/
public CompletableFuture<Void> deleteStore() throws FgaInvalidParameterException, ApiException {
String storeId = configuration.getStoreIdChecked();
return api.deleteStore(storeId);
}

/* **********************
* Authorization Models *
************************/

/**
* ReadAuthorizationModels - Read all authorization models
*
* @throws FgaInvalidParameterException When the Store ID is null, empty, or whitespace
*/
public CompletableFuture<ReadAuthorizationModelsResponse> ReadAuthorizationModels(
ReadAuthorizationModelsOptions options) throws FgaInvalidParameterException, ApiException {
String storeId = configuration.getStoreIdChecked();
return api.readAuthorizationModels(storeId, options.getPageSize(), options.getContinuationToken());
}

/**
* WriteAuthorizationModel - Create a new version of the authorization model
*
* @throws FgaInvalidParameterException When the Store ID is null, empty, or whitespace
*/
public CompletableFuture<WriteAuthorizationModelResponse> writeAuthorizationModel(
WriteAuthorizationModelRequest request) throws FgaInvalidParameterException, ApiException {
String storeId = configuration.getStoreIdChecked();
return api.writeAuthorizationModel(storeId, request);
}

/**
* ReadAuthorizationModel - Read the current authorization model
*
* @throws FgaInvalidParameterException When either the Store ID or Authorization Model ID are null, empty, or whitespace
*/
public CompletableFuture<ReadAuthorizationModelResponse> readAuthorizationModel()
throws FgaInvalidParameterException, ApiException {
String storeId = configuration.getStoreIdChecked();
String authorizationModelId = configuration.getAuthorizationModelIdChecked();
return api.readAuthorizationModel(storeId, authorizationModelId);
}

/**
* ReadLatestAuthorizationModel - Read the latest authorization model for the current store
*
* @throws FgaInvalidParameterException When the Store ID is null, empty, or whitespace
*/
public CompletableFuture<ReadAuthorizationModelResponse> readLatestAuthorizationModel()
throws FgaInvalidParameterException, ApiException {
String storeId = configuration.getStoreIdChecked();
return api.readAuthorizationModels(storeId, 1, null).thenApply(response -> new ReadAuthorizationModelResponse()
.authorizationModel(response.getAuthorizationModels().get(0)));
}

/* *********************
* Relationship Tuples *
***********************/

/**
* Read Changes - Read the list of historical relationship tuple writes and deletes
*
* @throws FgaInvalidParameterException When the Store ID is null, empty, or whitespace
*/
public CompletableFuture<ReadChangesResponse> ReadChanges(ReadChangesOptions options)
throws FgaInvalidParameterException, ApiException {
String storeId = configuration.getStoreIdChecked();
return api.readChanges(storeId, options.getType(), options.getPageSize(), options.getContinuationToken());
}

/**
* Read - Read tuples previously written to the store (does not evaluate)
*
* @throws FgaInvalidParameterException When the Store ID is null, empty, or whitespace
*/
public CompletableFuture<ReadResponse> read(ReadRequest body) throws FgaInvalidParameterException, ApiException {
String storeId = configuration.getStoreIdChecked();
return api.read(storeId, body);
}

/**
* Write - Create or delete relationship tuples
*
* @throws FgaInvalidParameterException When the Store ID is null, empty, or whitespace
*/
public CompletableFuture<Object> write(WriteRequest request) throws FgaInvalidParameterException, ApiException {
String storeId = configuration.getStoreIdChecked();
return api.write(storeId, request);
}

/**
* WriteTuples - Utility method to write tuples, wraps Write
*
* @throws FgaInvalidParameterException When the Store ID is null, empty, or whitespace
*/
public CompletableFuture<Object> writeTuples(TupleKeys tupleKeys)
throws FgaInvalidParameterException, ApiException {
var request = new WriteRequest().writes(tupleKeys);
String storeId = configuration.getStoreIdChecked();
String authorizationModelId = configuration.getAuthorizationModelId();
if (!isNullOrWhitespace(authorizationModelId)) {
request.authorizationModelId(authorizationModelId);
}
return api.write(storeId, request);
}

/**
* DeleteTuples - Utility method to delete tuples, wraps Write
*
* @throws FgaInvalidParameterException When the Store ID is null, empty, or whitespace
*/
public CompletableFuture<Object> deleteTuples(TupleKeys tupleKeys)
throws FgaInvalidParameterException, ApiException {
var request = new WriteRequest().deletes(tupleKeys);
String storeId = configuration.getStoreIdChecked();
String authorizationModelId = configuration.getAuthorizationModelId();
if (!isNullOrWhitespace(authorizationModelId)) {
request.authorizationModelId(authorizationModelId);
}
return api.write(storeId, request);
}

/* **********************
* Relationship Queries *
***********************/

/**
* 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(CheckRequest request)
throws FgaInvalidParameterException, ApiException {
String storeId = configuration.getStoreIdChecked();
return api.check(storeId, request);
}

/*
* BatchCheck - Run a set of checks (evaluates)
*/
// TODO

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

/**
* ListObjects - List the objects of a particular type that the user has a certain relation to (evaluates)
*
* @throws FgaInvalidParameterException When the Store ID is null, empty, or whitespace
*/
public CompletableFuture<ListObjectsResponse> listObjects(ListObjectsRequest request)
throws FgaInvalidParameterException, ApiException {
String storeId = configuration.getStoreIdChecked();
return api.listObjects(storeId, request);
}

/*
* ListRelations - List all the relations a user has with an object (evaluates)
*/
// TODO

/* ************
* Assertions *
**************/

/**
* ReadAssertions - Read assertions for a particular authorization model
*
* @throws FgaInvalidParameterException When either the Store ID or Authorization Model ID is null, empty, or whitespace
*/
public CompletableFuture<ReadAssertionsResponse> readAssertions()
throws FgaInvalidParameterException, ApiException {
String storeId = configuration.getStoreIdChecked();
String authorizationModelId = configuration.getAuthorizationModelIdChecked();
return api.readAssertions(storeId, authorizationModelId);
}

/**
* WriteAssertions - Updates assertions for a particular authorization model
*
* @throws FgaInvalidParameterException When either the Store ID or Authorization Model ID is null, empty, or whitespace
*/
public CompletableFuture<Void> writeAssertions(WriteAssertionsRequest request)
throws FgaInvalidParameterException, ApiException {
String storeId = configuration.getStoreIdChecked();
String authorizationModelId = configuration.getAuthorizationModelIdChecked();
return api.writeAssertions(storeId, authorizationModelId, request);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 ReadAuthorizationModelsOptions {
private Integer pageSize;
private String continuationToken;

public ReadAuthorizationModelsOptions pageSize(Integer pageSize) {
this.pageSize = pageSize;
return this;
}

public Integer getPageSize() {
return pageSize;
}

public ReadAuthorizationModelsOptions continuationToken(String continuationToken) {
this.continuationToken = continuationToken;
return this;
}

public String getContinuationToken() {
return continuationToken;
}
}
Loading

0 comments on commit aa94c70

Please sign in to comment.