Coverage for src/paperap/resources/documents.py: 58%

1"""

2----------------------------------------------------------------------------

3

4 METADATA:

5

6 File: documents.py

7 Project: paperap

8 Created: 2025-03-04

9 Version: 0.0.7

10 Author: Jess Mann

11 Email: jess@jmann.me

12 Copyright (c) 2025 Jess Mann

13

14----------------------------------------------------------------------------

15

16 LAST MODIFIED:

17

18 2025-03-04 By Jess Mann

19

20"""

21

22 from __future__ import annotations

23

24 from datetime import datetime

25 from typing import Any, Iterator, Optional, override

26

27 from typing_extensions import TypeVar

28

29 from paperap.exceptions import APIError, BadResponseError, ResourceNotFoundError

30 from paperap.models.document import Document, DocumentNote, DocumentQuerySet

31 from paperap.resources.base import BaseResource, StandardResource

32

33

34 class DocumentResource(StandardResource[Document, DocumentQuerySet]):

35 """Resource for managing documents."""

36

37 model_class = Document

38 name = "documents"

39

40 def download(self, document_id: int, *, original: bool = False) -> bytes | None:

41 url = f"documents/{document_id}/download"

42 params = {"original": str(original).lower()}

43 response = self.client.request("GET", url, params=params)

44 if not response:

45 raise ResourceNotFoundError(f"Document {document_id} download failed", self.name)

46 return response.get("content")

47

48 def upload(self, file_content: bytes, filename: str, **metadata) -> Document:

49 """Upload a document with optional metadata."""

50 data = {

51 "document": (filename, file_content),

52 **metadata

53 }

54 response = self.client.request("POST", "documents/post_document/", data=data)

55 if not response:

56 raise ResourceNotFoundError("Document upload failed")

57 return self.parse_to_model(response)

58

59

60 class DocumentNoteResource(StandardResource[DocumentNote]):

61 """Resource for managing document notes."""

62

63 model_class = DocumentNote

64 name = "document_notes"