|
| 1 | +""" |
| 2 | +This module provides functionality for managing individual documents in Typesense collections. |
| 3 | +
|
| 4 | +Classes: |
| 5 | + - Document: Handles operations related to a specific document within a collection. |
| 6 | +
|
| 7 | +Methods: |
| 8 | + - __init__: Initializes the Document object. |
| 9 | + - _endpoint_path: Constructs the API endpoint path for this specific document. |
| 10 | + - retrieve: Retrieves the details of this specific document. |
| 11 | + - update: Updates this specific document. |
| 12 | + - delete: Deletes this specific document. |
| 13 | +
|
| 14 | +The Document class interacts with the Typesense API to manage operations on a |
| 15 | +specific document within a collection. It provides methods to retrieve, update, |
| 16 | +and delete individual documents. |
| 17 | +
|
| 18 | +This module uses type hinting and is compatible with Python 3.11+ as well as earlier |
| 19 | +versions through the use of the typing_extensions library. |
| 20 | +""" |
| 21 | + |
1 | 22 | import sys |
2 | 23 |
|
3 | 24 | from typesense.api_call import ApiCall |
4 | | -from typesense.configuration import Configuration |
5 | | -from typesense.types.collection import CollectionSchema |
6 | | -from typesense.types.document import ( |
7 | | - DeleteQueryParameters, |
8 | | - DirtyValuesParameters, |
9 | | - DocumentSchema, |
10 | | - DocumentWriteParameters, |
11 | | -) |
| 25 | +from typesense.types.document import DirtyValuesParameters, DocumentSchema |
12 | 26 |
|
13 | 27 | if sys.version_info >= (3, 11): |
14 | 28 | import typing |
|
19 | 33 |
|
20 | 34 |
|
21 | 35 | class Document(typing.Generic[TDoc]): |
| 36 | + """ |
| 37 | + Class for managing individual documents in a Typesense collection. |
| 38 | +
|
| 39 | + This class provides methods to interact with a specific document, |
| 40 | + including retrieving, updating, and deleting it. |
| 41 | +
|
| 42 | + Attributes: |
| 43 | + api_call (ApiCall): The API call object for making requests. |
| 44 | + collection_name (str): The name of the collection. |
| 45 | + document_id (str): The ID of the document. |
| 46 | + """ |
| 47 | + |
22 | 48 | def __init__( |
23 | | - self, api_call: ApiCall, collection_name: str, document_id: str |
| 49 | + self, |
| 50 | + api_call: ApiCall, |
| 51 | + collection_name: str, |
| 52 | + document_id: str, |
24 | 53 | ) -> None: |
| 54 | + """ |
| 55 | + Initialize the Document object. |
| 56 | +
|
| 57 | + Args: |
| 58 | + api_call (ApiCall): The API call object for making requests. |
| 59 | + collection_name (str): The name of the collection. |
| 60 | + document_id (str): The ID of the document. |
| 61 | + """ |
25 | 62 | self.api_call = api_call |
26 | 63 | self.collection_name = collection_name |
27 | 64 | self.document_id = document_id |
28 | 65 |
|
29 | | - @property |
30 | | - def _endpoint_path(self) -> str: |
31 | | - from .collections import Collections |
32 | | - from .documents import Documents |
33 | | - |
34 | | - return "{0}/{1}/{2}/{3}".format( |
35 | | - Collections.RESOURCE_PATH, |
36 | | - self.collection_name, |
37 | | - Documents.RESOURCE_PATH, |
38 | | - self.document_id, |
39 | | - ) |
40 | | - |
41 | 66 | def retrieve(self) -> TDoc: |
42 | | - response = self.api_call.get( |
| 67 | + """ |
| 68 | + Retrieve this specific document. |
| 69 | +
|
| 70 | + Returns: |
| 71 | + TDoc: The retrieved document. |
| 72 | + """ |
| 73 | + response: TDoc = self.api_call.get( |
43 | 74 | endpoint=self._endpoint_path, |
44 | 75 | entity_type=typing.Dict[str, str], |
45 | 76 | as_json=True, |
46 | 77 | ) |
47 | | - |
48 | | - return typing.cast(TDoc, response) |
| 78 | + return response |
49 | 79 |
|
50 | 80 | def update( |
51 | | - self, document: TDoc, params: typing.Union[DirtyValuesParameters, None] = None |
| 81 | + self, |
| 82 | + document: TDoc, |
| 83 | + dirty_values_parameters: typing.Union[DirtyValuesParameters, None] = None, |
52 | 84 | ) -> TDoc: |
| 85 | + """ |
| 86 | + Update this specific document. |
| 87 | +
|
| 88 | + Args: |
| 89 | + document (TDoc): The updated document data. |
| 90 | + dirty_values_parameters (Union[DirtyValuesParameters, None], optional): |
| 91 | + Parameters for handling dirty values. |
| 92 | +
|
| 93 | + Returns: |
| 94 | + TDoc: The updated document. |
| 95 | + """ |
53 | 96 | response = self.api_call.patch( |
54 | 97 | self._endpoint_path, |
55 | 98 | body=document, |
56 | | - params=params, |
| 99 | + params=dirty_values_parameters, |
57 | 100 | entity_type=typing.Dict[str, str], |
58 | 101 | ) |
59 | | - |
60 | 102 | return typing.cast(TDoc, response) |
61 | 103 |
|
62 | 104 | def delete(self) -> TDoc: |
63 | | - response = self.api_call.delete( |
64 | | - self._endpoint_path, entity_type=typing.Dict[str, str] |
| 105 | + """ |
| 106 | + Delete this specific document. |
| 107 | +
|
| 108 | + Returns: |
| 109 | + TDoc: The deleted document. |
| 110 | + """ |
| 111 | + response: TDoc = self.api_call.delete( |
| 112 | + self._endpoint_path, |
| 113 | + entity_type=typing.Dict[str, str], |
| 114 | + ) |
| 115 | + return response |
| 116 | + |
| 117 | + @property |
| 118 | + def _endpoint_path(self) -> str: |
| 119 | + """ |
| 120 | + Construct the API endpoint path for this specific document. |
| 121 | +
|
| 122 | + Returns: |
| 123 | + str: The constructed endpoint path. |
| 124 | + """ |
| 125 | + from typesense.collections import Collections |
| 126 | + from typesense.documents import Documents |
| 127 | + |
| 128 | + return "/".join( |
| 129 | + [ |
| 130 | + Collections.RESOURCE_PATH, |
| 131 | + self.collection_name, |
| 132 | + Documents.resource_path, |
| 133 | + self.document_id, |
| 134 | + ], |
65 | 135 | ) |
66 | | - return typing.cast(TDoc, response) |
|
0 commit comments