-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle both error statuses and response actions
- Loading branch information
Showing
4 changed files
with
64 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -183,21 +183,34 @@ def paginate( | |
pagination logic. | ||
data_selector (Optional[jsonpath.TJsonPath]): JSONPath selector for | ||
extracting data from the response. | ||
hooks (Optional[Hooks]): Hooks to modify request/response objects. | ||
hooks (Optional[Hooks]): Hooks to modify request/response objects. Note that | ||
when hooks are not provided, the default behavior is to raise an exception | ||
on error status codes. | ||
Yields: | ||
PageData[Any]: A page of data from the paginated API response, along with request and response context. | ||
Raises: | ||
HTTPError: If the response status code is not a success code. This is raised | ||
by default when hooks are not provided. | ||
Example: | ||
>>> client = RESTClient(base_url="https://api.example.com") | ||
>>> for page in client.paginate("/search", method="post", json={"query": "foo"}): | ||
>>> print(page) | ||
""" | ||
|
||
paginator = paginator if paginator else copy.deepcopy(self.paginator) | ||
auth = auth or self.auth | ||
data_selector = data_selector or self.data_selector | ||
hooks = hooks or {} | ||
|
||
def raise_for_status(response: Response, *args: Any, **kwargs: Any) -> None: | ||
response.raise_for_status() | ||
|
||
if "response" not in hooks: | ||
This comment has been minimized.
Sorry, something went wrong. |
||
hooks["response"] = [raise_for_status] | ||
This comment has been minimized.
Sorry, something went wrong.
willi-mueller
Collaborator
|
||
|
||
request = self._create_request( | ||
path=path, method=method, params=params, json=json, auth=auth, hooks=hooks | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"response" seems to never be in hooks when we follow the example usage