Skip to content

Commit 12af785

Browse files
authored
Merge pull request #24 from openfga/refactor/java-dont-require-apiclient
refactor: simplify constructors, make client options consistent, bump to 0.1.0
2 parents 971b93b + eeb1a76 commit 12af785

19 files changed

Lines changed: 212 additions & 74 deletions

.openapi-generator/FILES

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,18 @@ src/main/java/dev/openfga/sdk/api/configuration/ClientConfiguration.java
9898
src/main/java/dev/openfga/sdk/api/configuration/ClientCredentials.java
9999
src/main/java/dev/openfga/sdk/api/configuration/ClientExpandOptions.java
100100
src/main/java/dev/openfga/sdk/api/configuration/ClientListObjectsOptions.java
101+
src/main/java/dev/openfga/sdk/api/configuration/ClientListStoresOptions.java
102+
src/main/java/dev/openfga/sdk/api/configuration/ClientReadAssertionsOptions.java
103+
src/main/java/dev/openfga/sdk/api/configuration/ClientReadAuthorizationModelOptions.java
104+
src/main/java/dev/openfga/sdk/api/configuration/ClientReadAuthorizationModelsOptions.java
105+
src/main/java/dev/openfga/sdk/api/configuration/ClientReadChangesOptions.java
101106
src/main/java/dev/openfga/sdk/api/configuration/ClientReadOptions.java
107+
src/main/java/dev/openfga/sdk/api/configuration/ClientWriteAssertionsOptions.java
102108
src/main/java/dev/openfga/sdk/api/configuration/ClientWriteOptions.java
103109
src/main/java/dev/openfga/sdk/api/configuration/Configuration.java
104110
src/main/java/dev/openfga/sdk/api/configuration/ConfigurationOverride.java
105111
src/main/java/dev/openfga/sdk/api/configuration/Credentials.java
106112
src/main/java/dev/openfga/sdk/api/configuration/CredentialsMethod.java
107-
src/main/java/dev/openfga/sdk/api/configuration/ListStoresOptions.java
108-
src/main/java/dev/openfga/sdk/api/configuration/ReadAuthorizationModelOptions.java
109-
src/main/java/dev/openfga/sdk/api/configuration/ReadAuthorizationModelsOptions.java
110-
src/main/java/dev/openfga/sdk/api/configuration/ReadChangesOptions.java
111113
src/main/java/dev/openfga/sdk/api/model/AbstractOpenApiSchema.java
112114
src/main/java/dev/openfga/sdk/api/model/Any.java
113115
src/main/java/dev/openfga/sdk/api/model/Assertion.java

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Changelog
22

3+
## v0.1.0
4+
5+
### [0.1.0](https://github.com/openfga/java-sdk/compare/v0.0.5...v0.1.0) (2023-09-27)
6+
7+
- [BREAKING] refactor(client): simplify OpenFgaClient and OpenFgaApi constructors to not require
8+
an ApiClient. This is a breaking change as it changed the ordering of parameters in constructors.
9+
- [BREAKING] refactor(client): all options classes for OpenFgaClient are now consistently prefixed
10+
with "Client"
11+
- chore(client): add ClientReadAssertionsOptions and ClientWriteAssertionsOptions for their
12+
respective Client APIs.
13+
314
## v0.0.5
415

516
### [0.0.5](https://github.com/openfga/java-sdk/compare/v0.0.4...v0.0.5) (2023-09-27)

README.md

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ It can be used with the following:
7474
* Gradle (Groovy)
7575

7676
```groovy
77-
implementation 'dev.openfga:openfga-sdk:0.0.5'
77+
implementation 'dev.openfga:openfga-sdk:0.1.0'
7878
```
7979

8080
* Gradle (Kotlin)
8181

8282
```kotlin
83-
implementation("dev.openfga:openfga-sdk:0.0.5")
83+
implementation("dev.openfga:openfga-sdk:0.1.0")
8484
```
8585

