Skip to content

Commit bd0dc63

Browse files
authored
Move all_children functions to GA (#320)
1 parent daa9ec4 commit bd0dc63

File tree

7 files changed

+225
-1
lines changed

7 files changed

+225
-1
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGELOG
22

3+
## v7.15.0 Next release
4+
5+
- Adds `all_children` function to the User class for retrieving paginated lists of children users, and deprecate the beta function
6+
- Adds `get_next_page_of_children` function to the User class for retrieving next paginated lists of children users, and deprecate the beta function
7+
38
## v7.14.1 (2023-10-30)
49

510
- Fixes a bug where `get_next_page` functions threw an error, preventing users from retrieving the final page of results

easypost/beta/user.py

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
List,
55
Optional,
66
)
7+
from warnings import warn
78

89
from easypost.easypost_object import convert_to_easypost_object
910
from easypost.error import Error
@@ -17,6 +18,11 @@
1718
class User(Resource):
1819
@classmethod
1920
def all_children(cls, api_key: Optional[str] = None, **params) -> Dict[str, Any]:
21+
warn(
22+
"This function is deprecated, please use `all_children` function in `user` class for GA accessibility",
23+
DeprecationWarning,
24+
stacklevel=2,
25+
)
2026
"""Retrieve a paginated list of children from the API."""
2127
requestor = Requestor(local_api_key=api_key)
2228
url = "/users/children"
@@ -30,6 +36,12 @@ def get_next_page_of_children(
3036
page_size: int,
3137
api_key: Optional[str] = None,
3238
) -> List["User"]:
39+
warn(
40+
"This function is deprecated, please use `get_next_page_of_children` function in"
41+
"`user` class for GA accessibility",
42+
DeprecationWarning,
43+
stacklevel=2,
44+
)
3345
"""Get next page of children."""
3446
requestor = Requestor(local_api_key=api_key)
3547
url = "/users/children"

easypost/requestor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def _objects_to_ids(cls, param: Dict[str, Any]) -> Dict[str, Any]:
4242
return {"id": param.id}
4343
elif isinstance(param, dict):
4444
data = {}
45-
for (k, v) in param.items():
45+
for k, v in param.items():
4646
if isinstance(v, list):
4747
data[k] = [cls._objects_to_ids(item) for item in v] # type: ignore
4848
else:

easypost/user.py

+36
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from easypost.api_key import ApiKey
99
from easypost.easypost_object import convert_to_easypost_object
10+
from easypost.error import Error
1011
from easypost.requestor import (
1112
RequestMethod,
1213
Requestor,
@@ -83,3 +84,38 @@ def update_brand(self, api_key: Optional[str] = None, **params) -> "User":
8384
method=RequestMethod.PATCH, url=self.instance_url() + "/brand", params=params
8485
)
8586
return convert_to_easypost_object(response=response, api_key=api_key)
87+
88+
@classmethod
89+
def all_children(cls, api_key: Optional[str] = None, **params) -> Dict[str, Any]:
90+
"""Retrieve a paginated list of children from the API."""
91+
requestor = Requestor(local_api_key=api_key)
92+
url = "/users/children"
93+
response, api_key = requestor.request(method=RequestMethod.GET, url=url, params=params)
94+
return convert_to_easypost_object(response=response, api_key=api_key)
95+
96+
@classmethod
97+
def get_next_page_of_children(
98+
cls,
99+
children: Dict[str, Any],
100+
page_size: int,
101+
api_key: Optional[str] = None,
102+
) -> List["User"]:
103+
"""Get next page of children."""
104+
requestor = Requestor(local_api_key=api_key)
105+
url = "/users/children"
106+
children_array = children.get("children", [])
107+
108+
if len(children_array) == 0 or not children.get("has_more", False):
109+
raise Error(message="There are no more pages to retrieve.")
110+
111+
params = {
112+
"after_id": children_array[-1].id,
113+
"page_size": page_size,
114+
}
115+
116+
data, api_key = requestor.request(method=RequestMethod.GET, url=url, params=params)
117+
next_children_array: List[Any] = data.get("children", [])
118+
if len(next_children_array) == 0:
119+
raise Error(message="There are no more pages to retrieve.")
120+
121+
return convert_to_easypost_object(response=data, api_key=api_key)

tests/cassettes/test_user_all_children.yaml

+71
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/cassettes/test_user_get_next_page.yaml

+73
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test_user.py

+27
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,30 @@ def test_user_update_brand(prod_api_key):
109109
assert isinstance(brand, easypost.Brand)
110110
assert str.startswith(brand.id, "brd_")
111111
assert brand.color == color
112+
113+
114+
@pytest.mark.vcr()
115+
def test_user_all_children(prod_api_key, page_size):
116+
children_data = easypost.User.all_children(page_size=page_size)
117+
118+
children_array = children_data["children"]
119+
assert len(children_array) <= page_size
120+
assert all(isinstance(child, easypost.User) for child in children_array)
121+
122+
has_more = children_data["has_more"]
123+
assert isinstance(has_more, bool)
124+
125+
126+
@pytest.mark.vcr()
127+
def test_user_get_next_page(prod_api_key, page_size):
128+
try:
129+
children = easypost.User.all_children(page_size=page_size)
130+
next_page = easypost.User.get_next_page_of_children(children=children, page_size=page_size)
131+
132+
first_id_of_first_page = children["children"][0].id
133+
first_id_of_second_page = next_page["children"][0].id
134+
135+
assert first_id_of_first_page != first_id_of_second_page
136+
except easypost.Error as e:
137+
if e.message != "There are no more pages to retrieve.":
138+
raise easypost.Error(message="Test failed intentionally.")

0 commit comments

Comments
 (0)