Fail fast when HDHive API returns business errors.
Treat success=false responses from HDHive search/unlock endpoints as explicit service errors so missing API keys or auth issues are surfaced instead of silently returning empty resources. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from config import Config
|
||||
from http_client import request_json
|
||||
from error_handling import AppServiceError, classify_http_error
|
||||
|
||||
|
||||
def _headers():
|
||||
@@ -12,20 +13,42 @@ def _headers():
|
||||
return headers
|
||||
|
||||
|
||||
def _ensure_hdhive_success(response_data):
|
||||
if not isinstance(response_data, dict):
|
||||
return
|
||||
success = response_data.get("success")
|
||||
if success is not False:
|
||||
return
|
||||
|
||||
code = str(response_data.get("code") or "HDHIVE_API_ERROR")
|
||||
message = response_data.get("message") or response_data.get("description") or "HDHIVE request failed"
|
||||
category = classify_http_error(400, code)
|
||||
raise AppServiceError(
|
||||
message,
|
||||
category=category,
|
||||
code=code,
|
||||
status=400,
|
||||
provider="hdhive",
|
||||
detail=response_data,
|
||||
)
|
||||
|
||||
|
||||
def search_resource(media_type, tmdb_id):
|
||||
url = f"{Config.HDHIVE_BASE_URL}/api/open/resources/{media_type}/{tmdb_id}"
|
||||
return request_json(
|
||||
result = request_json(
|
||||
url,
|
||||
headers=_headers(),
|
||||
max_retry=Config.MAX_RETRY,
|
||||
retry_delay_ms=Config.RETRY_DELAY_MS,
|
||||
provider="hdhive",
|
||||
)
|
||||
_ensure_hdhive_success(result.get("data"))
|
||||
return result
|
||||
|
||||
|
||||
def unlock_link(slug):
|
||||
url = f"{Config.HDHIVE_BASE_URL}/api/open/resources/unlock"
|
||||
return request_json(
|
||||
result = request_json(
|
||||
url,
|
||||
method="POST",
|
||||
payload={"slug": slug},
|
||||
@@ -34,6 +57,8 @@ def unlock_link(slug):
|
||||
retry_delay_ms=Config.RETRY_DELAY_MS,
|
||||
provider="hdhive",
|
||||
)
|
||||
_ensure_hdhive_success(result.get("data"))
|
||||
return result
|
||||
|
||||
|
||||
def normalize_resource(search_data, unlock_data):
|
||||
|
||||
Reference in New Issue
Block a user