8686
* Apache Maven
@@ -89,26 +89,26 @@ implementation("dev.openfga:openfga-sdk:0.0.5")
8989
<dependency>
9090
<groupId>dev.openfga</groupId>
9191
<artifactId>openfga-sdk</artifactId>
92-
<version>0.0.5</version>
92+
<version>0.1.0</version>
9393
</dependency>
9494
```
9595

9696
* Ivy
9797

9898
```xml
99-
<dependency org="dev.openfga" name="openfga-sdk" rev="0.0.5"/>
99+
<dependency org="dev.openfga" name="openfga-sdk" rev="0.1.0"/>
100100
```
101101

102102
* SBT
103103

104104
```scala
105-
libraryDependencies += "dev.openfga" % "openfga-sdk" % "0.0.5"
105+
libraryDependencies += "dev.openfga" % "openfga-sdk" % "0.1.0"
106106
```
107107

108108
* Leiningen
109109

110110
```edn
111-
[dev.openfga/openfga-sdk "0.0.5"]
111+
[dev.openfga/openfga-sdk "0.1.0"]
112112
```
113113

114114

@@ -122,20 +122,18 @@ libraryDependencies += "dev.openfga" % "openfga-sdk" % "0.0.5"
122122

123123
```java
124124
import com.fasterxml.jackson.databind.ObjectMapper;
125-
import dev.openfga.sdk.api.client.ApiClient;
126125
import dev.openfga.sdk.api.client.OpenFgaClient;
127126
import dev.openfga.sdk.api.configuration.ClientConfiguration;
128127
import java.net.http.HttpClient;
129128

