Improve balldontlie query flow and dev container write stability
This commit is contained in:
@ -5,7 +5,7 @@ from typing import Any
|
||||
import requests
|
||||
from django.conf import settings
|
||||
|
||||
from apps.providers.exceptions import ProviderRateLimitError, ProviderTransientError
|
||||
from apps.providers.exceptions import ProviderRateLimitError, ProviderTransientError, ProviderUnauthorizedError
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -89,9 +89,14 @@ class BalldontlieClient:
|
||||
|
||||
if status >= 400:
|
||||
body_preview = response.text[:240]
|
||||
raise ProviderTransientError(
|
||||
f"balldontlie client error status={status} path={path} body={body_preview}"
|
||||
)
|
||||
if status == 401:
|
||||
raise ProviderUnauthorizedError(
|
||||
provider="balldontlie",
|
||||
path=path,
|
||||
status_code=status,
|
||||
detail=body_preview,
|
||||
)
|
||||
raise ProviderTransientError(f"balldontlie client error status={status} path={path} body={body_preview}")
|
||||
|
||||
try:
|
||||
return response.json()
|
||||
@ -109,20 +114,36 @@ class BalldontlieClient:
|
||||
page_limit: int = 1,
|
||||
) -> list[dict[str, Any]]:
|
||||
page = 1
|
||||
cursor = None
|
||||
rows: list[dict[str, Any]] = []
|
||||
query = dict(params or {})
|
||||
|
||||
while page <= page_limit:
|
||||
query.update({"page": page, "per_page": per_page})
|
||||
payload = self.get_json(path, params=query)
|
||||
request_query = dict(query)
|
||||
request_query["per_page"] = per_page
|
||||
if cursor is not None:
|
||||
request_query["cursor"] = cursor
|
||||
else:
|
||||
# Keep backwards compatibility for endpoints still supporting page-based pagination.
|
||||
request_query["page"] = page
|
||||
|
||||
payload = self.get_json(path, params=request_query)
|
||||
data = payload.get("data") or []
|
||||
if isinstance(data, list):
|
||||
rows.extend(data)
|
||||
|
||||
meta = payload.get("meta") or {}
|
||||
next_cursor = meta.get("next_cursor")
|
||||
if next_cursor:
|
||||
cursor = next_cursor
|
||||
page += 1
|
||||
continue
|
||||
|
||||
next_page = meta.get("next_page")
|
||||
if not next_page:
|
||||
break
|
||||
page = int(next_page)
|
||||
if next_page:
|
||||
page = int(next_page)
|
||||
continue
|
||||
|
||||
break
|
||||
|
||||
return rows
|
||||
|
||||
Reference in New Issue
Block a user