Skip to content

Commit

Permalink
Fix incompatibility with httpx>=0.28 (#52)
Browse files Browse the repository at this point in the history
In `httpx==0.28.0`, the handling of GET query parameters has changed in
a backwards-incompatible way. The new behaviour is to replace query
parameters in the URL when parameters are passed via the `params` kwarg.
Our pagination code expects that parameters in the URL get merged with
the additional parameters of the `params` kwarg. This PR explicitly
implements the old behaviour of merging params to fix compatibility with
recent releases of `httpx`.

refs: 
- encode/httpx#3364
- encode/httpx#3440
  • Loading branch information
daniel-k authored Jan 16, 2025
1 parent e0e75c9 commit b188f07
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
13 changes: 8 additions & 5 deletions src/enlyze/api_clients/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _full_url(self, api_path: str) -> str:
"""Construct full URL from relative URL"""
return str(self._client.build_request("", api_path).url)

def get(self, api_path: str, **kwargs: Any) -> Any:
def get(self, api_path: str | httpx.URL, **kwargs: Any) -> Any:
"""Wraps :meth:`httpx.Client.get` with defensive error handling
:param api_path: Relative URL path inside the API name space (or a full URL)
Expand Down Expand Up @@ -146,11 +146,11 @@ def _has_more(self, paginated_response: R) -> bool:
def _next_page_call_args(
self,
*,
url: str,
url: str | httpx.URL,
params: dict[str, Any],
paginated_response: R,
**kwargs: Any,
) -> tuple[str, dict[str, Any], dict[str, Any]]:
) -> tuple[str | httpx.URL, dict[str, Any], dict[str, Any]]:
r"""Compute call arguments for the next page.
:param url: The URL used to fetch the current page
Expand All @@ -166,7 +166,7 @@ def _next_page_call_args(
"""

def get_paginated(
self, api_path: str, model: type[M], **kwargs: Any
self, api_path: str | httpx.URL, model: type[M], **kwargs: Any
) -> Iterator[M]:
"""Retrieve objects from a paginated ENLYZE platform API endpoint via HTTP GET.
Expand Down Expand Up @@ -196,7 +196,10 @@ def get_paginated(
params = kwargs.pop("params", {})

while True:
response_body = self.get(url, params=params, **kwargs)
# merge query parameters into URL instead of replacing (ref httpx#3364)
url_with_query_params = httpx.URL(url).copy_merge_params(params)

response_body = self.get(url_with_query_params, **kwargs)
try:
paginated_response = self.PaginatedResponseModel.model_validate(
response_body
Expand Down
4 changes: 2 additions & 2 deletions src/enlyze/api_clients/production_runs/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ def _has_more(self, paginated_response: _PaginatedResponse) -> bool:

def _next_page_call_args(
self,
url: str,
url: str | httpx.URL,
params: dict[str, Any],
paginated_response: _PaginatedResponse,
**kwargs: Any,
) -> tuple[str, dict[str, Any], dict[str, Any]]:
) -> tuple[str | httpx.URL, dict[str, Any], dict[str, Any]]:
next_params = {**params, "cursor": paginated_response.metadata.next_cursor}
return (url, next_params, kwargs)
4 changes: 2 additions & 2 deletions src/enlyze/api_clients/timeseries/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ def _has_more(self, paginated_response: _PaginatedResponse) -> bool:
def _next_page_call_args(
self,
*,
url: str,
url: str | httpx.URL,
params: dict[str, Any],
paginated_response: _PaginatedResponse,
**kwargs: Any,
) -> Tuple[str, dict[str, Any], dict[str, Any]]:
) -> Tuple[str | httpx.URL, dict[str, Any], dict[str, Any]]:
next_url = str(paginated_response.next)
return (next_url, params, kwargs)

0 comments on commit b188f07

Please sign in to comment.