Skip to content

Commit e81edec

Browse files
committed
feat(curation): add async support for curation set operations
- add AsyncCurationSet class for async individual curation set operations - add AsyncCurationSets class for async curation sets collection operations - add async tests for curation set and sets functionality - add async fixtures for testing async curation set operations - remove future annotations imports from test files
1 parent 421608a commit e81edec

5 files changed

Lines changed: 569 additions & 2 deletions

File tree

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
"""
2+
This module provides async functionality for managing individual curation sets in Typesense.
3+
4+
It contains the AsyncCurationSet class, which allows for retrieving, updating, deleting,
5+
and managing items within a curation set asynchronously.
6+
7+
Classes:
8+
AsyncCurationSet: Manages async operations on a single curation set in the Typesense API.
9+
10+
Dependencies:
11+
- typesense.async_api_call: Provides the AsyncApiCall class for making async API requests.
12+
- typesense.types.curation_set: Provides various curation set 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+
if sys.version_info >= (3, 11):
20+
import typing
21+
else:
22+
import typing_extensions as typing
23+
24+
from typesense.async_api_call import AsyncApiCall
25+
from typesense.types.curation_set import (
26+
CurationItemDeleteSchema,
27+
CurationItemSchema,
28+
CurationSetDeleteSchema,
29+
CurationSetListItemResponseSchema,
30+
CurationSetSchema,
31+
CurationSetUpsertSchema,
32+
)
33+
34+
35+
class AsyncCurationSet:
36+
"""
37+
Manages async operations on a single curation set in the Typesense API.
38+
39+
This class provides async methods to retrieve, update, and delete a curation set,
40+
as well as manage items within the curation set.
41+
42+
Attributes:
43+
api_call (AsyncApiCall): The AsyncApiCall instance for making async API requests.
44+
name (str): The name of the curation set.
45+
"""
46+
47+
def __init__(self, api_call: AsyncApiCall, name: str) -> None:
48+
"""
49+
Initialize the AsyncCurationSet instance.
50+
51+
Args:
52+
api_call (AsyncApiCall): The AsyncApiCall instance for making async API requests.
53+
name (str): The name of the curation set.
54+
"""
55+
self.api_call = api_call
56+
self.name = name
57+
58+
@property
59+
def _endpoint_path(self) -> str:
60+
"""
61+
Get the API endpoint path for this curation set.
62+
63+
Returns:
64+
str: The full endpoint path for the curation set.
65+
"""
66+
from typesense.async_curation_sets import AsyncCurationSets
67+
68+
return "/".join([AsyncCurationSets.resource_path, self.name])
69+
70+
async def retrieve(self) -> CurationSetSchema:
71+
"""
72+
Retrieve this specific curation set.
73+
74+
Returns:
75+
CurationSetSchema: The schema containing the curation set details.
76+
"""
77+
response: CurationSetSchema = await self.api_call.get(
78+
self._endpoint_path,
79+
as_json=True,
80+
entity_type=CurationSetSchema,
81+
)
82+
return response
83+
84+
async def delete(self) -> CurationSetDeleteSchema:
85+
"""
86+
Delete this specific curation set.
87+
88+
Returns:
89+
CurationSetDeleteSchema: The schema containing the deletion response.
90+
"""
91+
response: CurationSetDeleteSchema = await self.api_call.delete(
92+
self._endpoint_path,
93+
entity_type=CurationSetDeleteSchema,
94+
)
95+
return response
96+
97+
async def upsert(
98+
self,
99+
payload: CurationSetUpsertSchema,
100+
) -> CurationSetSchema:
101+
"""
102+
Create or update this curation set.
103+
104+
Args:
105+
payload (CurationSetUpsertSchema): The schema for creating or updating the curation set.
106+
107+
Returns:
108+
CurationSetSchema: The created or updated curation set.
109+
"""
110+
response: CurationSetSchema = await self.api_call.put(
111+
"/".join([self._endpoint_path]),
112+
body=payload,
113+
entity_type=CurationSetSchema,
114+
)
115+
return response
116+
117+
# Items sub-resource
118+
@property
119+
def _items_path(self) -> str:
120+
"""
121+
Get the API endpoint path for items in this curation set.
122+
123+
Returns:
124+
str: The full endpoint path for items (e.g., /curation_sets/{name}/items).
125+
"""
126+
return "/".join([self._endpoint_path, "items"])
127+
128+
async def list_items(
129+
self,
130+
*,
131+
limit: typing.Union[int, None] = None,
132+
offset: typing.Union[int, None] = None,
133+
) -> CurationSetListItemResponseSchema:
134+
"""
135+
List items in this curation set.
136+
137+
Args:
138+
limit (Union[int, None], optional): Maximum number of items to return. Defaults to None.
139+
offset (Union[int, None], optional): Number of items to skip. Defaults to None.
140+
141+
Returns:
142+
CurationSetListItemResponseSchema: The list of items in the curation set.
143+
"""
144+
params: typing.Dict[str, typing.Union[int, None]] = {
145+
"limit": limit,
146+
"offset": offset,
147+
}
148+
# Filter out None values to avoid sending them
149+
clean_params: typing.Dict[str, int] = {
150+
k: v for k, v in params.items() if v is not None
151+
}
152+
response: CurationSetListItemResponseSchema = await self.api_call.get(
153+
self._items_path,
154+
as_json=True,
155+
entity_type=CurationSetListItemResponseSchema,
156+
params=clean_params or None,
157+
)
158+
return response
159+
160+
async def get_item(self, item_id: str) -> CurationItemSchema:
161+
"""
162+
Get a specific item from this curation set.
163+
164+
Args:
165+
item_id (str): The ID of the item to retrieve.
166+
167+
Returns:
168+
CurationItemSchema: The item schema.
169+
"""
170+
response: CurationItemSchema = await self.api_call.get(
171+
"/".join([self._items_path, item_id]),
172+
as_json=True,
173+
entity_type=CurationItemSchema,
174+
)
175+
return response
176+
177+
async def upsert_item(
178+
self, item_id: str, item: CurationItemSchema
179+
) -> CurationItemSchema:
180+
"""
181+
Create or update an item in this curation set.
182+
183+
Args:
184+
item_id (str): The ID of the item.
185+
item (CurationItemSchema): The item schema.
186+
187+
Returns:
188+
CurationItemSchema: The created or updated item.
189+
"""
190+
response: CurationItemSchema = await self.api_call.put(
191+
"/".join([self._items_path, item_id]),
192+
body=item,
193+
entity_type=CurationItemSchema,
194+
)
195+
return response
196+
197+
async def delete_item(self, item_id: str) -> CurationItemDeleteSchema:
198+
"""
199+
Delete an item from this curation set.
200+
201+
Args:
202+
item_id (str): The ID of the item to delete.
203+
204+
Returns:
205+
CurationItemDeleteSchema: The deletion response.
206+
"""
207+
response: CurationItemDeleteSchema = await self.api_call.delete(
208+
"/".join([self._items_path, item_id]),
209+
entity_type=CurationItemDeleteSchema,
210+
)
211+
return response
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
"""
2+
This module provides async functionality for managing curation sets in Typesense.
3+
4+
It contains the AsyncCurationSets class, which allows for retrieving and
5+
accessing individual curation sets asynchronously.
6+
7+
Classes:
8+
AsyncCurationSets: Manages curation sets in the Typesense API (async).
9+
10+
Dependencies:
11+
- typesense.async_api_call: Provides the AsyncApiCall class for making async API requests.
12+
- typesense.async_curation_set: Provides the AsyncCurationSet class for individual curation set operations.
13+
- typesense.types.curation_set: Provides CurationSetsListResponseSchema type.
14+
15+
Note: This module uses conditional imports to support both Python 3.11+ and earlier versions.
16+
"""
17+
18+
import sys
19+
20+
if sys.version_info >= (3, 11):
21+
import typing
22+
else:
23+
import typing_extensions as typing
24+
25+
from typesense.async_api_call import AsyncApiCall
26+
from typesense.async_curation_set import AsyncCurationSet
27+
from typesense.types.curation_set import CurationSetsListResponseSchema
28+
29+
30+
class AsyncCurationSets:
31+
"""
32+
Manages curation sets in the Typesense API (async).
33+
34+
This class provides async methods to retrieve and access individual curation sets.
35+
36+
Attributes:
37+
resource_path (str): The API endpoint path for curation sets operations.
38+
api_call (AsyncApiCall): The AsyncApiCall instance for making async API requests.
39+
"""
40+
41+
resource_path: typing.Final[str] = "/curation_sets"
42+
43+
def __init__(self, api_call: AsyncApiCall) -> None:
44+
"""
45+
Initialize the AsyncCurationSets instance.
46+
47+
Args:
48+
api_call (AsyncApiCall): The AsyncApiCall instance for making async API requests.
49+
"""
50+
self.api_call = api_call
51+
52+
async def retrieve(self) -> CurationSetsListResponseSchema:
53+
"""
54+
Retrieve all curation sets.
55+
56+
Returns:
57+
CurationSetsListResponseSchema: The list of all curation sets.
58+
59+
Example:
60+
>>> curation_sets = AsyncCurationSets(async_api_call)
61+
>>> all_sets = await curation_sets.retrieve()
62+
>>> for set in all_sets:
63+
... print(set["name"])
64+
"""
65+
response: CurationSetsListResponseSchema = await self.api_call.get(
66+
AsyncCurationSets.resource_path,
67+
as_json=True,
68+
entity_type=CurationSetsListResponseSchema,
69+
)
70+
return response
71+
72+
def __getitem__(self, curation_set_name: str) -> AsyncCurationSet:
73+
"""
74+
Get or create an AsyncCurationSet instance for a given curation set name.
75+
76+
This method allows accessing curation sets using dictionary-like syntax.
77+
If the AsyncCurationSet instance doesn't exist, it creates a new one.
78+
79+
Args:
80+
curation_set_name (str): The name of the curation set.
81+
82+
Returns:
83+
AsyncCurationSet: The AsyncCurationSet instance for the specified name.
84+
85+
Example:
86+
>>> curation_sets = AsyncCurationSets(async_api_call)
87+
>>> products_set = curation_sets["products"]
88+
"""
89+
from typesense.async_curation_set import AsyncCurationSet as PerSet
90+
91+
return PerSet(self.api_call, curation_set_name)

0 commit comments

Comments
 (0)