feat: Refactor API path handling in Audiobookshelf client for improved endpoint management

This commit is contained in:
JB
2025-10-30 15:54:10 -07:00
parent 65ec77180d
commit eb28b01c06
2 changed files with 10 additions and 5 deletions
+8 -3
View File
@@ -51,6 +51,11 @@ class AudiobookshelfClient:
self._config = config self._config = config
self._base_url = config.normalized_base_url() self._base_url = config.normalized_base_url()
def _api_path(self, suffix: str = "") -> str:
"""Join the API prefix with the provided suffix without losing proxies."""
clean_suffix = suffix.lstrip("/")
return f"api/{clean_suffix}" if clean_suffix else "api"
def upload_audiobook( def upload_audiobook(
self, self,
audio_path: Path, audio_path: Path,
@@ -111,7 +116,7 @@ class AudiobookshelfClient:
payload["collectionId"] = self._config.collection_id payload["collectionId"] = self._config.collection_id
try: try:
with self._open_client() as client: with self._open_client() as client:
response = client.post("/api/uploads", json=payload) response = client.post(self._api_path("uploads"), json=payload)
response.raise_for_status() response.raise_for_status()
return response.json() return response.json()
except httpx.HTTPError as exc: except httpx.HTTPError as exc:
@@ -121,7 +126,7 @@ class AudiobookshelfClient:
mime_type, _ = mimetypes.guess_type(path.name) mime_type, _ = mimetypes.guess_type(path.name)
mime_type = mime_type or "application/octet-stream" mime_type = mime_type or "application/octet-stream"
data = {"kind": kind, "filename": path.name} data = {"kind": kind, "filename": path.name}
route = f"/api/uploads/{upload_id}/files" route = self._api_path(f"uploads/{upload_id}/files")
try: try:
with path.open("rb") as handle: with path.open("rb") as handle:
files = {"file": (path.name, handle, mime_type)} files = {"file": (path.name, handle, mime_type)}
@@ -144,7 +149,7 @@ class AudiobookshelfClient:
return payload return payload
def _finalize_upload(self, upload_id: str, payload: Dict[str, Any]) -> Dict[str, Any]: def _finalize_upload(self, upload_id: str, payload: Dict[str, Any]) -> Dict[str, Any]:
route = f"/api/uploads/{upload_id}/finish" route = self._api_path(f"uploads/{upload_id}/finish")
try: try:
with self._open_client() as client: with self._open_client() as client:
response = client.post(route, json=payload) response = client.post(route, json=payload)
+2 -2
View File
@@ -3044,7 +3044,7 @@ def test_audiobookshelf() -> ResponseReturnValue:
try: try:
with client._open_client() as http_client: # pylint: disable=protected-access with client._open_client() as http_client: # pylint: disable=protected-access
response = http_client.get("/api/libraries", params={"lite": "true"}) response = http_client.get(client._api_path("libraries"), params={"lite": "true"})
response.raise_for_status() response.raise_for_status()
payload_json = response.json() payload_json = response.json()
except Exception as exc: # pragma: no cover - network guard except Exception as exc: # pragma: no cover - network guard
@@ -3081,7 +3081,7 @@ def test_audiobookshelf() -> ResponseReturnValue:
if collection_id: if collection_id:
try: try:
with client._open_client() as http_client: # pylint: disable=protected-access with client._open_client() as http_client: # pylint: disable=protected-access
collection_resp = http_client.get(f"/api/collections/{collection_id}") collection_resp = http_client.get(client._api_path(f"collections/{collection_id}"))
collection_resp.raise_for_status() collection_resp.raise_for_status()
except Exception as exc: # pragma: no cover - network guard except Exception as exc: # pragma: no cover - network guard
status_code = getattr(getattr(exc, "response", None), "status_code", None) status_code = getattr(getattr(exc, "response", None), "status_code", None)