|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from typing import IO |
| 5 | + |
| 6 | +from cognite.client._api_client import APIClient |
| 7 | +from cognite.client.data_classes.documents import ( |
| 8 | + TemporaryLink, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +class DocumentPreviewAPI(APIClient): |
| 13 | + _RESOURCE_PATH = "/documents" |
| 14 | + |
| 15 | + def download_page_as_png_bytes(self, id: int, page_number: int = 1) -> bytes: |
| 16 | + """`Downloads an image preview for a specific page of the specified document. <https://developer.cognite.com/api#tag/Document-preview/operation/documentsPreviewImagePage>`_ |
| 17 | +
|
| 18 | + Args: |
| 19 | + id (int): The server-generated ID for the document you want to retrieve the preview of. |
| 20 | + page_number (int): Page number to preview. Starting at 1 for first page. |
| 21 | +
|
| 22 | + Returns: |
| 23 | + bytes: The png preview of the document. |
| 24 | +
|
| 25 | + Examples: |
| 26 | +
|
| 27 | + Download image preview of page 5 of file with id 123: |
| 28 | +
|
| 29 | + >>> from cognite.client import CogniteClient |
| 30 | + >>> client = CogniteClient() |
| 31 | + >>> content = client.documents.previews.download_page_as_png_bytes(id=123, page_number=5) |
| 32 | +
|
| 33 | + Download an image preview and display using IPython.display.Image (for example in a Jupyter Notebook): |
| 34 | +
|
| 35 | + >>> from IPython.display import Image |
| 36 | + >>> binary_png = client.documents.previews.download_page_as_png_bytes(id=123, page_number=5) |
| 37 | + >>> Image(binary_png) |
| 38 | + """ |
| 39 | + res = self._do_request( |
| 40 | + "GET", f"{self._RESOURCE_PATH}/{id}/preview/image/pages/{page_number}", accept="image/png" |
| 41 | + ) |
| 42 | + return res.content |
| 43 | + |
| 44 | + def download_page_as_png( |
| 45 | + self, path: Path | str | IO, id: int, page_number: int = 1, overwrite: bool = False |
| 46 | + ) -> None: |
| 47 | + """`Downloads an image preview for a specific page of the specified document. <https://developer.cognite.com/api#tag/Document-preview/operation/documentsPreviewImagePage>`_ |
| 48 | +
|
| 49 | + Args: |
| 50 | + path (Path | str | IO): The path to save the png preview of the document. If the path is a directory, the file name will be '[id]_page[page_number].png'. |
| 51 | + id (int): The server-generated ID for the document you want to retrieve the preview of. |
| 52 | + page_number (int): Page number to preview. Starting at 1 for first page. |
| 53 | + overwrite (bool): Whether to overwrite existing file at the given path. Defaults to False. |
| 54 | +
|
| 55 | + Examples: |
| 56 | +
|
| 57 | + Download Image preview of page 5 of file with id 123 to folder "previews": |
| 58 | +
|
| 59 | + >>> from cognite.client import CogniteClient |
| 60 | + >>> client = CogniteClient() |
| 61 | + >>> client.documents.previews.download_page_as_png("previews", id=123, page_number=5) |
| 62 | + """ |
| 63 | + if isinstance(path, IO): |
| 64 | + content = self.download_page_as_png_bytes(id) |
| 65 | + path.write(content) |
| 66 | + return |
| 67 | + |
| 68 | + if (path := Path(path)).is_dir(): |
| 69 | + path /= f"{id}_page{page_number}.png" |
| 70 | + elif path.suffix != ".png": |
| 71 | + raise ValueError("Path must be a directory or end with .png") |
| 72 | + if not overwrite and path.exists(): |
| 73 | + raise FileExistsError(f"File {path} already exists. Use overwrite=True to overwrite existing file.") |
| 74 | + content = self.download_page_as_png_bytes(id, page_number) |
| 75 | + path.write_bytes(content) |
| 76 | + |
| 77 | + def download_document_as_pdf_bytes(self, id: int) -> bytes: |
| 78 | + """`Downloads a pdf preview of the specified document. <https://developer.cognite.com/api#tag/Document-preview/operation/documentsPreviewPdf>`_ |
| 79 | +
|
| 80 | + Previews will be rendered if necessary during the request. Be prepared for the request to take a few seconds to complete. |
| 81 | +
|
| 82 | + Args: |
| 83 | + id (int): The server-generated ID for the document you want to retrieve the preview of. |
| 84 | +
|
| 85 | + Returns: |
| 86 | + bytes: The pdf preview of the document. |
| 87 | +
|
| 88 | + Examples: |
| 89 | +
|
| 90 | + Download PDF preview of file with id 123: |
| 91 | +
|
| 92 | + >>> from cognite.client import CogniteClient |
| 93 | + >>> client = CogniteClient() |
| 94 | + >>> content = client.documents.previews.download_document_as_pdf_bytes(id=123) |
| 95 | + """ |
| 96 | + res = self._do_request("GET", f"{self._RESOURCE_PATH}/{id}/preview/pdf", accept="application/pdf") |
| 97 | + return res.content |
| 98 | + |
| 99 | + def download_document_as_pdf(self, path: Path | str | IO, id: int, overwrite: bool = False) -> None: |
| 100 | + """`Downloads a pdf preview of the specified document. <https://developer.cognite.com/api#tag/Document-preview/operation/documentsPreviewPdf>`_ |
| 101 | +
|
| 102 | + Previews will be rendered if necessary during the request. Be prepared for the request to take a few seconds to complete. |
| 103 | +
|
| 104 | + Args: |
| 105 | + path (Path | str | IO): The path to save the pdf preview of the document. If the path is a directory, the file name will be '[id].pdf'. |
| 106 | + id (int): The server-generated ID for the document you want to retrieve the preview of. |
| 107 | + overwrite (bool): Whether to overwrite existing file at the given path. Defaults to False. |
| 108 | +
|
| 109 | + Examples: |
| 110 | +
|
| 111 | + Download PDF preview of file with id 123 to folder "previews": |
| 112 | +
|
| 113 | + >>> from cognite.client import CogniteClient |
| 114 | + >>> client = CogniteClient() |
| 115 | + >>> client.documents.previews.download_document_as_pdf("previews", id=123) |
| 116 | + """ |
| 117 | + if isinstance(path, IO): |
| 118 | + content = self.download_document_as_pdf_bytes(id) |
| 119 | + path.write(content) |
| 120 | + return |
| 121 | + |
| 122 | + if (path := Path(path)).is_dir(): |
| 123 | + path /= f"{id}.pdf" |
| 124 | + elif path.suffix != ".pdf": |
| 125 | + raise ValueError("Path must be a directory or end with .pdf") |
| 126 | + if not overwrite and path.exists(): |
| 127 | + raise FileExistsError(f"File {path} already exists. Use overwrite=True to overwrite existing file.") |
| 128 | + content = self.download_document_as_pdf_bytes(id) |
| 129 | + path.write_bytes(content) |
| 130 | + |
| 131 | + def retrieve_pdf_link(self, id: int) -> TemporaryLink: |
| 132 | + """`Retrieve a Temporary link to download pdf preview <https://developer.cognite.com/api#tag/Document-preview/operation/documentsPreviewPdfTemporaryLink>`_ |
| 133 | +
|
| 134 | + Args: |
| 135 | + id (int): The server-generated ID for the document you want to retrieve the preview of. |
| 136 | +
|
| 137 | + Returns: |
| 138 | + TemporaryLink: A temporary link to download the pdf preview. |
| 139 | +
|
| 140 | + Examples: |
| 141 | +
|
| 142 | + Retrieve the PDF preview download link for document with id 123: |
| 143 | +
|
| 144 | + >>> from cognite.client import CogniteClient |
| 145 | + >>> client = CogniteClient() |
| 146 | + >>> link = client.documents.previews.retrieve_pdf_link(id=123) |
| 147 | + """ |
| 148 | + res = self._get(f"{self._RESOURCE_PATH}/{id}/preview/pdf/temporarylink") |
| 149 | + return TemporaryLink.load(res.json()) |
0 commit comments