Add YAPF style + ignore, and format tracked Python files

This commit is contained in:
2025-12-29 18:42:02 -08:00
parent c019c00aed
commit 507946a3e4
108 changed files with 11664 additions and 6494 deletions

View File

@@ -31,7 +31,8 @@ class HTTPClient:
retries: int = DEFAULT_RETRIES,
user_agent: str = DEFAULT_USER_AGENT,
verify_ssl: bool = True,
headers: Optional[Dict[str, str]] = None,
headers: Optional[Dict[str,
str]] = None,
):
"""
Initialize HTTP client.
@@ -67,15 +68,19 @@ class HTTPClient:
def _get_headers(self) -> Dict[str, str]:
"""Get request headers with user-agent."""
headers = {"User-Agent": self.user_agent}
headers = {
"User-Agent": self.user_agent
}
headers.update(self.base_headers)
return headers
def get(
self,
url: str,
params: Optional[Dict[str, Any]] = None,
headers: Optional[Dict[str, str]] = None,
params: Optional[Dict[str,
Any]] = None,
headers: Optional[Dict[str,
str]] = None,
allow_redirects: bool = True,
) -> httpx.Response:
"""
@@ -104,7 +109,8 @@ class HTTPClient:
data: Optional[Any] = None,
json: Optional[Dict] = None,
files: Optional[Dict] = None,
headers: Optional[Dict[str, str]] = None,
headers: Optional[Dict[str,
str]] = None,
) -> httpx.Response:
"""
Make a POST request.
@@ -135,7 +141,8 @@ class HTTPClient:
json: Optional[Dict] = None,
content: Optional[Any] = None,
files: Optional[Dict] = None,
headers: Optional[Dict[str, str]] = None,
headers: Optional[Dict[str,
str]] = None,
) -> httpx.Response:
"""
Make a PUT request.
@@ -164,7 +171,8 @@ class HTTPClient:
def delete(
self,
url: str,
headers: Optional[Dict[str, str]] = None,
headers: Optional[Dict[str,
str]] = None,
) -> httpx.Response:
"""
Make a DELETE request.
@@ -201,8 +209,11 @@ class HTTPClient:
url: str,
file_path: str,
chunk_size: int = 8192,
progress_callback: Optional[Callable[[int, int], None]] = None,
headers: Optional[Dict[str, str]] = None,
progress_callback: Optional[Callable[[int,
int],
None]] = None,
headers: Optional[Dict[str,
str]] = None,
) -> Path:
"""
Download a file from URL with optional progress tracking.
@@ -220,7 +231,10 @@ class HTTPClient:
path = Path(file_path)
path.parent.mkdir(parents=True, exist_ok=True)
with self._request_stream("GET", url, headers=headers, follow_redirects=True) as response:
with self._request_stream("GET",
url,
headers=headers,
follow_redirects=True) as response:
response.raise_for_status()
total_bytes = int(response.headers.get("content-length", 0))
bytes_downloaded = 0
@@ -269,7 +283,9 @@ class HTTPClient:
httpx.Response object
"""
if not self._client:
raise RuntimeError("HTTPClient must be used with context manager (with statement)")
raise RuntimeError(
"HTTPClient must be used with context manager (with statement)"
)
# Merge headers
if "headers" in kwargs and kwargs["headers"]:
@@ -289,7 +305,9 @@ class HTTPClient:
return response
except httpx.TimeoutException as e:
last_exception = e
logger.warning(f"Timeout on attempt {attempt + 1}/{self.retries}: {url}")
logger.warning(
f"Timeout on attempt {attempt + 1}/{self.retries}: {url}"
)
if attempt < self.retries - 1:
continue
except httpx.HTTPStatusError as e:
@@ -300,7 +318,9 @@ class HTTPClient:
except:
response_text = "<unable to read response>"
if log_http_errors:
logger.error(f"HTTP {e.response.status_code} from {url}: {response_text}")
logger.error(
f"HTTP {e.response.status_code} from {url}: {response_text}"
)
raise
last_exception = e
try:
@@ -321,7 +341,9 @@ class HTTPClient:
continue
if last_exception:
logger.error(f"Request failed after {self.retries} attempts: {url} - {last_exception}")
logger.error(
f"Request failed after {self.retries} attempts: {url} - {last_exception}"
)
raise last_exception
raise RuntimeError("Request failed after retries")
@@ -329,7 +351,9 @@ class HTTPClient:
def _request_stream(self, method: str, url: str, **kwargs):
"""Make a streaming request."""
if not self._client:
raise RuntimeError("HTTPClient must be used with context manager (with statement)")
raise RuntimeError(
"HTTPClient must be used with context manager (with statement)"
)
# Merge headers
if "headers" in kwargs and kwargs["headers"]:
@@ -351,7 +375,8 @@ class AsyncHTTPClient:
retries: int = DEFAULT_RETRIES,
user_agent: str = DEFAULT_USER_AGENT,
verify_ssl: bool = True,
headers: Optional[Dict[str, str]] = None,
headers: Optional[Dict[str,
str]] = None,
):
"""
Initialize async HTTP client.
@@ -387,15 +412,19 @@ class AsyncHTTPClient:
def _get_headers(self) -> Dict[str, str]:
"""Get request headers with user-agent."""
headers = {"User-Agent": self.user_agent}
headers = {
"User-Agent": self.user_agent
}
headers.update(self.base_headers)
return headers
async def get(
self,
url: str,
params: Optional[Dict[str, Any]] = None,
headers: Optional[Dict[str, str]] = None,
params: Optional[Dict[str,
Any]] = None,
headers: Optional[Dict[str,
str]] = None,
allow_redirects: bool = True,
) -> httpx.Response:
"""
@@ -423,7 +452,8 @@ class AsyncHTTPClient:
url: str,
data: Optional[Any] = None,
json: Optional[Dict] = None,
headers: Optional[Dict[str, str]] = None,
headers: Optional[Dict[str,
str]] = None,
) -> httpx.Response:
"""
Make an async POST request.
@@ -450,8 +480,11 @@ class AsyncHTTPClient:
url: str,
file_path: str,
chunk_size: int = 8192,
progress_callback: Optional[Callable[[int, int], None]] = None,
headers: Optional[Dict[str, str]] = None,
progress_callback: Optional[Callable[[int,
int],
None]] = None,
headers: Optional[Dict[str,
str]] = None,
) -> Path:
"""
Download a file from URL asynchronously with optional progress tracking.
@@ -497,7 +530,9 @@ class AsyncHTTPClient:
httpx.Response object
"""
if not self._client:
raise RuntimeError("AsyncHTTPClient must be used with async context manager")
raise RuntimeError(
"AsyncHTTPClient must be used with async context manager"
)
# Merge headers
if "headers" in kwargs and kwargs["headers"]:
@@ -516,7 +551,9 @@ class AsyncHTTPClient:
return response
except httpx.TimeoutException as e:
last_exception = e
logger.warning(f"Timeout on attempt {attempt + 1}/{self.retries}: {url}")
logger.warning(
f"Timeout on attempt {attempt + 1}/{self.retries}: {url}"
)
if attempt < self.retries - 1:
await asyncio.sleep(0.5) # Brief delay before retry
continue
@@ -527,7 +564,9 @@ class AsyncHTTPClient:
response_text = e.response.text[:500]
except:
response_text = "<unable to read response>"
logger.error(f"HTTP {e.response.status_code} from {url}: {response_text}")
logger.error(
f"HTTP {e.response.status_code} from {url}: {response_text}"
)
raise
last_exception = e
try:
@@ -550,7 +589,9 @@ class AsyncHTTPClient:
continue
if last_exception:
logger.error(f"Request failed after {self.retries} attempts: {url} - {last_exception}")
logger.error(
f"Request failed after {self.retries} attempts: {url} - {last_exception}"
)
raise last_exception
raise RuntimeError("Request failed after retries")
@@ -558,7 +599,9 @@ class AsyncHTTPClient:
def _request_stream(self, method: str, url: str, **kwargs):
"""Make a streaming request."""
if not self._client:
raise RuntimeError("AsyncHTTPClient must be used with async context manager")
raise RuntimeError(
"AsyncHTTPClient must be used with async context manager"
)
# Merge headers
if "headers" in kwargs and kwargs["headers"]:
@@ -587,9 +630,16 @@ def post(url: str, **kwargs) -> httpx.Response:
def download(
url: str,
file_path: str,
progress_callback: Optional[Callable[[int, int], None]] = None,
progress_callback: Optional[Callable[[int,
int],
None]] = None,
**kwargs,
) -> Path:
"""Quick file download without context manager."""
with HTTPClient() as client:
return client.download(url, file_path, progress_callback=progress_callback, **kwargs)
return client.download(
url,
file_path,
progress_callback=progress_callback,
**kwargs
)