|
| 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 |
0 commit comments