|
| 1 | +"""Client for single Curation Set operations, including items APIs.""" |
| 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.api_call import ApiCall |
| 11 | +from typesense.types.curation_set import ( |
| 12 | + CurationSetSchema, |
| 13 | + CurationSetDeleteSchema, |
| 14 | + CurationSetUpsertSchema, |
| 15 | + CurationSetListItemResponseSchema, |
| 16 | + CurationItemSchema, |
| 17 | + CurationItemDeleteSchema, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +class CurationSet: |
| 22 | + def __init__(self, api_call: ApiCall, name: str) -> None: |
| 23 | + self.api_call = api_call |
| 24 | + self.name = name |
| 25 | + |
| 26 | + @property |
| 27 | + def _endpoint_path(self) -> str: |
| 28 | + from typesense.curation_sets import CurationSets |
| 29 | + |
| 30 | + return "/".join([CurationSets.resource_path, self.name]) |
| 31 | + |
| 32 | + def retrieve(self) -> CurationSetSchema: |
| 33 | + response: CurationSetSchema = self.api_call.get( |
| 34 | + self._endpoint_path, |
| 35 | + as_json=True, |
| 36 | + entity_type=CurationSetSchema, |
| 37 | + ) |
| 38 | + return response |
| 39 | + |
| 40 | + def delete(self) -> CurationSetDeleteSchema: |
| 41 | + response: CurationSetDeleteSchema = self.api_call.delete( |
| 42 | + self._endpoint_path, |
| 43 | + entity_type=CurationSetDeleteSchema, |
| 44 | + ) |
| 45 | + return response |
| 46 | + |
| 47 | + # Items sub-resource |
| 48 | + @property |
| 49 | + def _items_path(self) -> str: |
| 50 | + return "/".join([self._endpoint_path, "items"]) # /curation_sets/{name}/items |
| 51 | + |
| 52 | + def list_items( |
| 53 | + self, |
| 54 | + *, |
| 55 | + limit: typing.Union[int, None] = None, |
| 56 | + offset: typing.Union[int, None] = None, |
| 57 | + ) -> CurationSetListItemResponseSchema: |
| 58 | + params: typing.Dict[str, typing.Union[int, None]] = { |
| 59 | + "limit": limit, |
| 60 | + "offset": offset, |
| 61 | + } |
| 62 | + # Filter out None values to avoid sending them |
| 63 | + clean_params: typing.Dict[str, int] = { |
| 64 | + k: v for k, v in params.items() if v is not None # type: ignore[dict-item] |
| 65 | + } |
| 66 | + response: CurationSetListItemResponseSchema = self.api_call.get( |
| 67 | + self._items_path, |
| 68 | + as_json=True, |
| 69 | + entity_type=CurationSetListItemResponseSchema, |
| 70 | + params=clean_params or None, |
| 71 | + ) |
| 72 | + return response |
| 73 | + |
| 74 | + def get_item(self, item_id: str) -> CurationItemSchema: |
| 75 | + response: CurationItemSchema = self.api_call.get( |
| 76 | + "/".join([self._items_path, item_id]), |
| 77 | + as_json=True, |
| 78 | + entity_type=CurationItemSchema, |
| 79 | + ) |
| 80 | + return response |
| 81 | + |
| 82 | + def upsert_item(self, item_id: str, item: CurationItemSchema) -> CurationItemSchema: |
| 83 | + response: CurationItemSchema = self.api_call.put( |
| 84 | + "/".join([self._items_path, item_id]), |
| 85 | + body=item, |
| 86 | + entity_type=CurationItemSchema, |
| 87 | + ) |
| 88 | + return response |
| 89 | + |
| 90 | + def delete_item(self, item_id: str) -> CurationItemDeleteSchema: |
| 91 | + response: CurationItemDeleteSchema = self.api_call.delete( |
| 92 | + "/".join([self._items_path, item_id]), |
| 93 | + entity_type=CurationItemDeleteSchema, |
| 94 | + ) |
| 95 | + return response |
| 96 | + |
| 97 | + |
0 commit comments