Skip to content

Commit cff5fb2

Browse files
committed
feat(documents): add async support for document operations
- add AsyncDocument class for async individual document operations - add AsyncDocuments class for async documents collection operations - add async tests for document and documents functionality - add async fixtures for testing async document operations
1 parent 69a61c4 commit cff5fb2

5 files changed

Lines changed: 811 additions & 0 deletions

File tree

src/typesense/async_document.py

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
"""
2+
This module provides async functionality for managing individual documents in Typesense collections.
3+
4+
It contains the AsyncDocument class, which allows for retrieving, updating, and deleting
5+
documents asynchronously.
6+
7+
Classes:
8+
AsyncDocument: Manages async operations on a single document in the Typesense API.
9+
10+
Dependencies:
11+
- typesense.async_api_call: Provides the AsyncApiCall class for making async API requests.
12+
- typesense.types.document: Provides various document schema types.
13+
14+
Note: This module uses conditional imports to support both Python 3.11+ and earlier versions.
15+
"""
16+
17+
import sys
18+
19+
from typesense.async_api_call import AsyncApiCall
20+
from typesense.types.document import (
21+
DeleteSingleDocumentParameters,
22+
DirtyValuesParameters,
23+
DocumentSchema,
24+
RetrieveParameters,
25+
)
26+
27+
if sys.version_info >= (3, 11):
28+
import typing
29+
else:
30+
import typing_extensions as typing
31+
32+
TDoc = typing.TypeVar("TDoc", bound=DocumentSchema)
33+
34+
35+
class AsyncDocument(typing.Generic[TDoc]):
36+
"""
37+
Manages async operations on a single document in the Typesense API.
38+
39+
This class provides async methods to retrieve, update, and delete a document.
40+
41+
Attributes:
42+
api_call (AsyncApiCall): The AsyncApiCall instance for making async API requests.
43+
collection_name (str): The name of the collection.
44+
document_id (str): The ID of the document.
45+
"""
46+
47+
def __init__(
48+
self,
49+
api_call: AsyncApiCall,
50+
collection_name: str,
51+
document_id: str,
52+
) -> None:
53+
"""
54+
Initialize the AsyncDocument instance.
55+
56+
Args:
57+
api_call (AsyncApiCall): The AsyncApiCall instance for making async API requests.
58+
collection_name (str): The name of the collection.
59+
document_id (str): The ID of the document.
60+
"""
61+
self.api_call = api_call
62+
self.collection_name = collection_name
63+
self.document_id = document_id
64+
65+
async def retrieve(
66+
self,
67+
retrieve_parameters: typing.Union[RetrieveParameters, None] = None,
68+
) -> TDoc:
69+
"""
70+
Retrieve this specific document.
71+
72+
Args:
73+
retrieve_parameters (Union[RetrieveParameters, None], optional):
74+
Parameters for retrieving the document.
75+
76+
Returns:
77+
TDoc: The retrieved document.
78+
"""
79+
response: TDoc = await self.api_call.get(
80+
endpoint=self._endpoint_path,
81+
entity_type=typing.Dict[str, str],
82+
as_json=True,
83+
params=retrieve_parameters,
84+
)
85+
return response
86+
87+
async def update(
88+
self,
89+
document: TDoc,
90+
dirty_values_parameters: typing.Union[DirtyValuesParameters, None] = None,
91+
) -> TDoc:
92+
"""
93+
Update this specific document.
94+
95+
Args:
96+
document (TDoc): The updated document data.
97+
dirty_values_parameters (Union[DirtyValuesParameters, None], optional):
98+
Parameters for handling dirty values.
99+
100+
Returns:
101+
TDoc: The updated document.
102+
"""
103+
response = await self.api_call.patch(
104+
self._endpoint_path,
105+
body=document,
106+
params=dirty_values_parameters,
107+
entity_type=typing.Dict[str, str],
108+
)
109+
return typing.cast(TDoc, response)
110+
111+
async def delete(
112+
self,
113+
delete_parameters: typing.Union[DeleteSingleDocumentParameters, None] = None,
114+
) -> TDoc:
115+
"""
116+
Delete this specific document.
117+
118+
Args:
119+
delete_parameters (Union[DeleteSingleDocumentParameters, None], optional):
120+
Parameters for deletion.
121+
122+
Returns:
123+
TDoc: The deleted document.
124+
"""
125+
response: TDoc = await self.api_call.delete(
126+
self._endpoint_path,
127+
entity_type=typing.Dict[str, str],
128+
params=delete_parameters,
129+
)
130+
return response
131+
132+
@property
133+
def _endpoint_path(self) -> str:
134+
"""
135+
Construct the API endpoint path for this specific document.
136+
137+
Returns:
138+
str: The constructed endpoint path.
139+
"""
140+
from typesense.async_collections import AsyncCollections
141+
from typesense.async_documents import AsyncDocuments
142+
143+
return "/".join(
144+
[
145+
AsyncCollections.resource_path,
146+
self.collection_name,
147+
AsyncDocuments.resource_path,
148+
self.document_id,
149+
],
150+
)

0 commit comments

Comments
 (0)