-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathterms.py
More file actions
297 lines (232 loc) · 11.9 KB
/
terms.py
File metadata and controls
297 lines (232 loc) · 11.9 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
"""This class takes a base URL as an argument when it's initialized,
which is the endpoint for the RESTFUL API that we'll be interacting with.
The create(), read(), update(), and delete() methods each correspond to
the CRUD operations that can be performed on the API """
import json
from ..common import Parameter
from .._errors import ArgumentException
class Terms(Parameter):
"""
This class takes a base URL as an argument when it's initialized,
which is the endpoint for the RESTFUL API that
we'll be interacting with. The create(), read(), update(), and delete()
methods each correspond to the CRUD
operations that can be performed on the API """
def __init__(self, client, taxonomy_uid: str, terms_uid: str):
self.client = client
self.taxonomy_uid = taxonomy_uid
self.terms_uid = terms_uid
super().__init__(self.client)
self.path = f"taxonomies/{self.taxonomy_uid}/terms"
def find(self):
"""
This call fetches the list of all terms available for a taxonomies.
:return: Json, with taxonomy details.
-------------------------------
[Example:]
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack("api_key").taxonomy("taxonomy_uid").terms().find()
-------------------------------
"""
return self.client.get(self.path, headers = self.client.headers, params = self.params)
def fetch(self, terms_uid: str = None):
"""
The Get a terms call returns information about a specific taxonomy available on the stack.
:param terms_uid: The `terms_uid` parameter is a string that represents the unique identifier of
the terms you want to fetch. It is an optional parameter, meaning it can be None or an empty
string if you don't want to specify a specific terms_uid
:type terms_uid: str
:return: the result of the GET request made to the specified URL.
-------------------------------
[Example:]
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack('api_key').taxonomy('taxonomy_uid').terms('terms_uid').fetch()
-------------------------------
"""
if terms_uid is not None and terms_uid != '':
self.terms_uid = terms_uid
self.validate_taxonomy_uid()
self.validate_terms_uid()
url = f"{self.path}/{self.terms_uid}"
return self.client.get(url, headers = self.client.headers, params = self.params)
def create(self, data: dict):
"""
This call lets you add a new terms to your taxonomy.
:param data: The `data` parameter is a dictionary that contains the data to be sent in the
request body. It will be converted to a JSON string using the `json.dumps()` function before
being sent in the request
:type data: dict
:return: The code is returning the result of the `post` method call on the `self.client` object.
-------------------------------
[Example:]
>>> data ={
>>> "term": {
>>> "uid": "term_1",
>>> "name": "Term 1"
>>> },
>>> "parent_uid": null
>>> }
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack('api_key').taxonomy('taxonomy_uid').terms().create(data)
-------------------------------
"""
data = json.dumps(data)
return self.client.post(self.path, headers = self.client.headers, data=data, params = self.params)
def update(self, data: dict, terms_uid: str = None):
"""
The function updates a resource with the given data and terms UID.
:param data: A dictionary containing the data to be updated
:type data: dict
:param terms_uid: The `terms_uid` parameter is a string that represents the unique identifier of
the terms you want to update
:type terms_uid: str
:return: the result of the `self.client.put()` method, which is the response from making a PUT
request to the specified URL with the provided data and headers.
-------------------------------
[Example:]
>>> data ={
>>> "term": {
>>> "name": "Term 1"
>>> }
>>> }
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack('api_key').taxonomy("taxonomy_uid").terms('terms_uid').update(data)
-------------------------------
"""
if terms_uid is not None and terms_uid != '':
self.terms_uid = terms_uid
self.validate_taxonomy_uid()
url = url = f"{self.path}/{self.terms_uid}"
data = json.dumps(data)
return self.client.put(url, headers = self.client.headers, data=data, params = self.params)
def delete(self, terms_uid: str = None):
"""
The Delete terms call deletes an existing terms from your taxonomy.
:param terms_uid: The `terms_uid` parameter is a string that represents the unique identifier of
the terms you want to delete
:type terms_uid: str
:return: the result of the delete request made to the specified URL.
-------------------------------
[Example:]
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = result = client.stack('api_key').taxonomy('taxonomy_uid').terms('terms_uid').delete('taxonomy_uid')
-------------------------------
"""
if terms_uid is not None and terms_uid != '':
self.terms_uid = terms_uid
self.validate_taxonomy_uid()
self.validate_terms_uid()
url = f"{self.path}/{self.terms_uid}"
return self.client.delete(url, headers = self.client.headers, params = self.params)
def search(self, term_string: str):
"""
The "Get a terms" call returns information about a specified terms available on the taxonomy.
:param term_string: The term_string parameter is a string that represents the term you want to
search for. It is used to specify the term you want to search for in the search function
:type term_string: str
:return:Json, with taxonomy details.
-------------------------------
[Example:]
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack('api_key').taxonomy('taxonomy_uid').terms('terms_uid').search("terms_string")
-------------------------------
"""
self.validate_taxonomy_uid()
self.validate_term_string(term_string)
Parameter.add_param(self, "term", term_string)
return self.client.get(self.path, headers = self.client.headers, params = self.params)
def move(self, data: dict, terms_uid: str = None):
"""
The "Move terms" call will let you update the details
:param data: The `data` parameter is a dictionary that contains the data to be sent in the
request body. It will be converted to a JSON string before sending the request
:type data: dict
:param terms_uid: The `terms_uid` parameter is a string that represents the unique identifier of
the terms you want to move
:type terms_uid: str
:return: Json, with updated taxonomy details.
-------------------------------
[Example:]
>>> data ={
>>> "term": {
>>> "uid": "term_1"
>>> },
>>> "parent_uid": null
>>> }
>>> Under an existing Term:
>>> {
>>> "term": {
>>> "uid": "term_3"
>>> },
>>> "parent_uid": "term_1"
>>> }
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack('api_key').taxonomy("taxonomy_uid").terms('terms_uid').move(data).json()
-------------------------------
"""
if terms_uid is not None and terms_uid != '':
self.terms_uid = terms_uid
self.validate_taxonomy_uid()
self.validate_terms_uid()
url = f"{self.path}/{self.terms_uid}"
data = json.dumps(data)
return self.client.put(url, headers = self.client.headers, data=data, params = self.params)
def ancestors(self, terms_uid: str = None):
"""
The "Get a ancestors terms" call returns information about a specific terms available on the taxonomy.
:param terms_uid: The `terms_uid` parameter is a string that represents the unique identifier of
a term in a taxonomy. It is used to specify the term for which you want to retrieve the
ancestors
:type terms_uid: str
:return: The code is returning the result of a GET request to the specified URL, which is the
ancestors of the terms with the given terms_uid.
-------------------------------
[Example:]
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack('api_key').taxonomy('taxonomy_uid').terms('terms_uid').ancestors().json()
-------------------------------
"""
if terms_uid is not None and terms_uid != '':
self.terms_uid = terms_uid
self.validate_taxonomy_uid()
self.validate_terms_uid()
url = f"{self.path}/{self.terms_uid}/ancestors"
return self.client.get(url, headers = self.client.headers, params = self.params)
def descendants(self, terms_uid: str = None):
"""
The "Get a descendants terms" call returns information about a specific terms available on the taxonomy.
:param terms_uid: The `terms_uid` parameter is a string that represents the unique identifier of
a term in a taxonomy. It is used to specify the term for which you want to retrieve its
descendants
:type terms_uid: str
:return: the result of a GET request to the specified URL.
-------------------------------
[Example:]
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack('api_key').taxonomy('taxonomy_uid').terms('terms_uid').descendants().json()
-------------------------------
"""
if terms_uid is not None and terms_uid != '':
self.terms_uid = terms_uid
self.validate_taxonomy_uid()
self.validate_terms_uid()
url = f"{self.path}/{self.terms_uid}/descendants"
return self.client.get(url, headers = self.client.headers, params = self.params)
def validate_taxonomy_uid(self):
if self.taxonomy_uid is None or '':
raise ArgumentException('Taxonomy Uid is required')
def validate_terms_uid(self):
if self.terms_uid is None or '':
raise ArgumentException('Terms Uid is required')
def validate_term_string(self, term_string):
if term_string is None or '':
raise ArgumentException('Term String is required')