feat: reuse connections for better performance
This commit is contained in:
@@ -24,6 +24,7 @@ class DownloadManager:
|
|||||||
self.client = client
|
self.client = client
|
||||||
self.cache_dir = cache_dir
|
self.cache_dir = cache_dir
|
||||||
self.cache_dir.mkdir(parents=True, exist_ok=True)
|
self.cache_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
self._http_client = httpx.Client(auth=auth, timeout=30.0, follow_redirects=False)
|
||||||
|
|
||||||
def get_or_download(self, asin: str, notify: StatusCallback | None = None) -> Path | None:
|
def get_or_download(self, asin: str, notify: StatusCallback | None = None) -> Path | None:
|
||||||
"""Get local path of AAX file, downloading if missing."""
|
"""Get local path of AAX file, downloading if missing."""
|
||||||
@@ -108,11 +109,9 @@ class DownloadManager:
|
|||||||
"key": asin,
|
"key": asin,
|
||||||
"codec": codec,
|
"codec": codec,
|
||||||
}
|
}
|
||||||
response = httpx.get(
|
response = self._http_client.get(
|
||||||
url=DOWNLOAD_URL,
|
url=DOWNLOAD_URL,
|
||||||
params=params,
|
params=params,
|
||||||
follow_redirects=False,
|
|
||||||
auth=self.auth,
|
|
||||||
)
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
@@ -131,7 +130,7 @@ class DownloadManager:
|
|||||||
) -> Path | None:
|
) -> Path | None:
|
||||||
"""Download file from URL to destination."""
|
"""Download file from URL to destination."""
|
||||||
try:
|
try:
|
||||||
with httpx.stream("GET", url) as response:
|
with self._http_client.stream("GET", url) as response:
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
total_size = int(response.headers.get("content-length", 0))
|
total_size = int(response.headers.get("content-length", 0))
|
||||||
downloaded = 0
|
downloaded = 0
|
||||||
@@ -149,3 +148,8 @@ class DownloadManager:
|
|||||||
return dest_path
|
return dest_path
|
||||||
except Exception:
|
except Exception:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def close(self) -> None:
|
||||||
|
"""Close the HTTP client and release resources."""
|
||||||
|
if hasattr(self, "_http_client"):
|
||||||
|
self._http_client.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user