130129
public class Example {
131130
public static void main(String[] args) throws Exception {
132-
var clientConfig = new ClientConfiguration()
131+
var config = new ClientConfiguration()
133132
.apiUrl(System.getenv("OPENFGA_API_URL")) // If not specified, will default to "https://localhost:8080"
134133
.storeId(System.getenv("OPENFGA_STORE_ID")) // Not required when calling createStore() or listStores()
135134
.authorizationModelId(System.getenv("OPENFGA_AUTHORIZATION_MODEL_ID")); // Optional, can be overridden per request
136-
var apiClient = new ApiClient(HttpClient.newBuilder(), new ObjectMapper());
137135

138-
var fgaClient = new OpenFgaClient(apiClient, clientConfig);
136+
var fgaClient = new OpenFgaClient(config);
139137
var response = fgaClient.readAuthorizationModels().get();
140138
}
141139
}
@@ -145,7 +143,6 @@ public class Example {
145143

146144
```java
147145
import com.fasterxml.jackson.databind.ObjectMapper;
148-
import dev.openfga.sdk.api.client.ApiClient;
149146
import dev.openfga.sdk.api.client.OpenFgaClient;
150147
import dev.openfga.sdk.api.configuration.ApiToken;
151148
import dev.openfga.sdk.api.configuration.ClientConfiguration;
@@ -154,16 +151,15 @@ import java.net.http.HttpClient;
154151

155152
public class Example {
156153
public static void main(String[] args) throws Exception {
157-
var clientConfig = new ClientConfiguration()
154+
var config = new ClientConfiguration()
158155
.apiUrl(System.getenv("OPENFGA_API_URL")) // If not specified, will default to "https://localhost:8080"
159156
.storeId(System.getenv("OPENFGA_STORE_ID")) // Not required when calling createStore() or listStores()
160157
.authorizationModelId(System.getenv("OPENFGA_AUTHORIZATION_MODEL_ID")) // Optional, can be overridden per request
161158
.credentials(new Credentials(
162159
new ApiToken(System.getenv("OPENFGA_API_TOKEN")) // will be passed as the "Authorization: Bearer ${ApiToken}" request header
163160
));
164-
var apiClient = new ApiClient(HttpClient.newBuilder(), new ObjectMapper());
165161

166-
var fgaClient = new OpenFgaClient(apiClient, clientConfig);
162+
var fgaClient = new OpenFgaClient(config);
167163
var response = fgaClient.readAuthorizationModels().get();
168164
}
169165
}
@@ -173,7 +169,6 @@ public class Example {
173169

174170
```java
175171
import com.fasterxml.jackson.databind.ObjectMapper;
176-
import dev.openfga.sdk.api.client.ApiClient;
177172
import dev.openfga.sdk.api.client.OpenFgaClient;
178173
import dev.openfga.sdk.api.configuration.ClientConfiguration;
179174
import dev.openfga.sdk.api.configuration.ClientCredentials;
@@ -182,7 +177,7 @@ import java.net.http.HttpClient;
182177

183178
public class Example {
184179
public static void main(String[] args) throws Exception {
185-
var clientConfig = new ClientConfiguration()
180+
var config = new ClientConfiguration()
186181
.apiUrl(System.getenv("OPENFGA_API_URL")) // If not specified, will default to "https://localhost:8080"
187182
.storeId(System.getenv("OPENFGA_STORE_ID")) // Not required when calling createStore() or listStores()
188183
.authorizationModelId(System.getenv("OPENFGA_AUTHORIZATION_MODEL_ID")) // Optional, can be overridden per request
@@ -193,9 +188,8 @@ public class Example {
193188
.clientId(System.getenv("OPENFGA_CLIENT_ID"))
194189
.clientSecret(System.getenv("OPENFGA_CLIENT_SECRET"))
195190
));
196-
var apiClient = new ApiClient(HttpClient.newBuilder(), new ObjectMapper());
197191

198-
var fgaClient = new OpenFgaClient(apiClient, clientConfig);
192+
var fgaClient = new OpenFgaClient(config);
199193
var response = fgaClient.readAuthorizationModels().get();
200194
}
201195
}
@@ -219,7 +213,7 @@ Get a paginated list of stores.
219213
[API Documentation](https://openfga.dev/api/service/docs/api#/Stores/ListStores)
220214

221215
```java
222-
var options = new ListStoresOptions()
216+
var options = new ClientListStoresOptions()
223217
.pageSize(10)
224218
.continuationToken("...");
225219
var stores = fgaClient.listStores(options);
@@ -282,7 +276,7 @@ Read all authorization models in the store.
282276
[API Documentation](https://openfga.dev/api/service#/Authorization%20Models/ReadAuthorizationModels)
283277

284278
```java
285-
var options = new ReadAuthorizationModelsOptions()
279+
var options = new ClientReadAuthorizationModelsOptions()
286280
.pageSize(10)
287281
.continuationToken("...");
288282
var response = fgaClient.readAuthorizationModels(options).get();
@@ -345,7 +339,7 @@ Read a particular authorization model.
345339
[API Documentation](https://openfga.dev/api/service#/Authorization%20Models/ReadAuthorizationModel)
346340

347341
```java
348-
var options = new ReadAuthorizationModelOptions()
342+
var options = new ClientReadAuthorizationModelOptions()
349343
// You can rely on the model id set in the configuration or override it for this specific request
350344
.authorizationModelId("01GXSA8YR785C4FYS3C0RTG7B1");
351345

@@ -572,7 +566,9 @@ Read assertions for a particular authorization model.
572566
[API Documentation](https://openfga.dev/api/service#/Assertions/Read%20Assertions)
573567

574568
```java
575-
var response = fgaClient.readAssertions().get();
569+
var options = new ClientReadAssertionsOptions()
570+
.authorizationModelId("01GXSA8YR785C4FYS3C0RTG7B1");
571+
var response = fgaClient.readAssertions(options).get();
576572
```
577573

578574
##### Write Assertions
@@ -582,14 +578,16 @@ Update the assertions for a particular authorization model.
582578
[API Documentation](https://openfga.dev/api/service#/Assertions/Write%20Assertions)
583579

584580
```java
581+
var options = new ClientWriteAssertionsOptions()
582+
.authorizationModelId("01GXSA8YR785C4FYS3C0RTG7B1");
585583
var assertions = List.of(
586584
new ClientAssertion()
587585
.user("user:81684243-9356-4421-8fbf-a4f8d36aa31b")
588586
.relation("viewer")
589587
._object("document:roadmap")
590588
.expectation(true)
591589
);
592-
fgaClient.writeAssertions(assertions).get();
590+
fgaClient.writeAssertions(assertions, options).get();
593591
```
594592

595593

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ plugins {
1818
apply from: 'publish.gradle'
1919

2020
group = 'dev.openfga'
21-
version = '0.0.5'
21+
version = '0.1.0'
2222

2323
repositories {
2424
mavenCentral()

publish.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ publishing {
66
pom {
77
group = 'dev.openfga'
88
name = 'openfga-sdk'
9-
version = '0.0.5'
9+
version = '0.1.0'
1010
description = 'This is an autogenerated Java SDK for OpenFGA. It provides a wrapper around the [OpenFGA API definition](https://openfga.dev/api).'
1111
url = 'https://openfga.dev'
1212
licenses {

src/main/java/dev/openfga/sdk/api/OpenFgaApi.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ public class OpenFgaApi {
6666
private final Consumer<HttpResponse<InputStream>> memberVarResponseInterceptor;
6767
private final Consumer<HttpResponse<String>> memberVarAsyncResponseInterceptor;
6868

69-
public OpenFgaApi(ApiClient apiClient, Configuration configuration) throws FgaInvalidParameterException {
69+
public OpenFgaApi(Configuration configuration) throws FgaInvalidParameterException {
70+
this(configuration, new ApiClient());
71+
}
72+
73+
public OpenFgaApi(Configuration configuration, ApiClient apiClient) throws FgaInvalidParameterException {
7074
memberVarHttpClient = apiClient.getHttpClient();
7175
memberVarObjectMapper = apiClient.getObjectMapper();
7276
this.configuration = configuration;

src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ public class OpenFgaClient {
3030
private static final String CLIENT_METHOD_HEADER = "X-OpenFGA-Client-Method";
3131
private static final int DEFAULT_MAX_METHOD_PARALLEL_REQS = 10;
3232

33-
public OpenFgaClient(ApiClient apiClient, ClientConfiguration configuration) throws FgaInvalidParameterException {
33+
public OpenFgaClient(ClientConfiguration configuration) throws FgaInvalidParameterException {
34+
this(configuration, new ApiClient());
35+
}
36+
37+
public OpenFgaClient(ClientConfiguration configuration, ApiClient apiClient) throws FgaInvalidParameterException {
3438
this.apiClient = apiClient;
3539
this.configuration = configuration;
36-
this.api = new OpenFgaApi(apiClient, configuration);
40+
this.api = new OpenFgaApi(configuration, apiClient);
3741
}
3842

3943
/* ***********
@@ -50,7 +54,7 @@ public void setAuthorizationModelId(String authorizationModelId) {
5054

5155
public void setConfiguration(ClientConfiguration configuration) throws FgaInvalidParameterException {
5256
this.configuration = configuration;
53-
this.api = new OpenFgaApi(apiClient, configuration);
57+
this.api = new OpenFgaApi(configuration, apiClient);
5458
}
5559

5660
/* ********
@@ -65,7 +69,7 @@ public CompletableFuture<ListStoresResponse> listStores() throws FgaInvalidParam
6569
return call(() -> api.listStores(null, null));
6670
}
6771

68-
public CompletableFuture<ListStoresResponse> listStores(ListStoresOptions options)
72+
public CompletableFuture<ListStoresResponse> listStores(ClientListStoresOptions options)
6973
throws FgaInvalidParameterException {
7074
configuration.assertValid();
7175
return call(() -> api.listStores(options.getPageSize(), options.getContinuationToken()));
@@ -121,7 +125,7 @@ public CompletableFuture<ReadAuthorizationModelsResponse> readAuthorizationModel
121125
* @throws FgaInvalidParameterException When the Store ID is null, empty, or whitespace
122126
*/
123127
public CompletableFuture<ReadAuthorizationModelsResponse> readAuthorizationModels(
124-
ReadAuthorizationModelsOptions options) throws FgaInvalidParameterException {
128+
ClientReadAuthorizationModelsOptions options) throws FgaInvalidParameterException {
125129
configuration.assertValid();
126130
String storeId = configuration.getStoreIdChecked();
127131

@@ -171,7 +175,7 @@ public CompletableFuture<ReadAuthorizationModelResponse> readAuthorizationModel(
171175
* @throws FgaInvalidParameterException When either the Store ID or Authorization Model ID are null, empty, or whitespace
172176
*/
173177
public CompletableFuture<ReadAuthorizationModelResponse> readAuthorizationModel(
174-
ReadAuthorizationModelOptions options) throws FgaInvalidParameterException {
178+
ClientReadAuthorizationModelOptions options) throws FgaInvalidParameterException {
175179
configuration.assertValid();
176180
String storeId = configuration.getStoreIdChecked();
177181
String authorizationModelId = options.getAuthorizationModelIdChecked();
@@ -201,7 +205,7 @@ public CompletableFuture<ReadAuthorizationModelResponse> readLatestAuthorization
201205
*
202206
* @throws FgaInvalidParameterException When the Store ID is null, empty, or whitespace
203207
*/
204-
public CompletableFuture<ReadChangesResponse> readChanges(ReadChangesOptions options)
208+
public CompletableFuture<ReadChangesResponse> readChanges(ClientReadChangesOptions options)
205209
throws FgaInvalidParameterException {
206210
configuration.assertValid();
207211
String storeId = configuration.getStoreIdChecked();
@@ -464,10 +468,26 @@ public CompletableFuture<ListObjectsResponse> listObjects(
464468
* @throws FgaInvalidParameterException When either the Store ID or Authorization Model ID is null, empty, or whitespace
465469
*/
466470
public CompletableFuture<ReadAssertionsResponse> readAssertions() throws FgaInvalidParameterException {
467-
// TODO: Add version of this function that accepts ClientReadAssertionsOptions
471+
return readAssertions(null);
472+
}
473+
474+
/**
475+
* ReadAssertions - Read assertions for a particular authorization model
476+
*
477+
* @throws FgaInvalidParameterException When either the Store ID or Authorization Model ID is null, empty, or whitespace
478+
*/
479+
public CompletableFuture<ReadAssertionsResponse> readAssertions(ClientReadAssertionsOptions options)
480+
throws FgaInvalidParameterException {
468481
configuration.assertValid();
469482
String storeId = configuration.getStoreIdChecked();
470-
String authorizationModelId = configuration.getAuthorizationModelIdChecked();
483+
484+
String authorizationModelId;
485+
if (options != null && options.hasValidAuthorizationModelId()) {
486+
authorizationModelId = options.getAuthorizationModelId();
487+
} else {
488+
authorizationModelId = configuration.getAuthorizationModelIdChecked();
489+
}
490+
471491
return call(() -> api.readAssertions(storeId, authorizationModelId));
472492
}
473493

@@ -478,10 +498,26 @@ public CompletableFuture<ReadAssertionsResponse> readAssertions() throws FgaInva
478498
*/
479499
public CompletableFuture<Void> writeAssertions(List<ClientAssertion> assertions)
480500
throws FgaInvalidParameterException {
481-
// TODO: Add version of this function that accepts ClientWriteAssertionsOptions
501+
return writeAssertions(assertions, null);
502+
}
503+
504+
/**
505+
* WriteAssertions - Updates assertions for a particular authorization model
506+
*
507+
* @throws FgaInvalidParameterException When either the Store ID or Authorization Model ID is null, empty, or whitespace
508+
*/
509+
public CompletableFuture<Void> writeAssertions(
510+
List<ClientAssertion> assertions, ClientWriteAssertionsOptions options)
511+
throws FgaInvalidParameterException {
482512
configuration.assertValid();
483513
String storeId = configuration.getStoreIdChecked();
484-
String authorizationModelId = configuration.getAuthorizationModelIdChecked();
514+
515+
String authorizationModelId;
516+
if (options != null && options.hasValidAuthorizationModelId()) {
517+
authorizationModelId = options.getAuthorizationModelId();
518+
} else {
519+
authorizationModelId = configuration.getAuthorizationModelIdChecked();
520+
}
485521

486522
WriteAssertionsRequest body = new WriteAssertionsRequest().assertions(ClientAssertion.asAssertions(assertions));
487523

src/main/java/dev/openfga/sdk/api/configuration/ListStoresOptions.java renamed to src/main/java/dev/openfga/sdk/api/configuration/ClientListStoresOptions.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212

1313
package dev.openfga.sdk.api.configuration;
1414

15-
public class ListStoresOptions {
15+
public class ClientListStoresOptions {
1616
private Integer pageSize;
1717
private String continuationToken;
1818

19-
public ListStoresOptions pageSize(Integer pageSize) {
19+
public ClientListStoresOptions pageSize(Integer pageSize) {
2020
this.pageSize = pageSize;
2121
return this;
2222
}
@@ -25,7 +25,7 @@ public Integer getPageSize() {
2525
return pageSize;
2626
}
2727

28-
public ListStoresOptions continuationToken(String continuationToken) {
28+
public ClientListStoresOptions continuationToken(String continuationToken) {
2929
this.continuationToken = continuationToken;
3030
return this;
3131
}

0 commit comments

Comments
 (0)