-
Notifications
You must be signed in to change notification settings - Fork 23
feat: APIExecutor for calling raw arbitrary endpoints #273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+2,234
−2
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
f36e79e
feat: raw request builder
SoulPancake a205822
fix: fmt
SoulPancake ef8f654
feat: raw requests integration tests
SoulPancake 9d64d06
fix: rawapi doc
SoulPancake f8360be
Merge branch 'main' into feat/raw-requests
SoulPancake b38e829
feat: calling other endpoints section
SoulPancake 03cd5e2
fix: typo in readme
SoulPancake e8e1119
feat: refactor examples
SoulPancake 382f798
fix: refactor example
SoulPancake 94cb6fd
fix: comment
SoulPancake 32868d9
feat: use list stores via raw req
SoulPancake 947eddf
feat: refactor add typed resp in example
SoulPancake 60b69a4
fix: spotless fmt
SoulPancake e1e3f68
feat: address copilot comments
SoulPancake d7325a2
feat: address coderabbit comments
SoulPancake 541be99
fix: use gradle 8.2.1 for example
SoulPancake 1f08814
Merge branch 'main' into feat/raw-requests
SoulPancake f169187
feat: use build buulder chain for consistency
SoulPancake 1bfd8fb
feat: rename and refactor to APIExecutor for consistency
SoulPancake cf34ecb
Merge branch 'main' into feat/raw-requests
SoulPancake 040446d
fix: rename consistent naming in docs
SoulPancake 69f19ee
fix: changelog
SoulPancake 0fa1cea
Merge branch 'main' into feat/raw-requests
SoulPancake d40ec71
fix: naming
SoulPancake b0fe538
fix: naming convention in example
SoulPancake File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| # API Executor | ||
|
|
||
| Direct HTTP access to OpenFGA endpoints. | ||
|
|
||
| ## Quick Start | ||
|
|
||
| ```java | ||
| OpenFgaClient client = new OpenFgaClient(config); | ||
|
|
||
| // Build request | ||
| ApiExecutorRequestBuilder request = ApiExecutorRequestBuilder.builder("POST", "/stores/{store_id}/check") | ||
| .pathParam("store_id", storeId) | ||
| .body(Map.of("tuple_key", Map.of("user", "user:jon", "relation", "reader", "object", "doc:1"))) | ||
| .build(); | ||
|
|
||
| // Execute - typed response | ||
| ApiResponse<CheckResponse> response = client.apiExecutor().send(request, CheckResponse.class).get(); | ||
|
|
||
| // Execute - raw JSON | ||
| ApiResponse<String> rawResponse = client.apiExecutor().send(request).get(); | ||
| ``` | ||
|
|
||
| ## API Reference | ||
|
|
||
| ### ApiExecutorRequestBuilder | ||
|
|
||
| **Factory:** | ||
| ```java | ||
| ApiExecutorRequestBuilder.builder(String method, String path) | ||
| ``` | ||
|
|
||
| **Methods:** | ||
| ```java | ||
| .pathParam(String key, String value) // Replace {key} in path, URL-encoded | ||
| .queryParam(String key, String value) // Add query parameter, URL-encoded | ||
| .header(String key, String value) // Add HTTP header | ||
| .body(Object body) // Set request body (auto-serialized to JSON) | ||
| .build() // Complete the builder (required) | ||
| ``` | ||
|
|
||
| **Example:** | ||
| ```java | ||
| ApiExecutorRequestBuilder request = ApiExecutorRequestBuilder.builder("POST", "/stores/{store_id}/write") | ||
| .pathParam("store_id", "01ABC") | ||
| .queryParam("dry_run", "true") | ||
| .header("X-Request-ID", "uuid") | ||
| .body(requestObject) | ||
| .build(); | ||
| ``` | ||
|
|
||
| ### ApiExecutor | ||
|
|
||
| **Access:** | ||
| ```java | ||
| ApiExecutor apiExecutor = client.apiExecutor(); | ||
| ``` | ||
|
|
||
| **Methods:** | ||
| ```java | ||
| CompletableFuture<ApiResponse<String>> send(ApiExecutorRequestBuilder request) | ||
| CompletableFuture<ApiResponse<T>> send(ApiExecutorRequestBuilder request, Class<T> responseType) | ||
| ``` | ||
|
|
||
| ### ApiResponse<T> | ||
|
|
||
| ```java | ||
| int getStatusCode() // HTTP status | ||
| Map<String, List<String>> getHeaders() // Response headers | ||
| String getRawResponse() // Raw JSON body | ||
| T getData() // Deserialized data | ||
| ``` | ||
|
|
||
| ## Examples | ||
|
|
||
| ### GET Request | ||
| ```java | ||
| ApiExecutorRequestBuilder request = ApiExecutorRequestBuilder.builder("GET", "/stores/{store_id}/feature") | ||
| .pathParam("store_id", storeId) | ||
| .build(); | ||
|
|
||
| client.apiExecutor().send(request, FeatureResponse.class) | ||
| .thenAccept(r -> System.out.println("Status: " + r.getStatusCode())); | ||
| ``` | ||
|
|
||
| ### POST with Body | ||
| ```java | ||
| ApiExecutorRequestBuilder request = ApiExecutorRequestBuilder.builder("POST", "/stores/{store_id}/bulk-delete") | ||
| .pathParam("store_id", storeId) | ||
| .queryParam("force", "true") | ||
| .body(new BulkDeleteRequest("2023-01-01", "user", 1000)) | ||
| .build(); | ||
|
|
||
| client.apiExecutor().send(request, BulkDeleteResponse.class).get(); | ||
| ``` | ||
|
|
||
| ### Raw JSON Response | ||
| ```java | ||
| ApiResponse<String> response = client.apiExecutor().send(request).get(); | ||
| String json = response.getRawResponse(); // Raw JSON | ||
| ``` | ||
|
|
||
| ### Query Parameters | ||
| ```java | ||
| ApiExecutorRequestBuilder.builder("GET", "/stores/{store_id}/items") | ||
| .pathParam("store_id", storeId) | ||
| .queryParam("page", "1") | ||
| .queryParam("limit", "50") | ||
| .queryParam("sort", "created_at") | ||
| .build(); | ||
| ``` | ||
|
|
||
| ### Custom Headers | ||
| ```java | ||
| ApiExecutorRequestBuilder.builder("POST", "/stores/{store_id}/action") | ||
| .header("X-Request-ID", UUID.randomUUID().toString()) | ||
| .header("X-Idempotency-Key", "key-123") | ||
| .body(data) | ||
| .build(); | ||
| ``` | ||
|
|
||
| ### Error Handling | ||
| ```java | ||
| client.apiExecutor().send(request, ResponseType.class) | ||
| .exceptionally(e -> { | ||
| if (e.getCause() instanceof FgaError) { | ||
| FgaError error = (FgaError) e.getCause(); | ||
| System.err.println("API Error: " + error.getStatusCode()); | ||
| } | ||
| return null; | ||
| }); | ||
| ``` | ||
|
|
||
| ### Map as Request Body | ||
| ```java | ||
| ApiExecutorRequestBuilder.builder("POST", "/stores/{store_id}/settings") | ||
| .pathParam("store_id", storeId) | ||
| .body(Map.of( | ||
| "setting", "value", | ||
| "enabled", true, | ||
| "threshold", 100, | ||
| "options", List.of("opt1", "opt2") | ||
| )) | ||
| .build(); | ||
| ``` | ||
|
|
||
| ## Notes | ||
|
|
||
| - Path/query parameters are URL-encoded automatically | ||
| - Authentication tokens injected from client config | ||
| - `{store_id}` auto-replaced if not provided via `.pathParam()` | ||
|
|
||
| ## Migration to Typed Methods | ||
|
|
||
| When SDK adds typed methods for an endpoint, you can migrate from API Executor: | ||
|
|
||
| ```java | ||
| // API Executor | ||
| ApiExecutorRequestBuilder request = ApiExecutorRequestBuilder.builder("POST", "/stores/{store_id}/check") | ||
| .body(req) | ||
| .build(); | ||
|
|
||
| client.apiExecutor().send(request, CheckResponse.class).get(); | ||
|
|
||
| // Typed SDK (when available) | ||
| client.check(req).get(); | ||
| ``` | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| .PHONY: build run run-openfga | ||
| all: build | ||
|
|
||
| project_name=. | ||
| openfga_version=latest | ||
| language=java | ||
|
|
||
| build: | ||
| ../../gradlew -P language=$(language) build | ||
|
|
||
| run: | ||
| ../../gradlew -P language=$(language) run | ||
|
|
||
| run-openfga: | ||
| docker pull docker.io/openfga/openfga:${openfga_version} && \ | ||
| docker run -p 8080:8080 docker.io/openfga/openfga:${openfga_version} run | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.