Skip to content

Commit

Permalink
add allChildren function
Browse files Browse the repository at this point in the history
  • Loading branch information
jchen293 committed Dec 11, 2023
1 parent 0a01de4 commit 21d299d
Show file tree
Hide file tree
Showing 7 changed files with 319 additions and 0 deletions.
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;
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.
*
* @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, "beta");
}

/**
* Get the next page of an ChildrenCollection.
*
* @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);
}
}
94 changes: 94 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();
}
}
}

0 comments on commit 21d299d

Please sign in to comment.