|
| 1 | +"""Client for single Synonym Set operations (async).""" |
| 2 | + |
| 3 | +import sys |
| 4 | + |
| 5 | +if sys.version_info >= (3, 11): |
| 6 | + import typing |
| 7 | +else: |
| 8 | + import typing_extensions as typing |
| 9 | + |
| 10 | +from typesense.async_api_call import AsyncApiCall |
| 11 | +from typesense.types.synonym_set import ( |
| 12 | + SynonymItemDeleteSchema, |
| 13 | + SynonymItemSchema, |
| 14 | + SynonymSetCreateSchema, |
| 15 | + SynonymSetDeleteSchema, |
| 16 | + SynonymSetRetrieveSchema, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +class AsyncSynonymSet: |
| 21 | + def __init__(self, api_call: AsyncApiCall, name: str) -> None: |
| 22 | + self.api_call = api_call |
| 23 | + self.name = name |
| 24 | + |
| 25 | + @property |
| 26 | + def _endpoint_path(self) -> str: |
| 27 | + from typesense.async_synonym_sets import AsyncSynonymSets |
| 28 | + |
| 29 | + return "/".join([AsyncSynonymSets.resource_path, self.name]) |
| 30 | + |
| 31 | + async def retrieve(self) -> SynonymSetRetrieveSchema: |
| 32 | + response: SynonymSetRetrieveSchema = await self.api_call.get( |
| 33 | + self._endpoint_path, |
| 34 | + as_json=True, |
| 35 | + entity_type=SynonymSetRetrieveSchema, |
| 36 | + ) |
| 37 | + return response |
| 38 | + |
| 39 | + async def upsert(self, set: SynonymSetCreateSchema) -> SynonymSetCreateSchema: |
| 40 | + response: SynonymSetCreateSchema = await self.api_call.put( |
| 41 | + self._endpoint_path, |
| 42 | + entity_type=SynonymSetCreateSchema, |
| 43 | + body=set, |
| 44 | + ) |
| 45 | + return response |
| 46 | + |
| 47 | + async def delete(self) -> SynonymSetDeleteSchema: |
| 48 | + response: SynonymSetDeleteSchema = await self.api_call.delete( |
| 49 | + self._endpoint_path, |
| 50 | + entity_type=SynonymSetDeleteSchema, |
| 51 | + ) |
| 52 | + return response |
| 53 | + |
| 54 | + @property |
| 55 | + def _items_path(self) -> str: |
| 56 | + return "/".join([self._endpoint_path, "items"]) # /synonym_sets/{name}/items |
| 57 | + |
| 58 | + async def list_items( |
| 59 | + self, |
| 60 | + *, |
| 61 | + limit: typing.Union[int, None] = None, |
| 62 | + offset: typing.Union[int, None] = None, |
| 63 | + ) -> typing.List[SynonymItemSchema]: |
| 64 | + params: typing.Dict[str, typing.Union[int, None]] = { |
| 65 | + "limit": limit, |
| 66 | + "offset": offset, |
| 67 | + } |
| 68 | + clean_params: typing.Dict[str, int] = { |
| 69 | + k: v for k, v in params.items() if v is not None |
| 70 | + } |
| 71 | + response: typing.List[SynonymItemSchema] = await self.api_call.get( |
| 72 | + self._items_path, |
| 73 | + as_json=True, |
| 74 | + entity_type=typing.List[SynonymItemSchema], |
| 75 | + params=clean_params or None, |
| 76 | + ) |
| 77 | + return response |
| 78 | + |
| 79 | + async def get_item(self, item_id: str) -> SynonymItemSchema: |
| 80 | + response: SynonymItemSchema = await self.api_call.get( |
| 81 | + "/".join([self._items_path, item_id]), |
| 82 | + as_json=True, |
| 83 | + entity_type=SynonymItemSchema, |
| 84 | + ) |
| 85 | + return response |
| 86 | + |
| 87 | + async def upsert_item( |
| 88 | + self, item_id: str, item: SynonymItemSchema |
| 89 | + ) -> SynonymItemSchema: |
| 90 | + response: SynonymItemSchema = await self.api_call.put( |
| 91 | + "/".join([self._items_path, item_id]), |
| 92 | + body=item, |
| 93 | + entity_type=SynonymItemSchema, |
| 94 | + ) |
| 95 | + return response |
| 96 | + |
| 97 | + async def delete_item(self, item_id: str) -> SynonymItemDeleteSchema: |
| 98 | + # API returns {"id": "..."} for delete; openapi defines SynonymItemDeleteResponse with name but for items it's id |
| 99 | + response: SynonymItemDeleteSchema = await self.api_call.delete( |
| 100 | + "/".join([self._items_path, item_id]), entity_type=SynonymItemDeleteSchema |
| 101 | + ) |
| 102 | + return response |
0 commit comments