diff --git a/src/main/java/dev/openfga/sdk/api/OpenFgaApi.java b/src/main/java/dev/openfga/sdk/api/OpenFgaApi.java index 3eaeb30..884bc5b 100644 --- a/src/main/java/dev/openfga/sdk/api/OpenFgaApi.java +++ b/src/main/java/dev/openfga/sdk/api/OpenFgaApi.java @@ -12,6 +12,8 @@ package dev.openfga.sdk.api; +import static dev.openfga.sdk.util.StringUtil.isNullOrWhitespace; + import dev.openfga.sdk.api.auth.*; import dev.openfga.sdk.api.client.*; import dev.openfga.sdk.api.configuration.*; @@ -41,10 +43,9 @@ import java.net.URI; import java.net.http.HttpRequest; import java.time.Duration; -import java.util.ArrayList; -import java.util.List; -import java.util.StringJoiner; import java.util.concurrent.CompletableFuture; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * A low-level API representation of an OpenFGA server. @@ -108,66 +109,20 @@ public CompletableFuture> check( private CompletableFuture> check( String storeId, CheckRequest body, Configuration configuration) throws ApiException, FgaInvalidParameterException { - try { - HttpRequest request = - checkRequestBuilder(storeId, body, configuration).build(); - return new HttpRequestAttempt<>(request, "check", CheckResponse.class, apiClient, configuration) - .attemptHttpRequest(); - } catch (ApiException e) { - return CompletableFuture.failedFuture(e); - } - } - - private HttpRequest.Builder checkRequestBuilder(String storeId, CheckRequest body, Configuration configuration) - throws ApiException, FgaInvalidParameterException { - // verify the required parameter 'storeId' is set - if (storeId == null) { - throw new ApiException(400, "Missing the required parameter 'storeId' when calling check"); - } - // verify the required parameter 'body' is set - if (body == null) { - throw new ApiException(400, "Missing the required parameter 'body' when calling check"); - } - - // verify the Configuration is valid - configuration.assertValid(); - - HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); - String localVarPath = "/stores/{store_id}/check".replace("{store_id}", ApiClient.urlEncode(storeId.toString())); + validate(storeId, "storeId", "check"); - localVarRequestBuilder.uri(URI.create(configuration.getApiUrl() + localVarPath)); + validate(body, "body", "check"); - localVarRequestBuilder.header("Content-Type", "application/json"); - localVarRequestBuilder.header("Accept", "application/json"); - - if (configuration.getCredentials().getCredentialsMethod() != CredentialsMethod.NONE) { - String accessToken = getAccessToken(configuration); - localVarRequestBuilder.header("Authorization", "Bearer " + accessToken); - } - - if (configuration.getUserAgent() != null) { - localVarRequestBuilder.header("User-Agent", configuration.getUserAgent()); - } - - if (configuration.getDefaultHeaders() != null) { - configuration.getDefaultHeaders().forEach(localVarRequestBuilder::header); - } + String path = "/stores/{store_id}/check".replace("{store_id}", ApiClient.urlEncode(storeId.toString())); try { - byte[] localVarPostBody = apiClient.getObjectMapper().writeValueAsBytes(body); - localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody)); - } catch (IOException e) { - throw new ApiException(e); - } - Duration readTimeout = configuration.getReadTimeout(); - if (readTimeout != null) { - localVarRequestBuilder.timeout(readTimeout); - } - if (apiClient.getRequestInterceptor() != null) { - apiClient.getRequestInterceptor().accept(localVarRequestBuilder); + HttpRequest request = buildHttpRequest("POST", path, body, configuration); + return new HttpRequestAttempt<>(request, "check", CheckResponse.class, apiClient, configuration) + .attemptHttpRequest(); + } catch (ApiException e) { + return CompletableFuture.failedFuture(e); } - return localVarRequestBuilder; } /** @@ -198,8 +153,13 @@ public CompletableFuture> createStore( private CompletableFuture> createStore( CreateStoreRequest body, Configuration configuration) throws ApiException, FgaInvalidParameterException { + + validate(body, "body", "createStore"); + + String path = "/stores"; + try { - HttpRequest request = createStoreRequestBuilder(body, configuration).build(); + HttpRequest request = buildHttpRequest("POST", path, body, configuration); return new HttpRequestAttempt<>(request, "createStore", CreateStoreResponse.class, apiClient, configuration) .attemptHttpRequest(); } catch (ApiException e) { @@ -207,54 +167,6 @@ private CompletableFuture> createStore( } } - private HttpRequest.Builder createStoreRequestBuilder(CreateStoreRequest body, Configuration configuration) - throws ApiException, FgaInvalidParameterException { - // verify the required parameter 'body' is set - if (body == null) { - throw new ApiException(400, "Missing the required parameter 'body' when calling createStore"); - } - - // verify the Configuration is valid - configuration.assertValid(); - - HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); - - String localVarPath = "/stores"; - - localVarRequestBuilder.uri(URI.create(configuration.getApiUrl() + localVarPath)); - - localVarRequestBuilder.header("Content-Type", "application/json"); - localVarRequestBuilder.header("Accept", "application/json"); - - if (configuration.getCredentials().getCredentialsMethod() != CredentialsMethod.NONE) { - String accessToken = getAccessToken(configuration); - localVarRequestBuilder.header("Authorization", "Bearer " + accessToken); - } - - if (configuration.getUserAgent() != null) { - localVarRequestBuilder.header("User-Agent", configuration.getUserAgent()); - } - - if (configuration.getDefaultHeaders() != null) { - configuration.getDefaultHeaders().forEach(localVarRequestBuilder::header); - } - - try { - byte[] localVarPostBody = apiClient.getObjectMapper().writeValueAsBytes(body); - localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody)); - } catch (IOException e) { - throw new ApiException(e); - } - Duration readTimeout = configuration.getReadTimeout(); - if (readTimeout != null) { - localVarRequestBuilder.timeout(readTimeout); - } - if (apiClient.getRequestInterceptor() != null) { - apiClient.getRequestInterceptor().accept(localVarRequestBuilder); - } - return localVarRequestBuilder; - } - /** * Delete a store * Delete an OpenFGA store. This does not delete the data associated with the store, like tuples or authorization models. @@ -282,9 +194,13 @@ public CompletableFuture> deleteStore(String storeId, Configur private CompletableFuture> deleteStore(String storeId, Configuration configuration) throws ApiException, FgaInvalidParameterException { + + validate(storeId, "storeId", "deleteStore"); + + String path = "/stores/{store_id}".replace("{store_id}", ApiClient.urlEncode(storeId.toString())); + try { - HttpRequest request = - deleteStoreRequestBuilder(storeId, configuration).build(); + HttpRequest request = buildHttpRequest("DELETE", path, configuration); return new HttpRequestAttempt<>(request, "deleteStore", Void.class, apiClient, configuration) .attemptHttpRequest(); } catch (ApiException e) { @@ -292,48 +208,6 @@ private CompletableFuture> deleteStore(String storeId, Configu } } - private HttpRequest.Builder deleteStoreRequestBuilder(String storeId, Configuration configuration) - throws ApiException, FgaInvalidParameterException { - // verify the required parameter 'storeId' is set - if (storeId == null) { - throw new ApiException(400, "Missing the required parameter 'storeId' when calling deleteStore"); - } - - // verify the Configuration is valid - configuration.assertValid(); - - HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); - - String localVarPath = "/stores/{store_id}".replace("{store_id}", ApiClient.urlEncode(storeId.toString())); - - localVarRequestBuilder.uri(URI.create(configuration.getApiUrl() + localVarPath)); - - localVarRequestBuilder.header("Accept", "application/json"); - - if (configuration.getCredentials().getCredentialsMethod() != CredentialsMethod.NONE) { - String accessToken = getAccessToken(configuration); - localVarRequestBuilder.header("Authorization", "Bearer " + accessToken); - } - - if (configuration.getUserAgent() != null) { - localVarRequestBuilder.header("User-Agent", configuration.getUserAgent()); - } - - if (configuration.getDefaultHeaders() != null) { - configuration.getDefaultHeaders().forEach(localVarRequestBuilder::header); - } - - localVarRequestBuilder.method("DELETE", HttpRequest.BodyPublishers.noBody()); - Duration readTimeout = configuration.getReadTimeout(); - if (readTimeout != null) { - localVarRequestBuilder.timeout(readTimeout); - } - if (apiClient.getRequestInterceptor() != null) { - apiClient.getRequestInterceptor().accept(localVarRequestBuilder); - } - return localVarRequestBuilder; - } - /** * Expand all relationships in userset tree format, and following userset rewrite rules. Useful to reason about and debug a certain relationship * The Expand API will return all users and usersets that have certain relationship with an object in a certain store. This is different from the `/stores/{store_id}/read` API in that both users and computed usersets are returned. Body parameters `tuple_key.object` and `tuple_key.relation` are all required. The response will return a tree whose leaves are the specific users and usersets. Union, intersection and difference operator are located in the intermediate nodes. ## Example To expand all users that have the `reader` relationship with object `document:2021-budget`, use the Expand API with the following request body ```json { \"tuple_key\": { \"object\": \"document:2021-budget\", \"relation\": \"reader\" }, \"authorization_model_id\": \"01G50QVV17PECNVAHX1GG4Y5NC\" } ``` OpenFGA's response will be a userset tree of the users and usersets that have read access to the document. ```json { \"tree\":{ \"root\":{ \"type\":\"document:2021-budget#reader\", \"union\":{ \"nodes\":[ { \"type\":\"document:2021-budget#reader\", \"leaf\":{ \"users\":{ \"users\":[ \"user:bob\" ] } } }, { \"type\":\"document:2021-budget#reader\", \"leaf\":{ \"computed\":{ \"userset\":\"document:2021-budget#writer\" } } } ] } } } } ``` The caller can then call expand API for the `writer` relationship for the `document:2021-budget`. @@ -365,67 +239,20 @@ public CompletableFuture> expand( private CompletableFuture> expand( String storeId, ExpandRequest body, Configuration configuration) throws ApiException, FgaInvalidParameterException { - try { - HttpRequest request = - expandRequestBuilder(storeId, body, configuration).build(); - return new HttpRequestAttempt<>(request, "expand", ExpandResponse.class, apiClient, configuration) - .attemptHttpRequest(); - } catch (ApiException e) { - return CompletableFuture.failedFuture(e); - } - } - - private HttpRequest.Builder expandRequestBuilder(String storeId, ExpandRequest body, Configuration configuration) - throws ApiException, FgaInvalidParameterException { - // verify the required parameter 'storeId' is set - if (storeId == null) { - throw new ApiException(400, "Missing the required parameter 'storeId' when calling expand"); - } - // verify the required parameter 'body' is set - if (body == null) { - throw new ApiException(400, "Missing the required parameter 'body' when calling expand"); - } - - // verify the Configuration is valid - configuration.assertValid(); - - HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); - - String localVarPath = - "/stores/{store_id}/expand".replace("{store_id}", ApiClient.urlEncode(storeId.toString())); - - localVarRequestBuilder.uri(URI.create(configuration.getApiUrl() + localVarPath)); - localVarRequestBuilder.header("Content-Type", "application/json"); - localVarRequestBuilder.header("Accept", "application/json"); + validate(storeId, "storeId", "expand"); - if (configuration.getCredentials().getCredentialsMethod() != CredentialsMethod.NONE) { - String accessToken = getAccessToken(configuration); - localVarRequestBuilder.header("Authorization", "Bearer " + accessToken); - } - - if (configuration.getUserAgent() != null) { - localVarRequestBuilder.header("User-Agent", configuration.getUserAgent()); - } + validate(body, "body", "expand"); - if (configuration.getDefaultHeaders() != null) { - configuration.getDefaultHeaders().forEach(localVarRequestBuilder::header); - } + String path = "/stores/{store_id}/expand".replace("{store_id}", ApiClient.urlEncode(storeId.toString())); try { - byte[] localVarPostBody = apiClient.getObjectMapper().writeValueAsBytes(body); - localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody)); - } catch (IOException e) { - throw new ApiException(e); - } - Duration readTimeout = configuration.getReadTimeout(); - if (readTimeout != null) { - localVarRequestBuilder.timeout(readTimeout); - } - if (apiClient.getRequestInterceptor() != null) { - apiClient.getRequestInterceptor().accept(localVarRequestBuilder); + HttpRequest request = buildHttpRequest("POST", path, body, configuration); + return new HttpRequestAttempt<>(request, "expand", ExpandResponse.class, apiClient, configuration) + .attemptHttpRequest(); + } catch (ApiException e) { + return CompletableFuture.failedFuture(e); } - return localVarRequestBuilder; } /** @@ -456,8 +283,13 @@ public CompletableFuture> getStore( private CompletableFuture> getStore(String storeId, Configuration configuration) throws ApiException, FgaInvalidParameterException { + + validate(storeId, "storeId", "getStore"); + + String path = "/stores/{store_id}".replace("{store_id}", ApiClient.urlEncode(storeId.toString())); + try { - HttpRequest request = getStoreRequestBuilder(storeId, configuration).build(); + HttpRequest request = buildHttpRequest("GET", path, configuration); return new HttpRequestAttempt<>(request, "getStore", GetStoreResponse.class, apiClient, configuration) .attemptHttpRequest(); } catch (ApiException e) { @@ -465,48 +297,6 @@ private CompletableFuture> getStore(String storeId } } - private HttpRequest.Builder getStoreRequestBuilder(String storeId, Configuration configuration) - throws ApiException, FgaInvalidParameterException { - // verify the required parameter 'storeId' is set - if (storeId == null) { - throw new ApiException(400, "Missing the required parameter 'storeId' when calling getStore"); - } - - // verify the Configuration is valid - configuration.assertValid(); - - HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); - - String localVarPath = "/stores/{store_id}".replace("{store_id}", ApiClient.urlEncode(storeId.toString())); - - localVarRequestBuilder.uri(URI.create(configuration.getApiUrl() + localVarPath)); - - localVarRequestBuilder.header("Accept", "application/json"); - - if (configuration.getCredentials().getCredentialsMethod() != CredentialsMethod.NONE) { - String accessToken = getAccessToken(configuration); - localVarRequestBuilder.header("Authorization", "Bearer " + accessToken); - } - - if (configuration.getUserAgent() != null) { - localVarRequestBuilder.header("User-Agent", configuration.getUserAgent()); - } - - if (configuration.getDefaultHeaders() != null) { - configuration.getDefaultHeaders().forEach(localVarRequestBuilder::header); - } - - localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody()); - Duration readTimeout = configuration.getReadTimeout(); - if (readTimeout != null) { - localVarRequestBuilder.timeout(readTimeout); - } - if (apiClient.getRequestInterceptor() != null) { - apiClient.getRequestInterceptor().accept(localVarRequestBuilder); - } - return localVarRequestBuilder; - } - /** * List all objects of the given type that the user has a relation with * The ListObjects API returns a list of all the objects of the given type that the user has a relation with. To achieve this, both the store tuples and the authorization model are used. An `authorization_model_id` may be specified in the body. If it is not specified, the latest authorization model ID will be used. It is strongly recommended to specify authorization model id for better performance. You may also specify `contextual_tuples` that will be treated as regular tuples. Each of these tuples may have an associated `condition`. You may also provide a `context` object that will be used to evaluate the conditioned tuples in the system. It is strongly recommended to provide a value for all the input parameters of all the conditions, to ensure that all tuples be evaluated correctly. The response will contain the related objects in an array in the \"objects\" field of the response and they will be strings in the object format `<type>:<id>` (e.g. \"document:roadmap\"). The number of objects in the response array will be limited by the execution timeout specified in the flag OPENFGA_LIST_OBJECTS_DEADLINE and by the upper bound specified in the flag OPENFGA_LIST_OBJECTS_MAX_RESULTS, whichever is hit first. The objects given will not be sorted, and therefore two identical calls can give a given different set of objects. @@ -538,68 +328,20 @@ public CompletableFuture> listObjects( private CompletableFuture> listObjects( String storeId, ListObjectsRequest body, Configuration configuration) throws ApiException, FgaInvalidParameterException { - try { - HttpRequest request = - listObjectsRequestBuilder(storeId, body, configuration).build(); - return new HttpRequestAttempt<>(request, "listObjects", ListObjectsResponse.class, apiClient, configuration) - .attemptHttpRequest(); - } catch (ApiException e) { - return CompletableFuture.failedFuture(e); - } - } - - private HttpRequest.Builder listObjectsRequestBuilder( - String storeId, ListObjectsRequest body, Configuration configuration) - throws ApiException, FgaInvalidParameterException { - // verify the required parameter 'storeId' is set - if (storeId == null) { - throw new ApiException(400, "Missing the required parameter 'storeId' when calling listObjects"); - } - // verify the required parameter 'body' is set - if (body == null) { - throw new ApiException(400, "Missing the required parameter 'body' when calling listObjects"); - } - - // verify the Configuration is valid - configuration.assertValid(); - - HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); - - String localVarPath = - "/stores/{store_id}/list-objects".replace("{store_id}", ApiClient.urlEncode(storeId.toString())); - - localVarRequestBuilder.uri(URI.create(configuration.getApiUrl() + localVarPath)); - localVarRequestBuilder.header("Content-Type", "application/json"); - localVarRequestBuilder.header("Accept", "application/json"); - - if (configuration.getCredentials().getCredentialsMethod() != CredentialsMethod.NONE) { - String accessToken = getAccessToken(configuration); - localVarRequestBuilder.header("Authorization", "Bearer " + accessToken); - } + validate(storeId, "storeId", "listObjects"); - if (configuration.getUserAgent() != null) { - localVarRequestBuilder.header("User-Agent", configuration.getUserAgent()); - } + validate(body, "body", "listObjects"); - if (configuration.getDefaultHeaders() != null) { - configuration.getDefaultHeaders().forEach(localVarRequestBuilder::header); - } + String path = "/stores/{store_id}/list-objects".replace("{store_id}", ApiClient.urlEncode(storeId.toString())); try { - byte[] localVarPostBody = apiClient.getObjectMapper().writeValueAsBytes(body); - localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody)); - } catch (IOException e) { - throw new ApiException(e); - } - Duration readTimeout = configuration.getReadTimeout(); - if (readTimeout != null) { - localVarRequestBuilder.timeout(readTimeout); - } - if (apiClient.getRequestInterceptor() != null) { - apiClient.getRequestInterceptor().accept(localVarRequestBuilder); + HttpRequest request = buildHttpRequest("POST", path, body, configuration); + return new HttpRequestAttempt<>(request, "listObjects", ListObjectsResponse.class, apiClient, configuration) + .attemptHttpRequest(); + } catch (ApiException e) { + return CompletableFuture.failedFuture(e); } - return localVarRequestBuilder; } /** @@ -633,9 +375,12 @@ public CompletableFuture> listStores( private CompletableFuture> listStores( Integer pageSize, String continuationToken, Configuration configuration) throws ApiException, FgaInvalidParameterException { + + String path = "/stores"; + path = pathWithParams(path, "page_size", pageSize, "continuation_token", continuationToken); + try { - HttpRequest request = listStoresRequestBuilder(pageSize, continuationToken, configuration) - .build(); + HttpRequest request = buildHttpRequest("GET", path, configuration); return new HttpRequestAttempt<>(request, "listStores", ListStoresResponse.class, apiClient, configuration) .attemptHttpRequest(); } catch (ApiException e) { @@ -643,63 +388,6 @@ private CompletableFuture> listStores( } } - private HttpRequest.Builder listStoresRequestBuilder( - Integer pageSize, String continuationToken, Configuration configuration) - throws ApiException, FgaInvalidParameterException { - - // verify the Configuration is valid - configuration.assertValid(); - - HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); - - String localVarPath = "/stores"; - - List localVarQueryParams = new ArrayList<>(); - StringJoiner localVarQueryStringJoiner = new StringJoiner("&"); - String localVarQueryParameterBaseName; - localVarQueryParameterBaseName = "page_size"; - localVarQueryParams.addAll(ApiClient.parameterToPairs("page_size", pageSize)); - localVarQueryParameterBaseName = "continuation_token"; - localVarQueryParams.addAll(ApiClient.parameterToPairs("continuation_token", continuationToken)); - - if (!localVarQueryParams.isEmpty() || localVarQueryStringJoiner.length() != 0) { - StringJoiner queryJoiner = new StringJoiner("&"); - localVarQueryParams.forEach(p -> queryJoiner.add(p.getName() + '=' + p.getValue())); - if (localVarQueryStringJoiner.length() != 0) { - queryJoiner.add(localVarQueryStringJoiner.toString()); - } - localVarRequestBuilder.uri( - URI.create(configuration.getApiUrl() + localVarPath + '?' + queryJoiner.toString())); - } else { - localVarRequestBuilder.uri(URI.create(configuration.getApiUrl() + localVarPath)); - } - - localVarRequestBuilder.header("Accept", "application/json"); - - if (configuration.getCredentials().getCredentialsMethod() != CredentialsMethod.NONE) { - String accessToken = getAccessToken(configuration); - localVarRequestBuilder.header("Authorization", "Bearer " + accessToken); - } - - if (configuration.getUserAgent() != null) { - localVarRequestBuilder.header("User-Agent", configuration.getUserAgent()); - } - - if (configuration.getDefaultHeaders() != null) { - configuration.getDefaultHeaders().forEach(localVarRequestBuilder::header); - } - - localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody()); - Duration readTimeout = configuration.getReadTimeout(); - if (readTimeout != null) { - localVarRequestBuilder.timeout(readTimeout); - } - if (apiClient.getRequestInterceptor() != null) { - apiClient.getRequestInterceptor().accept(localVarRequestBuilder); - } - return localVarRequestBuilder; - } - /** * Get tuples from the store that matches a query, without following userset rewrite rules * The Read API will return the tuples for a certain store that match a query filter specified in the body of the request. It is different from the `/stores/{store_id}/expand` API in that it only returns relationship tuples that are stored in the system and satisfy the query. In the body: 1. `tuple_key` is optional. If not specified, it will return all tuples in the store. 2. `tuple_key.object` is mandatory if `tuple_key` is specified. It can be a full object (e.g., `type:object_id`) or type only (e.g., `type:`). 3. `tuple_key.user` is mandatory if tuple_key is specified in the case the `tuple_key.object` is a type only. ## Examples ### Query for all objects in a type definition To query for all objects that `user:bob` has `reader` relationship in the `document` type definition, call read API with body of ```json { \"tuple_key\": { \"user\": \"user:bob\", \"relation\": \"reader\", \"object\": \"document:\" } } ``` The API will return tuples and a continuation token, something like ```json { \"tuples\": [ { \"key\": { \"user\": \"user:bob\", \"relation\": \"reader\", \"object\": \"document:2021-budget\" }, \"timestamp\": \"2021-10-06T15:32:11.128Z\" } ], \"continuation_token\": \"eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ==\" } ``` This means that `user:bob` has a `reader` relationship with 1 document `document:2021-budget`. Note that this API, unlike the List Objects API, does not evaluate the tuples in the store. The continuation token will be empty if there are no more tuples to query. ### Query for all stored relationship tuples that have a particular relation and object To query for all users that have `reader` relationship with `document:2021-budget`, call read API with body of ```json { \"tuple_key\": { \"object\": \"document:2021-budget\", \"relation\": \"reader\" } } ``` The API will return something like ```json { \"tuples\": [ { \"key\": { \"user\": \"user:bob\", \"relation\": \"reader\", \"object\": \"document:2021-budget\" }, \"timestamp\": \"2021-10-06T15:32:11.128Z\" } ], \"continuation_token\": \"eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ==\" } ``` This means that `document:2021-budget` has 1 `reader` (`user:bob`). Note that, even if the model said that all `writers` are also `readers`, the API will not return writers such as `user:anne` because it only returns tuples and does not evaluate them. ### Query for all users with all relationships for a particular document To query for all users that have any relationship with `document:2021-budget`, call read API with body of ```json { \"tuple_key\": { \"object\": \"document:2021-budget\" } } ``` The API will return something like ```json { \"tuples\": [ { \"key\": { \"user\": \"user:anne\", \"relation\": \"writer\", \"object\": \"document:2021-budget\" }, \"timestamp\": \"2021-10-05T13:42:12.356Z\" }, { \"key\": { \"user\": \"user:bob\", \"relation\": \"reader\", \"object\": \"document:2021-budget\" }, \"timestamp\": \"2021-10-06T15:32:11.128Z\" } ], \"continuation_token\": \"eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ==\" } ``` This means that `document:2021-budget` has 1 `reader` (`user:bob`) and 1 `writer` (`user:anne`). @@ -731,9 +419,15 @@ public CompletableFuture> read( private CompletableFuture> read( String storeId, ReadRequest body, Configuration configuration) throws ApiException, FgaInvalidParameterException { + + validate(storeId, "storeId", "read"); + + validate(body, "body", "read"); + + String path = "/stores/{store_id}/read".replace("{store_id}", ApiClient.urlEncode(storeId.toString())); + try { - HttpRequest request = - readRequestBuilder(storeId, body, configuration).build(); + HttpRequest request = buildHttpRequest("POST", path, body, configuration); return new HttpRequestAttempt<>(request, "read", ReadResponse.class, apiClient, configuration) .attemptHttpRequest(); } catch (ApiException e) { @@ -741,58 +435,6 @@ private CompletableFuture> read( } } - private HttpRequest.Builder readRequestBuilder(String storeId, ReadRequest body, Configuration configuration) - throws ApiException, FgaInvalidParameterException { - // verify the required parameter 'storeId' is set - if (storeId == null) { - throw new ApiException(400, "Missing the required parameter 'storeId' when calling read"); - } - // verify the required parameter 'body' is set - if (body == null) { - throw new ApiException(400, "Missing the required parameter 'body' when calling read"); - } - - // verify the Configuration is valid - configuration.assertValid(); - - HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); - - String localVarPath = "/stores/{store_id}/read".replace("{store_id}", ApiClient.urlEncode(storeId.toString())); - - localVarRequestBuilder.uri(URI.create(configuration.getApiUrl() + localVarPath)); - - localVarRequestBuilder.header("Content-Type", "application/json"); - localVarRequestBuilder.header("Accept", "application/json"); - - if (configuration.getCredentials().getCredentialsMethod() != CredentialsMethod.NONE) { - String accessToken = getAccessToken(configuration); - localVarRequestBuilder.header("Authorization", "Bearer " + accessToken); - } - - if (configuration.getUserAgent() != null) { - localVarRequestBuilder.header("User-Agent", configuration.getUserAgent()); - } - - if (configuration.getDefaultHeaders() != null) { - configuration.getDefaultHeaders().forEach(localVarRequestBuilder::header); - } - - try { - byte[] localVarPostBody = apiClient.getObjectMapper().writeValueAsBytes(body); - localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody)); - } catch (IOException e) { - throw new ApiException(e); - } - Duration readTimeout = configuration.getReadTimeout(); - if (readTimeout != null) { - localVarRequestBuilder.timeout(readTimeout); - } - if (apiClient.getRequestInterceptor() != null) { - apiClient.getRequestInterceptor().accept(localVarRequestBuilder); - } - return localVarRequestBuilder; - } - /** * Read assertions for an authorization model ID * The ReadAssertions API will return, for a given authorization model id, all the assertions stored for it. An assertion is an object that contains a tuple key, and the expectation of whether a call to the Check API of that tuple key will return true or false. @@ -824,65 +466,23 @@ public CompletableFuture> readAssertions( private CompletableFuture> readAssertions( String storeId, String authorizationModelId, Configuration configuration) throws ApiException, FgaInvalidParameterException { - try { - HttpRequest request = readAssertionsRequestBuilder(storeId, authorizationModelId, configuration) - .build(); - return new HttpRequestAttempt<>( - request, "readAssertions", ReadAssertionsResponse.class, apiClient, configuration) - .attemptHttpRequest(); - } catch (ApiException e) { - return CompletableFuture.failedFuture(e); - } - } - - private HttpRequest.Builder readAssertionsRequestBuilder( - String storeId, String authorizationModelId, Configuration configuration) - throws ApiException, FgaInvalidParameterException { - // verify the required parameter 'storeId' is set - if (storeId == null) { - throw new ApiException(400, "Missing the required parameter 'storeId' when calling readAssertions"); - } - // verify the required parameter 'authorizationModelId' is set - if (authorizationModelId == null) { - throw new ApiException( - 400, "Missing the required parameter 'authorizationModelId' when calling readAssertions"); - } - // verify the Configuration is valid - configuration.assertValid(); + validate(storeId, "storeId", "readAssertions"); - HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); + validate(authorizationModelId, "authorizationModelId", "readAssertions"); - String localVarPath = "/stores/{store_id}/assertions/{authorization_model_id}" + String path = "/stores/{store_id}/assertions/{authorization_model_id}" .replace("{store_id}", ApiClient.urlEncode(storeId.toString())) .replace("{authorization_model_id}", ApiClient.urlEncode(authorizationModelId.toString())); - localVarRequestBuilder.uri(URI.create(configuration.getApiUrl() + localVarPath)); - - localVarRequestBuilder.header("Accept", "application/json"); - - if (configuration.getCredentials().getCredentialsMethod() != CredentialsMethod.NONE) { - String accessToken = getAccessToken(configuration); - localVarRequestBuilder.header("Authorization", "Bearer " + accessToken); - } - - if (configuration.getUserAgent() != null) { - localVarRequestBuilder.header("User-Agent", configuration.getUserAgent()); - } - - if (configuration.getDefaultHeaders() != null) { - configuration.getDefaultHeaders().forEach(localVarRequestBuilder::header); - } - - localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody()); - Duration readTimeout = configuration.getReadTimeout(); - if (readTimeout != null) { - localVarRequestBuilder.timeout(readTimeout); - } - if (apiClient.getRequestInterceptor() != null) { - apiClient.getRequestInterceptor().accept(localVarRequestBuilder); + try { + HttpRequest request = buildHttpRequest("GET", path, configuration); + return new HttpRequestAttempt<>( + request, "readAssertions", ReadAssertionsResponse.class, apiClient, configuration) + .attemptHttpRequest(); + } catch (ApiException e) { + return CompletableFuture.failedFuture(e); } - return localVarRequestBuilder; } /** @@ -915,9 +515,17 @@ public CompletableFuture> readAuthor private CompletableFuture> readAuthorizationModel( String storeId, String id, Configuration configuration) throws ApiException, FgaInvalidParameterException { + + validate(storeId, "storeId", "readAuthorizationModel"); + + validate(id, "id", "readAuthorizationModel"); + + String path = "/stores/{store_id}/authorization-models/{id}" + .replace("{store_id}", ApiClient.urlEncode(storeId.toString())) + .replace("{id}", ApiClient.urlEncode(id.toString())); + try { - HttpRequest request = readAuthorizationModelRequestBuilder(storeId, id, configuration) - .build(); + HttpRequest request = buildHttpRequest("GET", path, configuration); return new HttpRequestAttempt<>( request, "readAuthorizationModel", @@ -930,54 +538,6 @@ private CompletableFuture> readAutho } } - private HttpRequest.Builder readAuthorizationModelRequestBuilder( - String storeId, String id, Configuration configuration) throws ApiException, FgaInvalidParameterException { - // verify the required parameter 'storeId' is set - if (storeId == null) { - throw new ApiException(400, "Missing the required parameter 'storeId' when calling readAuthorizationModel"); - } - // verify the required parameter 'id' is set - if (id == null) { - throw new ApiException(400, "Missing the required parameter 'id' when calling readAuthorizationModel"); - } - - // verify the Configuration is valid - configuration.assertValid(); - - HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); - - String localVarPath = "/stores/{store_id}/authorization-models/{id}" - .replace("{store_id}", ApiClient.urlEncode(storeId.toString())) - .replace("{id}", ApiClient.urlEncode(id.toString())); - - localVarRequestBuilder.uri(URI.create(configuration.getApiUrl() + localVarPath)); - - localVarRequestBuilder.header("Accept", "application/json"); - - if (configuration.getCredentials().getCredentialsMethod() != CredentialsMethod.NONE) { - String accessToken = getAccessToken(configuration); - localVarRequestBuilder.header("Authorization", "Bearer " + accessToken); - } - - if (configuration.getUserAgent() != null) { - localVarRequestBuilder.header("User-Agent", configuration.getUserAgent()); - } - - if (configuration.getDefaultHeaders() != null) { - configuration.getDefaultHeaders().forEach(localVarRequestBuilder::header); - } - - localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody()); - Duration readTimeout = configuration.getReadTimeout(); - if (readTimeout != null) { - localVarRequestBuilder.timeout(readTimeout); - } - if (apiClient.getRequestInterceptor() != null) { - apiClient.getRequestInterceptor().accept(localVarRequestBuilder); - } - return localVarRequestBuilder; - } - /** * Return all the authorization models for a particular store * The ReadAuthorizationModels API will return all the authorization models for a certain store. OpenFGA's response will contain an array of all authorization models, sorted in descending order of creation. ## Example Assume that a store's authorization model has been configured twice. To get all the authorization models that have been created in this store, call GET authorization-models. The API will return a response that looks like: ```json { \"authorization_models\": [ { \"id\": \"01G50QVV17PECNVAHX1GG4Y5NC\", \"type_definitions\": [...] }, { \"id\": \"01G4ZW8F4A07AKQ8RHSVG9RW04\", \"type_definitions\": [...] }, ], \"continuation_token\": \"eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ==\" } ``` If there are no more authorization models available, the `continuation_token` field will be empty ```json { \"authorization_models\": [ { \"id\": \"01G50QVV17PECNVAHX1GG4Y5NC\", \"type_definitions\": [...] }, { \"id\": \"01G4ZW8F4A07AKQ8RHSVG9RW04\", \"type_definitions\": [...] }, ], \"continuation_token\": \"\" } ``` @@ -1013,10 +573,15 @@ public CompletableFuture> readAutho private CompletableFuture> readAuthorizationModels( String storeId, Integer pageSize, String continuationToken, Configuration configuration) throws ApiException, FgaInvalidParameterException { + + validate(storeId, "storeId", "readAuthorizationModels"); + + String path = "/stores/{store_id}/authorization-models" + .replace("{store_id}", ApiClient.urlEncode(storeId.toString())); + path = pathWithParams(path, "page_size", pageSize, "continuation_token", continuationToken); + try { - HttpRequest request = readAuthorizationModelsRequestBuilder( - storeId, pageSize, continuationToken, configuration) - .build(); + HttpRequest request = buildHttpRequest("GET", path, configuration); return new HttpRequestAttempt<>( request, "readAuthorizationModels", @@ -1029,69 +594,6 @@ private CompletableFuture> readAuth } } - private HttpRequest.Builder readAuthorizationModelsRequestBuilder( - String storeId, Integer pageSize, String continuationToken, Configuration configuration) - throws ApiException, FgaInvalidParameterException { - // verify the required parameter 'storeId' is set - if (storeId == null) { - throw new ApiException( - 400, "Missing the required parameter 'storeId' when calling readAuthorizationModels"); - } - - // verify the Configuration is valid - configuration.assertValid(); - - HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); - - String localVarPath = "/stores/{store_id}/authorization-models" - .replace("{store_id}", ApiClient.urlEncode(storeId.toString())); - - List localVarQueryParams = new ArrayList<>(); - StringJoiner localVarQueryStringJoiner = new StringJoiner("&"); - String localVarQueryParameterBaseName; - localVarQueryParameterBaseName = "page_size"; - localVarQueryParams.addAll(ApiClient.parameterToPairs("page_size", pageSize)); - localVarQueryParameterBaseName = "continuation_token"; - localVarQueryParams.addAll(ApiClient.parameterToPairs("continuation_token", continuationToken)); - - if (!localVarQueryParams.isEmpty() || localVarQueryStringJoiner.length() != 0) { - StringJoiner queryJoiner = new StringJoiner("&"); - localVarQueryParams.forEach(p -> queryJoiner.add(p.getName() + '=' + p.getValue())); - if (localVarQueryStringJoiner.length() != 0) { - queryJoiner.add(localVarQueryStringJoiner.toString()); - } - localVarRequestBuilder.uri( - URI.create(configuration.getApiUrl() + localVarPath + '?' + queryJoiner.toString())); - } else { - localVarRequestBuilder.uri(URI.create(configuration.getApiUrl() + localVarPath)); - } - - localVarRequestBuilder.header("Accept", "application/json"); - - if (configuration.getCredentials().getCredentialsMethod() != CredentialsMethod.NONE) { - String accessToken = getAccessToken(configuration); - localVarRequestBuilder.header("Authorization", "Bearer " + accessToken); - } - - if (configuration.getUserAgent() != null) { - localVarRequestBuilder.header("User-Agent", configuration.getUserAgent()); - } - - if (configuration.getDefaultHeaders() != null) { - configuration.getDefaultHeaders().forEach(localVarRequestBuilder::header); - } - - localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody()); - Duration readTimeout = configuration.getReadTimeout(); - if (readTimeout != null) { - localVarRequestBuilder.timeout(readTimeout); - } - if (apiClient.getRequestInterceptor() != null) { - apiClient.getRequestInterceptor().accept(localVarRequestBuilder); - } - return localVarRequestBuilder; - } - /** * Return a list of all the tuple changes * The ReadChanges API will return a paginated list of tuple changes (additions and deletions) that occurred in a given store, sorted by ascending time. The response will include a continuation token that is used to get the next set of changes. If there are no changes after the provided continuation token, the same token will be returned in order for it to be used when new changes are recorded. If the store never had any tuples added or removed, this token will be empty. You can use the `type` parameter to only get the list of tuple changes that affect objects of that type. When reading a write tuple change, if it was conditioned, the condition will be returned. When reading a delete tuple change, the condition will NOT be returned regardless of whether it was originally conditioned or not. @@ -1133,9 +635,14 @@ public CompletableFuture> readChanges( private CompletableFuture> readChanges( String storeId, String type, Integer pageSize, String continuationToken, Configuration configuration) throws ApiException, FgaInvalidParameterException { + + validate(storeId, "storeId", "readChanges"); + + String path = "/stores/{store_id}/changes".replace("{store_id}", ApiClient.urlEncode(storeId.toString())); + path = pathWithParams(path, "type", type, "page_size", pageSize, "continuation_token", continuationToken); + try { - HttpRequest request = readChangesRequestBuilder(storeId, type, pageSize, continuationToken, configuration) - .build(); + HttpRequest request = buildHttpRequest("GET", path, configuration); return new HttpRequestAttempt<>(request, "readChanges", ReadChangesResponse.class, apiClient, configuration) .attemptHttpRequest(); } catch (ApiException e) { @@ -1143,70 +650,6 @@ private CompletableFuture> readChanges( } } - private HttpRequest.Builder readChangesRequestBuilder( - String storeId, String type, Integer pageSize, String continuationToken, Configuration configuration) - throws ApiException, FgaInvalidParameterException { - // verify the required parameter 'storeId' is set - if (storeId == null) { - throw new ApiException(400, "Missing the required parameter 'storeId' when calling readChanges"); - } - - // verify the Configuration is valid - configuration.assertValid(); - - HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); - - String localVarPath = - "/stores/{store_id}/changes".replace("{store_id}", ApiClient.urlEncode(storeId.toString())); - - List localVarQueryParams = new ArrayList<>(); - StringJoiner localVarQueryStringJoiner = new StringJoiner("&"); - String localVarQueryParameterBaseName; - localVarQueryParameterBaseName = "type"; - localVarQueryParams.addAll(ApiClient.parameterToPairs("type", type)); - localVarQueryParameterBaseName = "page_size"; - localVarQueryParams.addAll(ApiClient.parameterToPairs("page_size", pageSize)); - localVarQueryParameterBaseName = "continuation_token"; - localVarQueryParams.addAll(ApiClient.parameterToPairs("continuation_token", continuationToken)); - - if (!localVarQueryParams.isEmpty() || localVarQueryStringJoiner.length() != 0) { - StringJoiner queryJoiner = new StringJoiner("&"); - localVarQueryParams.forEach(p -> queryJoiner.add(p.getName() + '=' + p.getValue())); - if (localVarQueryStringJoiner.length() != 0) { - queryJoiner.add(localVarQueryStringJoiner.toString()); - } - localVarRequestBuilder.uri( - URI.create(configuration.getApiUrl() + localVarPath + '?' + queryJoiner.toString())); - } else { - localVarRequestBuilder.uri(URI.create(configuration.getApiUrl() + localVarPath)); - } - - localVarRequestBuilder.header("Accept", "application/json"); - - if (configuration.getCredentials().getCredentialsMethod() != CredentialsMethod.NONE) { - String accessToken = getAccessToken(configuration); - localVarRequestBuilder.header("Authorization", "Bearer " + accessToken); - } - - if (configuration.getUserAgent() != null) { - localVarRequestBuilder.header("User-Agent", configuration.getUserAgent()); - } - - if (configuration.getDefaultHeaders() != null) { - configuration.getDefaultHeaders().forEach(localVarRequestBuilder::header); - } - - localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody()); - Duration readTimeout = configuration.getReadTimeout(); - if (readTimeout != null) { - localVarRequestBuilder.timeout(readTimeout); - } - if (apiClient.getRequestInterceptor() != null) { - apiClient.getRequestInterceptor().accept(localVarRequestBuilder); - } - return localVarRequestBuilder; - } - /** * Add or delete tuples from the store * The Write API will update the tuples for a certain store. Tuples and type definitions allow OpenFGA to determine whether a relationship exists between an object and an user. In the body, `writes` adds new tuples and `deletes` removes existing tuples. When deleting a tuple, any `condition` specified with it is ignored. The API is not idempotent: if, later on, you try to add the same tuple key (even if the `condition` is different), or if you try to delete a non-existing tuple, it will throw an error. An `authorization_model_id` may be specified in the body. If it is, it will be used to assert that each written tuple (not deleted) is valid for the model specified. If it is not specified, the latest authorization model ID will be used. ## Example ### Adding relationships To add `user:anne` as a `writer` for `document:2021-budget`, call write API with the following ```json { \"writes\": { \"tuple_keys\": [ { \"user\": \"user:anne\", \"relation\": \"writer\", \"object\": \"document:2021-budget\" } ] }, \"authorization_model_id\": \"01G50QVV17PECNVAHX1GG4Y5NC\" } ``` ### Removing relationships To remove `user:bob` as a `reader` for `document:2021-budget`, call write API with the following ```json { \"deletes\": { \"tuple_keys\": [ { \"user\": \"user:bob\", \"relation\": \"reader\", \"object\": \"document:2021-budget\" } ] } } ``` @@ -1237,66 +680,20 @@ public CompletableFuture> write( private CompletableFuture> write(String storeId, WriteRequest body, Configuration configuration) throws ApiException, FgaInvalidParameterException { - try { - HttpRequest request = - writeRequestBuilder(storeId, body, configuration).build(); - return new HttpRequestAttempt<>(request, "write", Object.class, apiClient, configuration) - .attemptHttpRequest(); - } catch (ApiException e) { - return CompletableFuture.failedFuture(e); - } - } - - private HttpRequest.Builder writeRequestBuilder(String storeId, WriteRequest body, Configuration configuration) - throws ApiException, FgaInvalidParameterException { - // verify the required parameter 'storeId' is set - if (storeId == null) { - throw new ApiException(400, "Missing the required parameter 'storeId' when calling write"); - } - // verify the required parameter 'body' is set - if (body == null) { - throw new ApiException(400, "Missing the required parameter 'body' when calling write"); - } - - // verify the Configuration is valid - configuration.assertValid(); - - HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); - - String localVarPath = "/stores/{store_id}/write".replace("{store_id}", ApiClient.urlEncode(storeId.toString())); - localVarRequestBuilder.uri(URI.create(configuration.getApiUrl() + localVarPath)); + validate(storeId, "storeId", "write"); - localVarRequestBuilder.header("Content-Type", "application/json"); - localVarRequestBuilder.header("Accept", "application/json"); + validate(body, "body", "write"); - if (configuration.getCredentials().getCredentialsMethod() != CredentialsMethod.NONE) { - String accessToken = getAccessToken(configuration); - localVarRequestBuilder.header("Authorization", "Bearer " + accessToken); - } - - if (configuration.getUserAgent() != null) { - localVarRequestBuilder.header("User-Agent", configuration.getUserAgent()); - } - - if (configuration.getDefaultHeaders() != null) { - configuration.getDefaultHeaders().forEach(localVarRequestBuilder::header); - } + String path = "/stores/{store_id}/write".replace("{store_id}", ApiClient.urlEncode(storeId.toString())); try { - byte[] localVarPostBody = apiClient.getObjectMapper().writeValueAsBytes(body); - localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody)); - } catch (IOException e) { - throw new ApiException(e); - } - Duration readTimeout = configuration.getReadTimeout(); - if (readTimeout != null) { - localVarRequestBuilder.timeout(readTimeout); - } - if (apiClient.getRequestInterceptor() != null) { - apiClient.getRequestInterceptor().accept(localVarRequestBuilder); + HttpRequest request = buildHttpRequest("POST", path, body, configuration); + return new HttpRequestAttempt<>(request, "write", Object.class, apiClient, configuration) + .attemptHttpRequest(); + } catch (ApiException e) { + return CompletableFuture.failedFuture(e); } - return localVarRequestBuilder; } /** @@ -1336,74 +733,24 @@ public CompletableFuture> writeAssertions( private CompletableFuture> writeAssertions( String storeId, String authorizationModelId, WriteAssertionsRequest body, Configuration configuration) throws ApiException, FgaInvalidParameterException { - try { - HttpRequest request = writeAssertionsRequestBuilder(storeId, authorizationModelId, body, configuration) - .build(); - return new HttpRequestAttempt<>(request, "writeAssertions", Void.class, apiClient, configuration) - .attemptHttpRequest(); - } catch (ApiException e) { - return CompletableFuture.failedFuture(e); - } - } - private HttpRequest.Builder writeAssertionsRequestBuilder( - String storeId, String authorizationModelId, WriteAssertionsRequest body, Configuration configuration) - throws ApiException, FgaInvalidParameterException { - // verify the required parameter 'storeId' is set - if (storeId == null) { - throw new ApiException(400, "Missing the required parameter 'storeId' when calling writeAssertions"); - } - // verify the required parameter 'authorizationModelId' is set - if (authorizationModelId == null) { - throw new ApiException( - 400, "Missing the required parameter 'authorizationModelId' when calling writeAssertions"); - } - // verify the required parameter 'body' is set - if (body == null) { - throw new ApiException(400, "Missing the required parameter 'body' when calling writeAssertions"); - } + validate(storeId, "storeId", "writeAssertions"); - // verify the Configuration is valid - configuration.assertValid(); + validate(authorizationModelId, "authorizationModelId", "writeAssertions"); - HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); + validate(body, "body", "writeAssertions"); - String localVarPath = "/stores/{store_id}/assertions/{authorization_model_id}" + String path = "/stores/{store_id}/assertions/{authorization_model_id}" .replace("{store_id}", ApiClient.urlEncode(storeId.toString())) .replace("{authorization_model_id}", ApiClient.urlEncode(authorizationModelId.toString())); - localVarRequestBuilder.uri(URI.create(configuration.getApiUrl() + localVarPath)); - - localVarRequestBuilder.header("Content-Type", "application/json"); - localVarRequestBuilder.header("Accept", "application/json"); - - if (configuration.getCredentials().getCredentialsMethod() != CredentialsMethod.NONE) { - String accessToken = getAccessToken(configuration); - localVarRequestBuilder.header("Authorization", "Bearer " + accessToken); - } - - if (configuration.getUserAgent() != null) { - localVarRequestBuilder.header("User-Agent", configuration.getUserAgent()); - } - - if (configuration.getDefaultHeaders() != null) { - configuration.getDefaultHeaders().forEach(localVarRequestBuilder::header); - } - try { - byte[] localVarPostBody = apiClient.getObjectMapper().writeValueAsBytes(body); - localVarRequestBuilder.method("PUT", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody)); - } catch (IOException e) { - throw new ApiException(e); - } - Duration readTimeout = configuration.getReadTimeout(); - if (readTimeout != null) { - localVarRequestBuilder.timeout(readTimeout); - } - if (apiClient.getRequestInterceptor() != null) { - apiClient.getRequestInterceptor().accept(localVarRequestBuilder); + HttpRequest request = buildHttpRequest("PUT", path, body, configuration); + return new HttpRequestAttempt<>(request, "writeAssertions", Void.class, apiClient, configuration) + .attemptHttpRequest(); + } catch (ApiException e) { + return CompletableFuture.failedFuture(e); } - return localVarRequestBuilder; } /** @@ -1437,9 +784,16 @@ public CompletableFuture> writeAuth private CompletableFuture> writeAuthorizationModel( String storeId, WriteAuthorizationModelRequest body, Configuration configuration) throws ApiException, FgaInvalidParameterException { + + validate(storeId, "storeId", "writeAuthorizationModel"); + + validate(body, "body", "writeAuthorizationModel"); + + String path = "/stores/{store_id}/authorization-models" + .replace("{store_id}", ApiClient.urlEncode(storeId.toString())); + try { - HttpRequest request = writeAuthorizationModelRequestBuilder(storeId, body, configuration) - .build(); + HttpRequest request = buildHttpRequest("POST", path, body, configuration); return new HttpRequestAttempt<>( request, "writeAuthorizationModel", @@ -1452,59 +806,83 @@ private CompletableFuture> writeAut } } - private HttpRequest.Builder writeAuthorizationModelRequestBuilder( - String storeId, WriteAuthorizationModelRequest body, Configuration configuration) + private HttpRequest buildHttpRequest(String method, String path, Configuration configuration) throws ApiException, FgaInvalidParameterException { - // verify the required parameter 'storeId' is set - if (storeId == null) { - throw new ApiException( - 400, "Missing the required parameter 'storeId' when calling writeAuthorizationModel"); - } - // verify the required parameter 'body' is set - if (body == null) { - throw new ApiException(400, "Missing the required parameter 'body' when calling writeAuthorizationModel"); + return buildHttpRequestWithPublisher(method, path, HttpRequest.BodyPublishers.noBody(), configuration); + } + + private HttpRequest buildHttpRequest(String method, String path, T body, Configuration configuration) + throws ApiException, FgaInvalidParameterException { + try { + byte[] localVarPostBody = apiClient.getObjectMapper().writeValueAsBytes(body); + var bodyPublisher = HttpRequest.BodyPublishers.ofByteArray(localVarPostBody); + return buildHttpRequestWithPublisher(method, path, bodyPublisher, configuration); + } catch (IOException e) { + throw new ApiException(e); } + } + private HttpRequest buildHttpRequestWithPublisher( + String method, String path, HttpRequest.BodyPublisher bodyPublisher, Configuration configuration) + throws ApiException, FgaInvalidParameterException { // verify the Configuration is valid configuration.assertValid(); - HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); + var httpRequest = HttpRequest.newBuilder(); - String localVarPath = "/stores/{store_id}/authorization-models" - .replace("{store_id}", ApiClient.urlEncode(storeId.toString())); + httpRequest.uri(URI.create(configuration.getApiUrl() + path)); - localVarRequestBuilder.uri(URI.create(configuration.getApiUrl() + localVarPath)); - - localVarRequestBuilder.header("Content-Type", "application/json"); - localVarRequestBuilder.header("Accept", "application/json"); + httpRequest.header("Content-Type", "application/json"); + httpRequest.header("Accept", "application/json"); if (configuration.getCredentials().getCredentialsMethod() != CredentialsMethod.NONE) { String accessToken = getAccessToken(configuration); - localVarRequestBuilder.header("Authorization", "Bearer " + accessToken); + httpRequest.header("Authorization", "Bearer " + accessToken); } if (configuration.getUserAgent() != null) { - localVarRequestBuilder.header("User-Agent", configuration.getUserAgent()); + httpRequest.header("User-Agent", configuration.getUserAgent()); } if (configuration.getDefaultHeaders() != null) { - configuration.getDefaultHeaders().forEach(localVarRequestBuilder::header); + configuration.getDefaultHeaders().forEach(httpRequest::header); } - try { - byte[] localVarPostBody = apiClient.getObjectMapper().writeValueAsBytes(body); - localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody)); - } catch (IOException e) { - throw new ApiException(e); - } + httpRequest.method(method, bodyPublisher); + Duration readTimeout = configuration.getReadTimeout(); if (readTimeout != null) { - localVarRequestBuilder.timeout(readTimeout); + httpRequest.timeout(readTimeout); } if (apiClient.getRequestInterceptor() != null) { - apiClient.getRequestInterceptor().accept(localVarRequestBuilder); + apiClient.getRequestInterceptor().accept(httpRequest); + } + + return httpRequest.build(); + } + + private String pathWithParams(String basePath, Object... params) { + var path = new StringBuilder().append(basePath); + var paramStream = Stream.builder(); + for (int i = 0; i < params.length - 1; i += 2) { + if (params[i] == null || params[i + 1] == null) { + continue; + } + Pair.of(params[i].toString(), params[i + 1].toString()) + .map(Pair::asQueryStringPair) + .ifPresent(paramStream::add); + } + String parameters = paramStream.build().collect(Collectors.joining("&")); + if (!isNullOrWhitespace(parameters)) { + path.append("?").append(parameters); + } + return path.toString(); + } + + private void validate(Object obj, String name, String context) throws FgaInvalidParameterException { + if (obj == null || obj instanceof String && isNullOrWhitespace((String) obj)) { + throw new FgaInvalidParameterException(name, context); } - return localVarRequestBuilder; } /** diff --git a/src/main/java/dev/openfga/sdk/api/client/ApiClient.java b/src/main/java/dev/openfga/sdk/api/client/ApiClient.java index 8fa1c79..d147ebc 100644 --- a/src/main/java/dev/openfga/sdk/api/client/ApiClient.java +++ b/src/main/java/dev/openfga/sdk/api/client/ApiClient.java @@ -21,7 +21,6 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import dev.openfga.sdk.api.configuration.Configuration; import dev.openfga.sdk.errors.FgaInvalidParameterException; -import dev.openfga.sdk.util.Pair; import java.io.InputStream; import java.net.URI; import java.net.URLEncoder; @@ -31,12 +30,7 @@ import java.time.Duration; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.StringJoiner; import java.util.function.Consumer; -import java.util.stream.Collectors; import org.openapitools.jackson.nullable.JsonNullableModule; /** @@ -144,79 +138,6 @@ public static String urlEncode(String s) { return URLEncoder.encode(s, UTF_8).replaceAll("\\+", "%20"); } - /** - * Convert a URL query name/value parameter to a list of encoded {@link Pair} - * objects. - * - *

The value can be null, in which case an empty list is returned.

- * - * @param name The query name parameter. - * @param value The query value, which may not be a collection but may be - * null. - * @return A singleton list of the {@link Pair} objects representing the input - * parameters, which is encoded for use in a URL. If the value is null, an - * empty list is returned. - */ - public static List parameterToPairs(String name, Object value) { - if (name == null || name.isEmpty() || value == null) { - return Collections.emptyList(); - } - return Collections.singletonList(new Pair(urlEncode(name), urlEncode(valueToString(value)))); - } - - /** - * Convert a URL query name/collection parameter to a list of encoded - * {@link Pair} objects. - * - * @param collectionFormat The swagger collectionFormat string (csv, tsv, etc). - * @param name The query name parameter. - * @param values A collection of values for the given query name, which may be - * null. - * @return A list of {@link Pair} objects representing the input parameters, - * which is encoded for use in a URL. If the values collection is null, an - * empty list is returned. - */ - public static List parameterToPairs(String collectionFormat, String name, Collection values) { - if (name == null || name.isEmpty() || values == null || values.isEmpty()) { - return Collections.emptyList(); - } - - // get the collection format (default: csv) - String format = collectionFormat == null || collectionFormat.isEmpty() ? "csv" : collectionFormat; - - // create the params based on the collection format - if ("multi".equals(format)) { - return values.stream() - .map(value -> new Pair(urlEncode(name), urlEncode(valueToString(value)))) - .collect(Collectors.toList()); - } - - String delimiter; - switch (format) { - case "csv": - delimiter = urlEncode(","); - break; - case "ssv": - delimiter = urlEncode(" "); - break; - case "tsv": - delimiter = urlEncode("\t"); - break; - case "pipes": - delimiter = urlEncode("|"); - break; - default: - throw new IllegalArgumentException("Illegal collection format: " + collectionFormat); - } - - StringJoiner joiner = new StringJoiner(delimiter); - for (Object value : values) { - joiner.add(urlEncode(valueToString(value))); - } - - return Collections.singletonList(new Pair(urlEncode(name), joiner.toString())); - } - protected ObjectMapper createDefaultObjectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); diff --git a/src/main/java/dev/openfga/sdk/util/Pair.java b/src/main/java/dev/openfga/sdk/util/Pair.java index 772ee9c..1d02263 100644 --- a/src/main/java/dev/openfga/sdk/util/Pair.java +++ b/src/main/java/dev/openfga/sdk/util/Pair.java @@ -12,6 +12,11 @@ package dev.openfga.sdk.util; +import static dev.openfga.sdk.api.client.ApiClient.urlEncode; +import static dev.openfga.sdk.util.StringUtil.isNullOrWhitespace; + +import java.util.Optional; + public class Pair { private String name = ""; private String value = ""; @@ -21,6 +26,17 @@ public Pair(String name, String value) { setValue(value); } + public static Optional of(String name, Object value) { + if (isNullOrWhitespace(name) || value == null) { + return Optional.empty(); + } + return Optional.of(new Pair(name, value.toString())); + } + + public String asQueryStringPair() { + return urlEncode(name) + "=" + urlEncode(value); + } + private void setName(String name) { if (!isValidString(name)) { return; diff --git a/src/test/java/dev/openfga/sdk/api/OpenFgaApiTest.java b/src/test/java/dev/openfga/sdk/api/OpenFgaApiTest.java index fe955ff..7ab7cab 100644 --- a/src/test/java/dev/openfga/sdk/api/OpenFgaApiTest.java +++ b/src/test/java/dev/openfga/sdk/api/OpenFgaApiTest.java @@ -19,7 +19,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.pgssoft.httpclient.HttpClientMock; import dev.openfga.sdk.api.client.*; -import dev.openfga.sdk.api.client.model.*; import dev.openfga.sdk.api.configuration.*; import dev.openfga.sdk.api.model.*; import dev.openfga.sdk.errors.*; @@ -204,12 +203,11 @@ public void createStoreTest() throws Exception { @Test public void createStore_bodyRequired() { // When - ExecutionException execException = assertThrows( - ExecutionException.class, () -> fga.createStore(null).get()); + var exception = assertThrows( + FgaInvalidParameterException.class, () -> fga.createStore(null).get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); - assertEquals("Missing the required parameter 'body' when calling createStore", exception.getMessage()); + assertEquals("Required parameter body was invalid when calling createStore.", exception.getMessage()); } @Test @@ -296,12 +294,11 @@ public void getStoreTest() throws Exception { @Test public void getStore_storeIdRequired() { // When - ExecutionException execException = - assertThrows(ExecutionException.class, () -> fga.getStore(null).get()); + var exception = assertThrows( + FgaInvalidParameterException.class, () -> fga.getStore(null).get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); - assertEquals("Missing the required parameter 'storeId' when calling getStore", exception.getMessage()); + assertEquals("Required parameter storeId was invalid when calling getStore.", exception.getMessage()); } @Test @@ -384,12 +381,11 @@ public void deleteStoreTest() throws Exception { @Test public void deleteStore_storeIdRequired() { // When - ExecutionException execException = assertThrows( - ExecutionException.class, () -> fga.deleteStore(null).get()); + var exception = assertThrows( + FgaInvalidParameterException.class, () -> fga.deleteStore(null).get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); - assertEquals("Missing the required parameter 'storeId' when calling deleteStore", exception.getMessage()); + assertEquals("Required parameter storeId was invalid when calling deleteStore.", exception.getMessage()); } @Test @@ -487,15 +483,13 @@ public void readAuthorizationModelsTest() throws Exception { @Test public void readAuthorizationModels_storeIdRequired() { // When - ExecutionException execException = - assertThrows(ExecutionException.class, () -> fga.readAuthorizationModels(null, null, null) + var exception = + assertThrows(FgaInvalidParameterException.class, () -> fga.readAuthorizationModels(null, null, null) .get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); assertEquals( - "Missing the required parameter 'storeId' when calling readAuthorizationModels", - exception.getMessage()); + "Required parameter storeId was invalid when calling readAuthorizationModels.", exception.getMessage()); } @Test @@ -595,28 +589,25 @@ public void writeAuthorizationModelTest() throws Exception { @Test public void writeAuthorizationModel_storeIdRequired() { // When - ExecutionException execException = assertThrows( - ExecutionException.class, () -> fga.writeAuthorizationModel(null, new WriteAuthorizationModelRequest()) - .get()); + var exception = assertThrows(FgaInvalidParameterException.class, () -> fga.writeAuthorizationModel( + null, new WriteAuthorizationModelRequest()) + .get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); assertEquals( - "Missing the required parameter 'storeId' when calling writeAuthorizationModel", - exception.getMessage()); + "Required parameter storeId was invalid when calling writeAuthorizationModel.", exception.getMessage()); } @Test public void writeAuthorizationModel_bodyRequired() { // When - ExecutionException execException = - assertThrows(ExecutionException.class, () -> fga.writeAuthorizationModel(DEFAULT_STORE_ID, null) - .get()); + var exception = assertThrows( + FgaInvalidParameterException.class, + () -> fga.writeAuthorizationModel(DEFAULT_STORE_ID, null).get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); assertEquals( - "Missing the required parameter 'body' when calling writeAuthorizationModel", exception.getMessage()); + "Required parameter body was invalid when calling writeAuthorizationModel.", exception.getMessage()); } @Test @@ -712,26 +703,24 @@ public void readAuthorizationModelTest() throws Exception { @Test public void readAuthorizationModel_storeIdRequired() { // When - ExecutionException execException = - assertThrows(ExecutionException.class, () -> fga.readAuthorizationModel(null, DEFAULT_AUTH_MODEL_ID) - .get()); + var exception = assertThrows( + FgaInvalidParameterException.class, + () -> fga.readAuthorizationModel(null, DEFAULT_AUTH_MODEL_ID).get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); assertEquals( - "Missing the required parameter 'storeId' when calling readAuthorizationModel", exception.getMessage()); + "Required parameter storeId was invalid when calling readAuthorizationModel.", exception.getMessage()); } @Test public void readAuthorizationModel_idRequired() { // When - ExecutionException execException = - assertThrows(ExecutionException.class, () -> fga.readAuthorizationModel(DEFAULT_STORE_ID, null) - .get()); + var exception = assertThrows( + FgaInvalidParameterException.class, + () -> fga.readAuthorizationModel(DEFAULT_STORE_ID, null).get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); - assertEquals("Missing the required parameter 'id' when calling readAuthorizationModel", exception.getMessage()); + assertEquals("Required parameter id was invalid when calling readAuthorizationModel.", exception.getMessage()); } @Test @@ -835,13 +824,11 @@ public void readChangesTest() throws Exception { @Test public void readChanges_storeIdRequired() throws Exception { // When - ExecutionException execException = - assertThrows(ExecutionException.class, () -> fga.readChanges(null, null, null, null) - .get()); + var exception = assertThrows(FgaInvalidParameterException.class, () -> fga.readChanges(null, null, null, null) + .get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); - assertEquals("Missing the required parameter 'storeId' when calling readChanges", exception.getMessage()); + assertEquals("Required parameter storeId was invalid when calling readChanges.", exception.getMessage()); } @Test @@ -1008,24 +995,21 @@ public void read_complexContext() throws Exception { @Test public void read_storeIdRequired() { // When - ExecutionException execException = - assertThrows(ExecutionException.class, () -> fga.read(null, new ReadRequest()) - .get()); + var exception = assertThrows(FgaInvalidParameterException.class, () -> fga.read(null, new ReadRequest()) + .get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); - assertEquals("Missing the required parameter 'storeId' when calling read", exception.getMessage()); + assertEquals("Required parameter storeId was invalid when calling read.", exception.getMessage()); } @Test public void read_bodyRequired() { // When - ExecutionException execException = assertThrows( - ExecutionException.class, () -> fga.read(DEFAULT_STORE_ID, null).get()); + var exception = assertThrows(FgaInvalidParameterException.class, () -> fga.read(DEFAULT_STORE_ID, null) + .get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); - assertEquals("Missing the required parameter 'body' when calling read", exception.getMessage()); + assertEquals("Required parameter body was invalid when calling read.", exception.getMessage()); } @Test @@ -1240,25 +1224,21 @@ public Object getObj() { @Test public void write_storeIdRequired() { // When - ExecutionException execException = - assertThrows(ExecutionException.class, () -> fga.write(null, new WriteRequest()) - .get()); + var exception = assertThrows(FgaInvalidParameterException.class, () -> fga.write(null, new WriteRequest()) + .get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); - assertEquals("Missing the required parameter 'storeId' when calling write", exception.getMessage()); + assertEquals("Required parameter storeId was invalid when calling write.", exception.getMessage()); } @Test public void write_bodyRequired() { // When - ExecutionException execException = - assertThrows(ExecutionException.class, () -> fga.write(DEFAULT_STORE_ID, null) - .get()); + var exception = assertThrows(FgaInvalidParameterException.class, () -> fga.write(DEFAULT_STORE_ID, null) + .get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); - assertEquals("Missing the required parameter 'body' when calling write", exception.getMessage()); + assertEquals("Required parameter body was invalid when calling write.", exception.getMessage()); } @Test @@ -1358,25 +1338,21 @@ public void check() throws Exception { @Test public void check_storeIdRequired() { // When - ExecutionException execException = - assertThrows(ExecutionException.class, () -> fga.check(null, new CheckRequest()) - .get()); + var exception = assertThrows(FgaInvalidParameterException.class, () -> fga.check(null, new CheckRequest()) + .get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); - assertEquals("Missing the required parameter 'storeId' when calling check", exception.getMessage()); + assertEquals("Required parameter storeId was invalid when calling check.", exception.getMessage()); } @Test public void check_bodyRequired() { // When - ExecutionException execException = - assertThrows(ExecutionException.class, () -> fga.check(DEFAULT_STORE_ID, null) - .get()); + var exception = assertThrows(FgaInvalidParameterException.class, () -> fga.check(DEFAULT_STORE_ID, null) + .get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); - assertEquals("Missing the required parameter 'body' when calling check", exception.getMessage()); + assertEquals("Required parameter body was invalid when calling check.", exception.getMessage()); } @Test @@ -1487,25 +1463,21 @@ public void expandTest() throws Exception { @Test public void expand_storeIdRequired() { // When - ExecutionException execException = - assertThrows(ExecutionException.class, () -> fga.expand(null, new ExpandRequest()) - .get()); + var exception = assertThrows(FgaInvalidParameterException.class, () -> fga.expand(null, new ExpandRequest()) + .get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); - assertEquals("Missing the required parameter 'storeId' when calling expand", exception.getMessage()); + assertEquals("Required parameter storeId was invalid when calling expand.", exception.getMessage()); } @Test public void expand_bodyRequired() { // When - ExecutionException execException = - assertThrows(ExecutionException.class, () -> fga.expand(DEFAULT_STORE_ID, null) - .get()); + var exception = assertThrows(FgaInvalidParameterException.class, () -> fga.expand(DEFAULT_STORE_ID, null) + .get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); - assertEquals("Missing the required parameter 'body' when calling expand", exception.getMessage()); + assertEquals("Required parameter body was invalid when calling expand.", exception.getMessage()); } @Test @@ -1603,25 +1575,22 @@ public void listObjectsTest() throws Exception { @Test public void listObjects_storeIdRequired() { // When - ExecutionException execException = - assertThrows(ExecutionException.class, () -> fga.listObjects(null, new ListObjectsRequest()) + var exception = + assertThrows(FgaInvalidParameterException.class, () -> fga.listObjects(null, new ListObjectsRequest()) .get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); - assertEquals("Missing the required parameter 'storeId' when calling listObjects", exception.getMessage()); + assertEquals("Required parameter storeId was invalid when calling listObjects.", exception.getMessage()); } @Test public void listObjects_bodyRequired() { // When - ExecutionException execException = - assertThrows(ExecutionException.class, () -> fga.listObjects(DEFAULT_STORE_ID, null) - .get()); + var exception = assertThrows(FgaInvalidParameterException.class, () -> fga.listObjects(DEFAULT_STORE_ID, null) + .get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); - assertEquals("Missing the required parameter 'body' when calling listObjects", exception.getMessage()); + assertEquals("Required parameter body was invalid when calling listObjects.", exception.getMessage()); } @Test @@ -1721,26 +1690,24 @@ public void readAssertionsTest() throws Exception { @Test public void readAssertions_storeIdRequired() { // When - ExecutionException execException = - assertThrows(ExecutionException.class, () -> fga.readAssertions(null, DEFAULT_AUTH_MODEL_ID) + var exception = + assertThrows(FgaInvalidParameterException.class, () -> fga.readAssertions(null, DEFAULT_AUTH_MODEL_ID) .get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); - assertEquals("Missing the required parameter 'storeId' when calling readAssertions", exception.getMessage()); + assertEquals("Required parameter storeId was invalid when calling readAssertions.", exception.getMessage()); } @Test public void readAssertions_authModelIdRequired() { // When - ExecutionException execException = - assertThrows(ExecutionException.class, () -> fga.readAssertions(DEFAULT_STORE_ID, null) + var exception = + assertThrows(FgaInvalidParameterException.class, () -> fga.readAssertions(DEFAULT_STORE_ID, null) .get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); assertEquals( - "Missing the required parameter 'authorizationModelId' when calling readAssertions", + "Required parameter authorizationModelId was invalid when calling readAssertions.", exception.getMessage()); } @@ -1837,39 +1804,36 @@ public void writeAssertionsTest() throws Exception { @Test public void writeAssertions_storeIdRequired() { // When - ExecutionException execException = assertThrows(ExecutionException.class, () -> fga.writeAssertions( + var exception = assertThrows(FgaInvalidParameterException.class, () -> fga.writeAssertions( null, DEFAULT_AUTH_MODEL_ID, new WriteAssertionsRequest()) .get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); - assertEquals("Missing the required parameter 'storeId' when calling writeAssertions", exception.getMessage()); + assertEquals("Required parameter storeId was invalid when calling writeAssertions.", exception.getMessage()); } @Test public void writeAssertions_authModelIdRequired() { // When - ExecutionException execException = assertThrows(ExecutionException.class, () -> fga.writeAssertions( + var exception = assertThrows(FgaInvalidParameterException.class, () -> fga.writeAssertions( DEFAULT_STORE_ID, null, new WriteAssertionsRequest()) .get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); assertEquals( - "Missing the required parameter 'authorizationModelId' when calling writeAssertions", + "Required parameter authorizationModelId was invalid when calling writeAssertions.", exception.getMessage()); } @Test public void writeAssertions_bodyRequired() { // When - ExecutionException execException = assertThrows( - ExecutionException.class, () -> fga.writeAssertions(DEFAULT_STORE_ID, DEFAULT_AUTH_MODEL_ID, null) - .get()); + var exception = assertThrows(FgaInvalidParameterException.class, () -> fga.writeAssertions( + DEFAULT_STORE_ID, DEFAULT_AUTH_MODEL_ID, null) + .get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); - assertEquals("Missing the required parameter 'body' when calling writeAssertions", exception.getMessage()); + assertEquals("Required parameter body was invalid when calling writeAssertions.", exception.getMessage()); } @Test diff --git a/src/test/java/dev/openfga/sdk/api/client/OpenFgaClientTest.java b/src/test/java/dev/openfga/sdk/api/client/OpenFgaClientTest.java index de09cf7..c8b3df0 100644 --- a/src/test/java/dev/openfga/sdk/api/client/OpenFgaClientTest.java +++ b/src/test/java/dev/openfga/sdk/api/client/OpenFgaClientTest.java @@ -256,8 +256,8 @@ public void createStore_bodyRequired() { ExecutionException.class, () -> fga.createStore(null).get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); - assertEquals("Missing the required parameter 'body' when calling createStore", exception.getMessage()); + var exception = assertInstanceOf(FgaInvalidParameterException.class, execException.getCause()); + assertEquals("Required parameter body was invalid when calling createStore.", exception.getMessage()); } @Test @@ -717,9 +717,9 @@ public void writeAuthorizationModel_bodyRequired() { .get()); // Then - var exception = assertInstanceOf(ApiException.class, execException.getCause()); + var exception = assertInstanceOf(FgaInvalidParameterException.class, execException.getCause()); assertEquals( - "Missing the required parameter 'body' when calling writeAuthorizationModel", exception.getMessage()); + "Required parameter body was invalid when calling writeAuthorizationModel.", exception.getMessage()); } @Test