Skip to content

Commit 812498b

Browse files
authored
[feat] Add beta feature to retrieve paginated child user list (#300)
1 parent 9a6b196 commit 812498b

File tree

5 files changed

+216
-0
lines changed

5 files changed

+216
-0
lines changed

easypost/beta/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
from easypost.beta.carrier_metadata import CarrierMetadata
33
from easypost.beta.rate import Rate
44
from easypost.beta.referral import Referral
5+
from easypost.beta.user import User

easypost/beta/user.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from typing import (
2+
Any,
3+
Dict,
4+
List,
5+
Optional,
6+
)
7+
8+
from easypost.easypost_object import convert_to_easypost_object
9+
from easypost.error import Error
10+
from easypost.requestor import (
11+
RequestMethod,
12+
Requestor,
13+
)
14+
from easypost.resource import Resource
15+
16+
17+
class User(Resource):
18+
@classmethod
19+
def all_children(cls, api_key: Optional[str] = None, **params) -> Dict[str, Any]:
20+
"""Retrieve a paginated list of children from the API."""
21+
requestor = Requestor(local_api_key=api_key)
22+
url = "/users/children"
23+
response, api_key = requestor.request(method=RequestMethod.GET, url=url, params=params, beta=True)
24+
return convert_to_easypost_object(response=response, api_key=api_key)
25+
26+
@classmethod
27+
def get_next_page_of_children(
28+
cls,
29+
children: Dict[str, Any],
30+
page_size: int,
31+
api_key: Optional[str] = None,
32+
) -> List["User"]:
33+
"""Get next page of children."""
34+
requestor = Requestor(local_api_key=api_key)
35+
url = "/users/children"
36+
children_array = children.get("children", [])
37+
38+
if len(children_array) == 0 or not children.get("has_more", False):
39+
raise Error(message="There are no more pages to retrieve.")
40+
41+
params = {
42+
"after_id": children_array[-1].id,
43+
"page_size": page_size,
44+
}
45+
46+
data, api_key = requestor.request(method=RequestMethod.GET, url=url, params=params, beta=True)
47+
next_children_array: List[Any] = data.get("children", [])
48+
if len(next_children_array) == 0 or not data.get("has_more", False):
49+
raise Error(message="There are no more pages to retrieve.")
50+
51+
return convert_to_easypost_object(response=data, api_key=api_key)

tests/cassettes/test_beta_user_all_children.yaml

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

tests/cassettes/test_beta_user_get_next_page.yaml

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

tests/test_beta_user.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pytest
2+
3+
import easypost
4+
5+
6+
@pytest.mark.vcr()
7+
def test_beta_user_all_children(prod_api_key, page_size):
8+
children_data = easypost.beta.User.all_children(page_size=page_size)
9+
10+
children_array = children_data["children"]
11+
assert len(children_array) <= page_size
12+
assert all(isinstance(child, easypost.User) for child in children_array)
13+
14+
has_more = children_data["has_more"]
15+
assert isinstance(has_more, bool)
16+
17+
18+
@pytest.mark.vcr()
19+
def test_beta_user_get_next_page(prod_api_key, page_size):
20+
try:
21+
children = easypost.beta.User.all_children(page_size=page_size)
22+
next_page = easypost.beta.User.get_next_page_of_children(children=children, page_size=page_size)
23+
24+
first_id_of_first_page = children["children"][0].id
25+
first_id_of_second_page = next_page["children"][0].id
26+
27+
assert first_id_of_first_page != first_id_of_second_page
28+
except easypost.Error as e:
29+
if e.message != "There are no more pages to retrieve.":
30+
raise easypost.Error(message="Test failed intentionally.")

0 commit comments

Comments
 (0)