Skip to content
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

add allChildren and getNextPage functions #299

Merged
merged 8 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## Next release

- Adds `allChildren` function in User service to get a paginated list of children
- Adds `getNextPage` function in User service to get next paginated list of children

## v7.0.1 (2023-12-08)

- Adds the `object` field to all models; previously, most models were missing this field.
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/easypost/model/Children.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.easypost.model;

import java.util.List;

public class Children extends EasyPostResource {
private String parentId;
private String name;
private String phoneNumber;
private Boolean verified;
private Boolean defaultCarbonOffset;
jchen293 marked this conversation as resolved.
Show resolved Hide resolved
private List<ApiKey> apiKeys;
}
25 changes: 25 additions & 0 deletions src/main/java/com/easypost/model/ChildrenCollection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.easypost.model;

import java.util.List;
import java.util.Map;

import lombok.Getter;

@Getter
public final class ChildrenCollection extends PaginatedCollection<Children> {
private List<Children> children;

@Override
protected Map<String, Object> buildNextPageParameters(List<Children> children, Integer pageSize) {
String lastId = children.get(children.size() - 1).getId();

Map<String, Object> parameters = new java.util.HashMap<>();
parameters.put("before_id", lastId);

if (pageSize != null) {
parameters.put("page_size", pageSize);
}

return parameters;
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/easypost/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

import com.easypost.Constants;
import com.easypost.exception.EasyPostException;
import com.easypost.exception.General.EndOfPaginationError;
import com.easypost.exception.General.FilteringError;
import com.easypost.http.Requestor;
import com.easypost.http.Requestor.RequestMethod;
import com.easypost.model.ApiKey;
import com.easypost.model.ApiKeys;
import com.easypost.model.Brand;
import com.easypost.model.ChildrenCollection;
import com.easypost.model.User;

import lombok.SneakyThrows;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

public class UserService {
private final EasyPostClient client;
Expand Down Expand Up @@ -137,4 +142,34 @@ public Brand updateBrand(final String id, final Map<String, Object> params) thro

return Requestor.request(RequestMethod.PUT, endpoint, wrappedParams, Brand.class, client);
}

/**
* Retrieve the paginated list of children of a user.
jchen293 marked this conversation as resolved.
Show resolved Hide resolved
*
* @param params Map of parameters.
* @return ChildCollection object.
* @throws EasyPostException when the request fails.
*/
public ChildrenCollection allChildren(final Map<String, Object> params) throws EasyPostException {
String endpoint = "users/children";

return Requestor.request(RequestMethod.GET, endpoint, params, ChildrenCollection.class, client);
}

/**
* Get the next page of an ChildrenCollection.
jchen293 marked this conversation as resolved.
Show resolved Hide resolved
*
* @param collection ChildrenCollection to get next page of.
* @param pageSize The number of results to return on the next page.
* @return ChildrenCollection object.
* @throws EndOfPaginationError when there are no more pages to retrieve.
*/
public ChildrenCollection getNextPage(ChildrenCollection collection, Integer pageSize) throws EndOfPaginationError {
return collection.getNextPage(new Function<Map<String, Object>, ChildrenCollection>() {
@Override @SneakyThrows
public ChildrenCollection apply(Map<String, Object> parameters) {
return allChildren(parameters);
}
}, collection.getChildren(), pageSize);
}
}
88 changes: 88 additions & 0 deletions src/test/cassettes/user/all_children.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 91 additions & 0 deletions src/test/cassettes/user/get_next_page.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions src/test/java/com/easypost/UserTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package com.easypost;

import com.easypost.exception.EasyPostException;
import com.easypost.exception.General.EndOfPaginationError;
import com.easypost.model.Brand;
import com.easypost.model.Children;
import com.easypost.model.ChildrenCollection;
import com.easypost.model.User;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

public final class UserTest {
private static String testUserId = null;
Expand Down Expand Up @@ -177,4 +184,54 @@ public void testUpdateBrand() throws EasyPostException {
assertTrue(brand.getId().startsWith("brd_"));
assertEquals(color, brand.getColor());
}

/**
* Test retrieving a paginated list of children.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testAllChildren() throws EasyPostException {
vcr.setUpTest("all_children");

Map<String, Object> params = new HashMap<>();
params.put("page_size", Fixtures.pageSize());

ChildrenCollection children = vcr.client.user.allChildren(params);

List<Children> childrenList = children.getChildren();

assertTrue(childrenList.size() <= Fixtures.pageSize());
assertNotNull(children.getHasMore());
assertTrue(childrenList.stream().allMatch(children_user -> children_user != null));
}

/**
* Test retrieving the next page.
*
* @throws EasyPostException when the request fails.
*/
@Test
public void testGetNextPage() throws EasyPostException {
vcr.setUpTest("get_next_page");

Map<String, Object> params = new HashMap<>();
params.put("page_size", Fixtures.pageSize());
ChildrenCollection collection = vcr.client.user.allChildren(params);

try {
ChildrenCollection nextPage = vcr.client.user.getNextPage(collection, Fixtures.pageSize());

assertNotNull(nextPage);

String firstIdOfFirstPage = collection.getChildren().get(0).getId();
String firstIdOfSecondPage = nextPage.getChildren().get(0).getId();

assertNotEquals(firstIdOfFirstPage, firstIdOfSecondPage);
} catch (EndOfPaginationError e) { // There's no next page, that's not a failure
assertTrue(true);
} catch (Exception e) { // Any other exception is a failure
fail();
}
}
}