Skip to content

Commit b38ab1d

Browse files
committed
Use palace AsyncClient instance of httpx.AsyncClient
1 parent fcbcdf3 commit b38ab1d

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

src/palace/manager/integration/license/overdrive/api.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import flask
1515
import httpx
16-
from httpx import URL, HTTPStatusError, Limits, RequestError, Timeout
16+
from httpx import URL, Limits, Timeout
1717
from pydantic import ValidationError
1818
from requests import Response
1919
from requests.structures import CaseInsensitiveDict
@@ -118,6 +118,8 @@
118118
from palace.manager.sqlalchemy.util import get_one
119119
from palace.manager.util import base64
120120
from palace.manager.util.datetime_helpers import utc_now
121+
from palace.manager.util.http.async_http import AsyncClient
122+
from palace.manager.util.http.base import ResponseCodesTypes
121123
from palace.manager.util.http.exception import BadResponseException
122124
from palace.manager.util.http.http import HTTP, RequestKwargs
123125

@@ -704,32 +706,24 @@ async def fetch_book_info_list(
704706
books,
705707
urls,
706708
)
707-
except (RequestError, HTTPStatusError) as e:
708-
self.log.error(f"Request error: {e}")
709-
self.log.error(f"URL: {e.request.url}")
710-
request_url = str(e.request.url)
711-
retried_requests[request_url] += 1
712-
713-
if retried_requests[request_url] > 3:
714-
response.raise_for_status()
709+
except BadResponseException as e:
710+
if e.response.status_code == 404:
711+
self.log.warning(
712+
f"404 returned: {e.response.url}: ignoring..."
713+
)
715714
else:
716-
if "404 Not Found" in str(e):
717-
self.log.warning(
718-
f'url "{e.request.url}" NOT FOUND. Skipping...'
719-
)
720-
else:
721-
self.log.warning(
722-
f"Retrying request (attempt {retried_requests[request_url]}/3)"
723-
)
724-
urls.appendleft(request_url)
715+
self.log.error(f"Request error: {e}")
716+
e.response.raise_for_status()
725717
if urls:
726718
self._make_request(client, urls, pending_requests)
727719

728720
return list(books.values()), next
729721

730-
def create_async_client(self, connections: int = 5) -> httpx.AsyncClient:
731-
return httpx.AsyncClient(
722+
def create_async_client(self, connections: int = 5) -> AsyncClient:
723+
return AsyncClient.for_web(
724+
max_retries=3,
732725
timeout=Timeout(20.0, pool=None),
726+
allowed_response_codes=ResponseCodesTypes,
733727
limits=Limits(
734728
max_connections=connections,
735729
max_keepalive_connections=connections,
@@ -739,7 +733,7 @@ def create_async_client(self, connections: int = 5) -> httpx.AsyncClient:
739733

740734
def _make_request(
741735
self,
742-
client: httpx.AsyncClient,
736+
client: AsyncClient,
743737
urls: deque[str],
744738
pending_requests: list[asyncio.Task[httpx._models.Response]],
745739
) -> None:

src/palace/manager/util/http/async_http.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,10 @@ def for_worker(
365365
backoff=backoff,
366366
)
367367

368+
@property
369+
def headers(self) -> httpx.Headers:
370+
return self._httpx_client.headers
371+
368372
async def _perform_request(
369373
self,
370374
method: str,

tests/manager/integration/license/overdrive/test_api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2818,8 +2818,10 @@ async def test_fetch_book_info_list_retry_and_error(
28182818
# error for 4 attempts for availability and metadata
28192819
mock_async_client.queue_response(500, content="500 Internal Server Error")
28202820

2821-
with pytest.raises(HTTPStatusError, match="500 Internal Server Error") as e:
2821+
with pytest.raises(HTTPStatusError) as e:
28222822
initial_endpoint = api.book_info_initial_endpoint(start=None, page_size=1)
28232823
await api.fetch_book_info_list(
28242824
initial_endpoint, fetch_metadata=True, fetch_availability=True
28252825
)
2826+
2827+
assert e.value.response.status_code == 500

0 commit comments

Comments
 (0)