-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtaxonomy.py
More file actions
155 lines (117 loc) · 5.83 KB
/
taxonomy.py
File metadata and controls
155 lines (117 loc) · 5.83 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
"""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
from ..terms.terms import Terms
class Taxonomy(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):
self.client = client
self.taxonomy_uid = taxonomy_uid
super().__init__(self.client)
self.path = "taxonomies"
def find(self):
"""
This call fetches the list of all taxonomies available for a stack.
:return: Json, with taxonomy details.
-------------------------------
[Example:]
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack("api_key").taxonomy().find().json()
-------------------------------
"""
return self.client.get(self.path, headers = self.client.headers, params = self.params)
def fetch(self, taxonomy_uid: str = None):
"""
The "Get a taxonomy" call returns information about a specific taxonomy available on the stack.
:return: Json, with taxonomy details.
-------------------------------
[Example:]
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack('api_key').taxonomy('taxonomy_uid').fetch('taxonomy_uid').json()
-------------------------------
"""
if taxonomy_uid is not None and taxonomy_uid != '':
self.taxonomy_uid = taxonomy_uid
self.validate_taxonomy_uid()
url = f"{self.path}/{self.taxonomy_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 taxonomy to your stack.
:param data: The `data` parameter is the payload that you want to send in the request body. It
should be a dictionary or a JSON serializable object that you want to send as the request body
:return: Json, with taxonomy details.
-------------------------------
[Example:]
>>> data ={
>>> "taxonomy": {
>>> "uid": "taxonomy12345",
>>> "name": "Taxonomy 12345",
>>> "description": "Description for Taxonomy 1"
>>> }
>>> }
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack('api_key').taxonomy().create(data).json()
-------------------------------
"""
data = json.dumps(data)
return self.client.post(self.path, headers = self.client.headers, data=data, params = self.params)
def update(self, data: dict, taxonomy_uid: str = None):
"""
The "Update taxonomy" call will let you update the details
:param data: The `data` parameter is the data that you want to update. It should be a dictionary
or an object that can be serialized to JSON
:return: Json, with updated taxonomy details.
-------------------------------
[Example:]
>>> data ={
>>> "taxonomy": {
>>> "name": "Taxonomy 12345",
>>> "description": "Description updated for Taxonomy 12345"
>>> }
>>> }
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack('api_key').taxonomy("taxonomy_uid").update(data).json()
-------------------------------
"""
if taxonomy_uid is not None and taxonomy_uid != '':
self.taxonomy_uid = taxonomy_uid
self.validate_taxonomy_uid()
url = f"{self.path}/{self.taxonomy_uid}"
data = json.dumps(data)
return self.client.put(url, headers = self.client.headers, data=data, params = self.params)
def delete(self, taxonomy_uid: str = None):
"""
The "Delete taxonomy" call deletes an existing taxonomy from your stack.
:return: The delete() method returns the status code and message as a response.
-------------------------------
[Example:]
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = result = client.stack('api_key').taxonomy('taxonomy_uid').delete('taxonomy_uid').json()
-------------------------------
"""
if taxonomy_uid is not None and taxonomy_uid != '':
self.taxonomy_uid = taxonomy_uid
self.validate_taxonomy_uid()
url = f"{self.path}/{self.taxonomy_uid}"
return self.client.delete(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 terms(self, terms_uid: str = None):
self.validate_taxonomy_uid()
return Terms(self.client, self.taxonomy_uid, terms_uid)