Repository navigation
server: page the provider list APIs #117
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
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
71 changes: 71 additions & 0 deletions
71
server/opensharing-server-core/src/main/java/io/opensharing/http/Pagination.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,71 @@ | ||
| package io.opensharing.http; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Base64; | ||
| import java.util.List; | ||
| import java.util.function.Function; | ||
|
|
||
| /** | ||
| * Pages list endpoints. A page holds {@link #MAX_RESULTS} items when the request sets no | ||
| * maxResults or sets 0, and never more. A page token is an opaque encoding of the offset of the | ||
| * next page in the list's order, so that order must be total. | ||
| */ | ||
| public final class Pagination { | ||
|
|
||
| public static final int MAX_RESULTS = 500; | ||
|
|
||
| private static final String TOKEN_PREFIX = "os1:"; | ||
|
|
||
| private Pagination() {} | ||
|
|
||
| /** Rows {@code offset} to {@code offset + limit - 1} of a list. */ | ||
| @FunctionalInterface | ||
| public interface Slice<E> { | ||
| List<E> fetch(int offset, int limit); | ||
| } | ||
|
|
||
| /** One page of {@code slice} starting at {@code pageToken}. */ | ||
| public static <E, T> ListResponse<T> page( | ||
| Integer maxResults, String pageToken, Slice<E> slice, Function<E, T> mapper) { | ||
| int size = pageSize(maxResults); | ||
| int offset = offsetOf(pageToken); | ||
| // One extra row tells whether another page follows. | ||
| List<E> rows = slice.fetch(offset, size + 1); | ||
| List<T> items = rows.stream().limit(size).map(mapper).toList(); | ||
| return new ListResponse<>(items, rows.size() > size ? encode(offset + size) : null); | ||
| } | ||
|
|
||
| private static int pageSize(Integer maxResults) { | ||
| if (maxResults == null || maxResults == 0) { | ||
| return MAX_RESULTS; | ||
| } | ||
| if (maxResults < 0) { | ||
| throw ApiException.invalidParameter("maxResults must not be negative"); | ||
| } | ||
| return Math.min(maxResults, MAX_RESULTS); | ||
| } | ||
|
|
||
| private static int offsetOf(String pageToken) { | ||
| if (pageToken == null || pageToken.isEmpty()) { | ||
| return 0; | ||
| } | ||
| try { | ||
| String decoded = new String(Base64.getUrlDecoder().decode(pageToken), StandardCharsets.UTF_8); | ||
| if (decoded.startsWith(TOKEN_PREFIX)) { | ||
| int offset = Integer.parseInt(decoded.substring(TOKEN_PREFIX.length())); | ||
| if (offset >= 0) { | ||
| return offset; | ||
| } | ||
| } | ||
| } catch (IllegalArgumentException invalid) { | ||
| // Reported below like any other token this server did not issue. | ||
| } | ||
| throw ApiException.invalidParameter("pageToken is invalid"); | ||
| } | ||
|
|
||
| private static String encode(int offset) { | ||
| return Base64.getUrlEncoder() | ||
| .withoutPadding() | ||
| .encodeToString((TOKEN_PREFIX + offset).getBytes(StandardCharsets.UTF_8)); | ||
| } | ||
| } | ||
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
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
57 changes: 57 additions & 0 deletions
57
server/opensharing-server-core/src/test/java/io/opensharing/http/PaginationTest.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,57 @@ | ||
| package io.opensharing.http; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| import java.util.List; | ||
| import java.util.function.Function; | ||
| import java.util.stream.IntStream; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class PaginationTest { | ||
|
|
||
| private static final List<Integer> ROWS = IntStream.range(0, 1200).boxed().toList(); | ||
|
|
||
| @Test | ||
| void pagesThroughTheListWithTokens() { | ||
| ListResponse<Integer> first = page(null, null); | ||
| assertEquals(ROWS.subList(0, 500), first.items()); | ||
|
|
||
| ListResponse<Integer> second = page(null, first.nextPageToken()); | ||
| assertEquals(ROWS.subList(500, 1000), second.items()); | ||
|
|
||
| ListResponse<Integer> last = page(null, second.nextPageToken()); | ||
| assertEquals(ROWS.subList(1000, 1200), last.items()); | ||
| assertNull(last.nextPageToken()); | ||
| } | ||
|
|
||
| @Test | ||
| void capsRequestedPageSizesAtTheMaximum() { | ||
| assertEquals(500, page(1000, null).items().size()); | ||
| assertEquals(List.of(0, 1), page(2, null).items()); | ||
| } | ||
|
|
||
| @Test | ||
| void treatsAZeroPageSizeAsUnset() { | ||
| ListResponse<Integer> first = page(0, null); | ||
| assertEquals(ROWS.subList(0, 500), first.items()); | ||
| assertEquals(ROWS.subList(500, 1000), page(0, first.nextPageToken()).items()); | ||
| } | ||
|
|
||
| @Test | ||
| void rejectsNegativePageSizesAndForeignTokens() { | ||
| assertThrows(ApiException.class, () -> page(-1, null)); | ||
| assertThrows(ApiException.class, () -> page(null, "not-a-token")); | ||
| assertThrows(ApiException.class, () -> page(null, "b3MxOi0x")); | ||
| } | ||
|
|
||
| private static ListResponse<Integer> page(Integer maxResults, String pageToken) { | ||
| return Pagination.page(maxResults, pageToken, PaginationTest::slice, Function.identity()); | ||
| } | ||
|
|
||
| private static List<Integer> slice(int offset, int limit) { | ||
| int end = Math.min(offset + limit, ROWS.size()); | ||
| return ROWS.subList(Math.min(offset, end), end); | ||
| } | ||
| } |
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
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.