Coverage for src/lanraragi_api/api/opds.py: 43%
14 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-22 23:19 +0000
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-22 23:19 +0000
1from requests import Response
3from lanraragi_api.api.base import BaseAPICall
6class OPDSAPI(BaseAPICall):
7 """Endpoints related to OPDS catalog generation and serving.
9 Shared request and error behavior is documented on ``BaseAPICall``.
10 """
12 def get_opds_catalog(
13 self, archive_id: str | None = None, category_id: str | None = None
14 ) -> str:
15 """Get the Archive Index as an OPDS 1.2 Catalog with PSE 1.1 compatibility.
17 Args:
18 archive_id: ID of a single archive. When set, the request is
19 forwarded to ``get_opds_item`` and one OPDS entry is returned
20 instead of the catalog. Defaults to None.
21 category_id: Category ID. If passed, the OPDS catalog will be
22 filtered to only show archives from this category. Defaults to
23 None.
25 Returns:
26 str: OPDS catalog, or a single OPDS entry when ``archive_id`` is
27 set, as an XML string.
29 Raises:
30 APIHttpError: Any non-2xx status code returned by the server.
31 """
32 if archive_id:
33 return self.get_opds_item(archive_id)
35 path = "/api/opds"
36 resp = self.request("GET", path, params={"category": category_id})
37 return resp.text
39 def get_opds_item(self, id: str) -> str:
40 """Return a specific OPDS item as XML.
42 This shows only one ``<entry>`` for the given ID in the result, instead
43 of all the archives.
45 Args:
46 id: ID of an archive.
48 Returns:
49 str: OPDS entry as an XML string.
51 Raises:
52 APIHttpError: Any non-2xx status code returned by the server.
53 """
54 resp = self.request("GET", f"/api/opds/{id}")
55 return resp.text
57 def get_opds_page(self, id: str, page: int | None = None) -> Response:
58 """Return a specific image page for OPDS-PSE.
60 Args:
61 id: ID of an archive.
62 page: Page number to fetch. Defaults to None, which lets the server
63 pick the page.
65 Returns:
66 Response: Raw response holding the image bytes of the page.
68 Raises:
69 APIHttpError: Any non-2xx status code returned by the server.
70 """
71 return self.request("GET", f"/api/opds/{id}/pse", params={"page": page})