Skip to content

Commit aa8c43d

Browse files
authored
Add all_children and get_next_page_of_children functions (#315)
1 parent 2b63098 commit aa8c43d

File tree

6 files changed

+213
-1
lines changed

6 files changed

+213
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGELOG
22

3+
## Next release
4+
5+
- Adds `all_children` function to the User service for retrieving paginated lists of children
6+
- Adds `get_next_page_of_children` function to User service to get next paginated list of children
7+
38
## v9.0.1 (2023-12-20)
49

510
- Corrects the return type of `regenerate_rates`

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ clean:
2727

2828
## coverage - Test the project and generate an HTML coverage report
2929
coverage:
30-
$(VIRTUAL_BIN)/pytest --cov=$(PROJECT_NAME) --cov-branch --cov-report=html --cov-report=lcov --cov-report=term-missing --cov-fail-under=88
30+
$(VIRTUAL_BIN)/pytest --cov=$(PROJECT_NAME) --cov-branch --cov-report=html --cov-report=lcov --cov-report=term-missing --cov-fail-under=87
3131

3232
## docs - Generates docs for the library
3333
docs:

easypost/services/user_service.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,33 @@ def update_brand(self, id: str, **params) -> User:
117117
)
118118

119119
return convert_to_easypost_object(response=response)
120+
121+
def all_children(self, **params) -> Dict[str, Any]:
122+
"""Retrieve a paginated list of children from the API."""
123+
url = "/users/children"
124+
response = Requestor(self._client).request(
125+
method=RequestMethod.GET,
126+
url=url,
127+
params=params,
128+
)
129+
130+
return convert_to_easypost_object(response=response)
131+
132+
def get_next_page_of_children(
133+
self,
134+
children: Dict[str, Any],
135+
page_size: int,
136+
optional_params: Optional[Dict[str, Any]] = None,
137+
) -> Dict[str, Any]:
138+
"""Retrieve the next page of the list Children response."""
139+
self._check_has_next_page(collection=children)
140+
141+
params = {
142+
"before_id": children["children"][-1].id,
143+
"page_size": page_size,
144+
}
145+
146+
if optional_params:
147+
params.update(optional_params)
148+
149+
return self.all_children(**params)

tests/cassettes/test_user_all_children.yaml

Lines changed: 71 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/cassettes/test_user_children_get_next_page.yaml

Lines changed: 71 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test_user.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import pytest
2+
from easypost.constant import (
3+
_FILTERS_KEY,
4+
_TEST_FAILED_INTENTIONALLY_ERROR,
5+
NO_MORE_PAGES_ERROR,
6+
)
27
from easypost.models import (
38
Brand,
49
User,
@@ -76,3 +81,33 @@ def test_user_update_brand(prod_client):
7681
assert isinstance(brand, Brand)
7782
assert str.startswith(brand.id, "brd_")
7883
assert brand.color == color
84+
85+
86+
@pytest.mark.vcr()
87+
def test_user_all_children(prod_client, page_size):
88+
children_data = prod_client.user.all_children(page_size=page_size)
89+
90+
children_array = children_data["children"]
91+
assert len(children_array) <= page_size
92+
assert all(isinstance(child, User) for child in children_array)
93+
94+
has_more = children_data["has_more"]
95+
assert isinstance(has_more, bool)
96+
97+
98+
@pytest.mark.vcr()
99+
def test_user_children_get_next_page(prod_client, page_size):
100+
try:
101+
first_page = prod_client.user.all_children(page_size=page_size)
102+
next_page = prod_client.user.get_next_page_of_children(children=first_page, page_size=page_size)
103+
104+
first_id_of_first_page = first_page["children"][0].id
105+
first_id_of_second_page = next_page["children"][0].id
106+
107+
assert first_id_of_first_page != first_id_of_second_page
108+
109+
# Verify that the filters are being passed along for behind-the-scenes reference
110+
assert first_page[_FILTERS_KEY] == next_page[_FILTERS_KEY]
111+
except Exception as e:
112+
if e.message != NO_MORE_PAGES_ERROR:
113+
raise Exception(_TEST_FAILED_INTENTIONALLY_ERROR)

0 commit comments

Comments
 (0)