-
Notifications
You must be signed in to change notification settings - Fork 23
feat: raw requestor for calling 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
Open
SoulPancake
wants to merge
18
commits into
main
Choose a base branch
from
feat/raw-requests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 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 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| # Raw API | ||
|
|
||
| Direct HTTP access to OpenFGA endpoints. | ||
|
|
||
| ## Quick Start | ||
|
|
||
| ```java | ||
| OpenFgaClient client = new OpenFgaClient(config); | ||
|
|
||
| // Build request | ||
| RawRequestBuilder request = RawRequestBuilder.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.raw().send(request, CheckResponse.class).get(); | ||
|
|
||
| // Execute - raw JSON | ||
| ApiResponse<String> rawResponse = client.raw().send(request).get(); | ||
| ``` | ||
|
|
||
| ## API Reference | ||
|
|
||
| ### RawRequestBuilder | ||
|
|
||
| **Factory:** | ||
| ```java | ||
| RawRequestBuilder.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 | ||
| RawRequestBuilder request = RawRequestBuilder.builder("POST", "/stores/{store_id}/write") | ||
| .pathParam("store_id", "01ABC") | ||
| .queryParam("dry_run", "true") | ||
| .header("X-Request-ID", "uuid") | ||
| .body(requestObject) | ||
| .build(); | ||
| ``` | ||
|
|
||
| ### RawApi | ||
|
|
||
| **Access:** | ||
| ```java | ||
| RawApi rawApi = client.raw(); | ||
| ``` | ||
|
|
||
| **Methods:** | ||
| ```java | ||
| CompletableFuture<ApiResponse<String>> send(RawRequestBuilder request) | ||
| CompletableFuture<ApiResponse<T>> send(RawRequestBuilder 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 | ||
| RawRequestBuilder request = RawRequestBuilder.builder("GET", "/stores/{store_id}/feature") | ||
| .pathParam("store_id", storeId) | ||
| .build(); | ||
|
|
||
| client.raw().send(request, FeatureResponse.class) | ||
| .thenAccept(r -> System.out.println("Status: " + r.getStatusCode())); | ||
| ``` | ||
|
|
||
| ### POST with Body | ||
| ```java | ||
| RawRequestBuilder request = RawRequestBuilder.builder("POST", "/stores/{store_id}/bulk-delete") | ||
| .pathParam("store_id", storeId) | ||
| .queryParam("force", "true") | ||
| .body(new BulkDeleteRequest("2023-01-01", "user", 1000)) | ||
| .build(); | ||
|
|
||
| client.raw().send(request, BulkDeleteResponse.class).get(); | ||
| ``` | ||
|
|
||
| ### Raw JSON Response | ||
| ```java | ||
| ApiResponse<String> response = client.raw().send(request).get(); | ||
| String json = response.getRawResponse(); // Raw JSON | ||
| ``` | ||
|
|
||
| ### Query Parameters | ||
| ```java | ||
| RawRequestBuilder.builder("GET", "/stores/{store_id}/items") | ||
| .pathParam("store_id", storeId) | ||
| .queryParam("page", "1") | ||
| .queryParam("limit", "50") | ||
| .queryParam("sort", "created_at") | ||
| .build(); | ||
| ``` | ||
|
|
||
| ### Custom Headers | ||
| ```java | ||
| RawRequestBuilder.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.raw().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 | ||
| RawRequestBuilder.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 Raw API: | ||
|
|
||
| ```java | ||
| // Raw API | ||
| RawRequestBuilder request = RawRequestBuilder.builder("POST", "/stores/{store_id}/check") | ||
| .body(req) | ||
| .build(); | ||
|
|
||
| client.raw().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.