|
2 | 2 | from typing import ( |
3 | 3 | Any, |
4 | 4 | Dict, |
5 | | - List, |
6 | 5 | Optional, |
7 | 6 | ) |
8 | 7 |
|
9 | | -from easypost.constant import NO_MORE_PAGES_ERROR |
| 8 | +from easypost.constant import ( |
| 9 | + _FILTERS_KEY, |
| 10 | + NO_MORE_PAGES_ERROR, |
| 11 | +) |
10 | 12 | from easypost.easypost_object import convert_to_easypost_object |
11 | 13 | from easypost.errors import EndOfPaginationError |
12 | 14 | from easypost.requestor import ( |
@@ -46,12 +48,14 @@ def _create_resource(self, class_name: str, **params) -> Any: |
46 | 48 |
|
47 | 49 | return convert_to_easypost_object(response=response) |
48 | 50 |
|
49 | | - def _all_resources(self, class_name: str, **params) -> Any: |
| 51 | + def _all_resources(self, class_name: str, filters: Optional[Dict[str, Any]] = None, **params) -> Any: |
50 | 52 | """Retrieve a list of EasyPostObjects from the EasyPost API.""" |
51 | 53 | url = self._class_url(class_name) |
52 | | - |
53 | 54 | response = Requestor(self._client).request(method=RequestMethod.GET, url=url, params=params) |
54 | 55 |
|
| 56 | + if filters: # presence of filters indicates we are dealing with a paginated response |
| 57 | + response[_FILTERS_KEY] = filters # Save the filters used to reference in potential get_next_page call |
| 58 | + |
55 | 59 | return convert_to_easypost_object(response=response) |
56 | 60 |
|
57 | 61 | def _retrieve_resource(self, class_name: str, id: str) -> Any: |
@@ -79,32 +83,7 @@ def _delete_resource(self, class_name: str, id: str) -> Any: |
79 | 83 |
|
80 | 84 | return convert_to_easypost_object(response=response) |
81 | 85 |
|
82 | | - def _get_next_page_resources( |
83 | | - self, |
84 | | - class_name: str, |
85 | | - collection: Dict[str, Any], |
86 | | - page_size: int, |
87 | | - optional_params: Optional[Dict[str, Any]] = None, |
88 | | - ) -> Dict[str, Any]: |
89 | | - """Retrieve next page of EasyPostObjects via the EasyPost API.""" |
90 | | - url = self._class_url(class_name) |
91 | | - collection_array = collection.get(url[1:]) |
92 | | - |
93 | | - if collection_array is None or len(collection_array) == 0 or not collection.get("has_more"): |
| 86 | + def _check_has_next_page(self, collection: Dict[str, Any]) -> None: |
| 87 | + """Raise exception if there is no next page of a collection.""" |
| 88 | + if not collection.get("has_more", False): |
94 | 89 | raise EndOfPaginationError(NO_MORE_PAGES_ERROR) |
95 | | - |
96 | | - params = { |
97 | | - "before_id": collection_array[-1].id, |
98 | | - "page_size": page_size, |
99 | | - } |
100 | | - |
101 | | - if optional_params: |
102 | | - params.update(optional_params) |
103 | | - |
104 | | - response = Requestor(self._client).request(method=RequestMethod.GET, url=url, params=params) |
105 | | - |
106 | | - response_array: List[Any] = response.get(url[1:]) # type: ignore |
107 | | - if response is None or len(response_array) == 0: |
108 | | - raise EndOfPaginationError(NO_MORE_PAGES_ERROR) |
109 | | - |
110 | | - return convert_to_easypost_object(response=response) |
0 commit comments