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

1from requests import Response 

2 

3from lanraragi_api.api.base import BaseAPICall 

4 

5 

6class OPDSAPI(BaseAPICall): 

7 """Endpoints related to OPDS catalog generation and serving. 

8 

9 Shared request and error behavior is documented on ``BaseAPICall``. 

10 """ 

11 

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. 

16 

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. 

24 

25 Returns: 

26 str: OPDS catalog, or a single OPDS entry when ``archive_id`` is 

27 set, as an XML string. 

28 

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) 

34 

35 path = "/api/opds" 

36 resp = self.request("GET", path, params={"category": category_id}) 

37 return resp.text 

38 

39 def get_opds_item(self, id: str) -> str: 

40 """Return a specific OPDS item as XML. 

41 

42 This shows only one ``<entry>`` for the given ID in the result, instead 

43 of all the archives. 

44 

45 Args: 

46 id: ID of an archive. 

47 

48 Returns: 

49 str: OPDS entry as an XML string. 

50 

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 

56 

57 def get_opds_page(self, id: str, page: int | None = None) -> Response: 

58 """Return a specific image page for OPDS-PSE. 

59 

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. 

64 

65 Returns: 

66 Response: Raw response holding the image bytes of the page. 

67 

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})