-
Notifications
You must be signed in to change notification settings - Fork 281
feat(java): implement URI and path-based pagination #12324
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
tjb9dc
wants to merge
6
commits into
main
Choose a base branch
from
tb/java-uri-path-pagination
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 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ce6bcb3
feat(java): implement URI and path-based pagination visitors
tjb9dc 39e6739
feat(java): implement URI and path-based pagination visitors
tjb9dc 0b79b2f
chore(java): add UriPage/PathPage pagination utilities and update see…
tjb9dc ff9223b
feat(java): generate UriPage/PathPage utilities and conditionally inc…
tjb9dc 866f814
Merge main into tb/java-uri-path-pagination
tjb9dc 5b26775
style(java): apply spotless formatting
tjb9dc 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
122 changes: 122 additions & 0 deletions
122
generators/java/generator-utils/src/main/resources/PathPage.java
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,122 @@ | ||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.function.Function; | ||
| import okhttp3.Headers; | ||
| import okhttp3.OkHttpClient; | ||
| import okhttp3.Request; | ||
| import okhttp3.RequestBody; | ||
| import okhttp3.Response; | ||
| import okhttp3.ResponseBody; | ||
|
|
||
| /** | ||
| * Utility for path-based pagination where the response contains a relative path for the next page. | ||
| * The path is combined with the base URL from client options to form the full request URL. | ||
| */ | ||
| public final class PathPage { | ||
|
|
||
| private PathPage() {} | ||
|
|
||
| /** | ||
| * Creates a {@link SyncPagingIterable} from an initial parsed response, using the provided | ||
| * next path to fetch subsequent pages. | ||
| * | ||
| * @param <T> the item type | ||
| * @param <R> the response type | ||
| * @param initialResponse the parsed response from the initial request | ||
| * @param nextPath the next page path extracted from the response (empty if no more pages) | ||
| * @param items the items extracted from the initial response | ||
| * @param responseClass the class of the response type for deserialization | ||
| * @param getNext function to extract the next path from a parsed response | ||
| * @param getItems function to extract the items list from a parsed response | ||
| * @param httpMethod the HTTP method to use for follow-up requests (e.g., "GET", "POST") | ||
| * @param requestBody the request body for follow-up requests (null for GET) | ||
| * @param additionalHeaders extra headers to include (e.g., Accept, Content-Type) | ||
| * @param clientOptions the client options for making HTTP requests | ||
| * @param requestOptions the request options (headers, timeout, etc.) | ||
| * @return a {@link SyncPagingIterable} that lazily fetches subsequent pages | ||
| */ | ||
| public static <T, R> SyncPagingIterable<T> create( | ||
| R initialResponse, | ||
| Optional<String> nextPath, | ||
| List<T> items, | ||
| Class<R> responseClass, | ||
| Function<R, Optional<String>> getNext, | ||
| Function<R, List<T>> getItems, | ||
| String httpMethod, | ||
| RequestBody requestBody, | ||
| Headers additionalHeaders, | ||
| ClientOptions clientOptions, | ||
| RequestOptions requestOptions) { | ||
| return new SyncPagingIterable<>( | ||
| nextPath.isPresent(), | ||
| items, | ||
| initialResponse, | ||
| nextPath.isPresent() | ||
| ? () -> fetchPage( | ||
| nextPath.get(), | ||
| responseClass, | ||
| getNext, | ||
| getItems, | ||
| httpMethod, | ||
| requestBody, | ||
| additionalHeaders, | ||
| clientOptions, | ||
| requestOptions) | ||
| : () -> null); | ||
| } | ||
|
|
||
| private static <T, R> SyncPagingIterable<T> fetchPage( | ||
| String path, | ||
| Class<R> responseClass, | ||
| Function<R, Optional<String>> getNext, | ||
| Function<R, List<T>> getItems, | ||
| String httpMethod, | ||
| RequestBody requestBody, | ||
| Headers additionalHeaders, | ||
| ClientOptions clientOptions, | ||
| RequestOptions requestOptions) { | ||
| Request.Builder requestBuilder = new Request.Builder() | ||
| .url(clientOptions.environment().getUrl() + path) | ||
| .method(httpMethod, requestBody) | ||
| .headers(Headers.of(clientOptions.headers(requestOptions))); | ||
| for (int i = 0; i < additionalHeaders.size(); i++) { | ||
| requestBuilder.addHeader(additionalHeaders.name(i), additionalHeaders.value(i)); | ||
| } | ||
| Request okhttpRequest = requestBuilder.build(); | ||
| OkHttpClient client = clientOptions.httpClient(); | ||
| if (requestOptions != null && requestOptions.getTimeout().isPresent()) { | ||
| client = clientOptions.httpClientWithTimeout(requestOptions); | ||
| } | ||
| try (Response response = client.newCall(okhttpRequest).execute()) { | ||
| ResponseBody responseBody = response.body(); | ||
| String responseBodyString = responseBody != null ? responseBody.string() : "{}"; | ||
| if (response.isSuccessful()) { | ||
| R parsedResponse = ObjectMappers.JSON_MAPPER.readValue(responseBodyString, responseClass); | ||
| Optional<String> nextUrl = getNext.apply(parsedResponse); | ||
| List<T> results = getItems.apply(parsedResponse); | ||
| return new SyncPagingIterable<>( | ||
| nextUrl.isPresent(), | ||
| results, | ||
| parsedResponse, | ||
| nextUrl.isPresent() | ||
| ? () -> fetchPage( | ||
| nextUrl.get(), | ||
| responseClass, | ||
| getNext, | ||
| getItems, | ||
| httpMethod, | ||
| requestBody, | ||
| additionalHeaders, | ||
| clientOptions, | ||
| requestOptions) | ||
| : () -> null); | ||
| } | ||
| Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); | ||
| throw new __API_EXCEPTION__( | ||
| "Error with status code " + response.code(), response.code(), errorBody, response); | ||
tjb9dc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (IOException e) { | ||
| throw new __BASE_EXCEPTION__("Network error executing HTTP request", e); | ||
| } | ||
| } | ||
| } | ||
121 changes: 121 additions & 0 deletions
121
generators/java/generator-utils/src/main/resources/UriPage.java
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,121 @@ | ||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.function.Function; | ||
| import okhttp3.Headers; | ||
| import okhttp3.OkHttpClient; | ||
| import okhttp3.Request; | ||
| import okhttp3.RequestBody; | ||
| import okhttp3.Response; | ||
| import okhttp3.ResponseBody; | ||
|
|
||
| /** | ||
| * Utility for URI-based pagination where the response contains a full URL for the next page. | ||
| */ | ||
| public final class UriPage { | ||
|
|
||
| private UriPage() {} | ||
|
|
||
| /** | ||
| * Creates a {@link SyncPagingIterable} from an initial parsed response, using the provided | ||
| * next URI to fetch subsequent pages. | ||
| * | ||
| * @param <T> the item type | ||
| * @param <R> the response type | ||
| * @param initialResponse the parsed response from the initial request | ||
| * @param nextUri the next page URI extracted from the response (empty if no more pages) | ||
| * @param items the items extracted from the initial response | ||
| * @param responseClass the class of the response type for deserialization | ||
| * @param getNext function to extract the next URI from a parsed response | ||
| * @param getItems function to extract the items list from a parsed response | ||
| * @param httpMethod the HTTP method to use for follow-up requests (e.g., "GET", "POST") | ||
| * @param requestBody the request body for follow-up requests (null for GET) | ||
| * @param additionalHeaders extra headers to include (e.g., Accept, Content-Type) | ||
| * @param clientOptions the client options for making HTTP requests | ||
| * @param requestOptions the request options (headers, timeout, etc.) | ||
| * @return a {@link SyncPagingIterable} that lazily fetches subsequent pages | ||
| */ | ||
| public static <T, R> SyncPagingIterable<T> create( | ||
| R initialResponse, | ||
| Optional<String> nextUri, | ||
| List<T> items, | ||
| Class<R> responseClass, | ||
| Function<R, Optional<String>> getNext, | ||
| Function<R, List<T>> getItems, | ||
| String httpMethod, | ||
| RequestBody requestBody, | ||
| Headers additionalHeaders, | ||
| ClientOptions clientOptions, | ||
| RequestOptions requestOptions) { | ||
| return new SyncPagingIterable<>( | ||
| nextUri.isPresent(), | ||
| items, | ||
| initialResponse, | ||
| nextUri.isPresent() | ||
| ? () -> fetchPage( | ||
| nextUri.get(), | ||
| responseClass, | ||
| getNext, | ||
| getItems, | ||
| httpMethod, | ||
| requestBody, | ||
| additionalHeaders, | ||
| clientOptions, | ||
| requestOptions) | ||
| : () -> null); | ||
| } | ||
|
|
||
| private static <T, R> SyncPagingIterable<T> fetchPage( | ||
| String url, | ||
| Class<R> responseClass, | ||
| Function<R, Optional<String>> getNext, | ||
| Function<R, List<T>> getItems, | ||
| String httpMethod, | ||
| RequestBody requestBody, | ||
| Headers additionalHeaders, | ||
| ClientOptions clientOptions, | ||
| RequestOptions requestOptions) { | ||
| Request.Builder requestBuilder = new Request.Builder() | ||
| .url(url) | ||
| .method(httpMethod, requestBody) | ||
| .headers(Headers.of(clientOptions.headers(requestOptions))); | ||
| for (int i = 0; i < additionalHeaders.size(); i++) { | ||
| requestBuilder.addHeader(additionalHeaders.name(i), additionalHeaders.value(i)); | ||
| } | ||
| Request okhttpRequest = requestBuilder.build(); | ||
| OkHttpClient client = clientOptions.httpClient(); | ||
| if (requestOptions != null && requestOptions.getTimeout().isPresent()) { | ||
| client = clientOptions.httpClientWithTimeout(requestOptions); | ||
| } | ||
| try (Response response = client.newCall(okhttpRequest).execute()) { | ||
| ResponseBody responseBody = response.body(); | ||
| String responseBodyString = responseBody != null ? responseBody.string() : "{}"; | ||
| if (response.isSuccessful()) { | ||
| R parsedResponse = ObjectMappers.JSON_MAPPER.readValue(responseBodyString, responseClass); | ||
| Optional<String> nextUrl = getNext.apply(parsedResponse); | ||
| List<T> results = getItems.apply(parsedResponse); | ||
| return new SyncPagingIterable<>( | ||
| nextUrl.isPresent(), | ||
| results, | ||
| parsedResponse, | ||
| nextUrl.isPresent() | ||
| ? () -> fetchPage( | ||
| nextUrl.get(), | ||
| responseClass, | ||
| getNext, | ||
| getItems, | ||
| httpMethod, | ||
| requestBody, | ||
| additionalHeaders, | ||
| clientOptions, | ||
| requestOptions) | ||
| : () -> null); | ||
| } | ||
| Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); | ||
| throw new __API_EXCEPTION__( | ||
| "Error with status code " + response.code(), response.code(), errorBody, response); | ||
| } catch (IOException e) { | ||
tjb9dc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new __BASE_EXCEPTION__("Network error executing HTTP request", e); | ||
| } | ||
tjb9dc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
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
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.