forked from typesense/typesense-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcuration_set.py
More file actions
106 lines (91 loc) · 3.21 KB
/
curation_set.py
File metadata and controls
106 lines (91 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""Client for single Curation Set operations, including items APIs."""
import sys
if sys.version_info >= (3, 11):
import typing
else:
import typing_extensions as typing
from typesense.api_call import ApiCall
from typesense.types.curation_set import (
CurationItemDeleteSchema,
CurationItemSchema,
CurationSetDeleteSchema,
CurationSetListItemResponseSchema,
CurationSetSchema,
CurationSetUpsertSchema,
)
class CurationSet:
def __init__(self, api_call: ApiCall, name: str) -> None:
self.api_call = api_call
self.name = name
@property
def _endpoint_path(self) -> str:
from typesense.curation_sets import CurationSets
return "/".join([CurationSets.resource_path, self.name])
def retrieve(self) -> CurationSetSchema:
response: CurationSetSchema = self.api_call.get(
self._endpoint_path,
as_json=True,
entity_type=CurationSetSchema,
)
return response
def delete(self) -> CurationSetDeleteSchema:
response: CurationSetDeleteSchema = self.api_call.delete(
self._endpoint_path,
entity_type=CurationSetDeleteSchema,
)
return response
def upsert(
self,
payload: CurationSetUpsertSchema,
) -> CurationSetSchema:
response: CurationSetSchema = self.api_call.put(
"/".join([self._endpoint_path]),
body=payload,
entity_type=CurationSetSchema,
)
return response
# Items sub-resource
@property
def _items_path(self) -> str:
return "/".join([self._endpoint_path, "items"]) # /curation_sets/{name}/items
def list_items(
self,
*,
limit: typing.Union[int, None] = None,
offset: typing.Union[int, None] = None,
) -> CurationSetListItemResponseSchema:
params: typing.Dict[str, typing.Union[int, None]] = {
"limit": limit,
"offset": offset,
}
# Filter out None values to avoid sending them
clean_params: typing.Dict[str, int] = {
k: v for k, v in params.items() if v is not None
}
response: CurationSetListItemResponseSchema = self.api_call.get(
self._items_path,
as_json=True,
entity_type=CurationSetListItemResponseSchema,
params=clean_params or None,
)
return response
def get_item(self, item_id: str) -> CurationItemSchema:
response: CurationItemSchema = self.api_call.get(
"/".join([self._items_path, item_id]),
as_json=True,
entity_type=CurationItemSchema,
)
return response
def upsert_item(self, item_id: str, item: CurationItemSchema) -> CurationItemSchema:
response: CurationItemSchema = self.api_call.put(
"/".join([self._items_path, item_id]),
body=item,
entity_type=CurationItemSchema,
)
return response
def delete_item(self, item_id: str) -> CurationItemDeleteSchema:
response: CurationItemDeleteSchema = self.api_call.delete(
"/".join([self._items_path, item_id]),
entity_type=CurationItemDeleteSchema,
)
return response