-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathglobal_fields.py
More file actions
189 lines (164 loc) · 7.65 KB
/
global_fields.py
File metadata and controls
189 lines (164 loc) · 7.65 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
"""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 contentstack_management.common import Parameter
_path = 'global_fields'
class GlobalFields(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, global_field_uid=None, options=None):
self.client = client
self.global_field_uid = global_field_uid
self.options = options
super().__init__(self.client)
if self.options and 'api_version' in self.options:
Parameter.add_header(self, 'api_version', str(self.options['api_version']))
def find(self):
"""
Find the global fields entries
:return: Json, with global fields details.
-------------------------------
[Example:]
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack("api_key").global_fields('global_field_uid').find().json()
-------------------------------
"""
response = self.client.get(_path, headers=self.client.headers, params = self.params)
# Remove the api_version header after request
if self.options and 'api_version' in self.options:
self.client.headers.pop('api_version', None)
return response
def fetch(self):
"""
Fetches the global fields entry
:return: Json, with global fields details.
-------------------------------
[Example:]
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack('api_key').global_fields('global_field_uid').fetch().json()
-------------------------------
"""
url = f"{_path}/{self.global_field_uid}"
response = self.client.get(url, headers=self.client.headers, params = self.params)
# Remove the api_version header after request
if self.options and 'api_version' in self.options:
self.client.headers.pop('api_version', None)
return response
def create(self, data):
"""
Create the global fields entries
:return: Json, with global fields details.
-------------------------------
[Example:]
>>> data = {
>>> "global_field": {
>>> "title": "Servlet",
>>> "uid": "servlet",
>>> "schema": [{
>>> "display_name": "Name",
>>> "uid": "name",
>>> "data_type": "text"
>>> }
>>> }
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack('api_key').global_fields().create(data)
-------------------------------
"""
data = json.dumps(data)
response = self.client.post(_path, headers=self.client.headers, data=data, params = self.params)
# Remove the api_version header after request
if self.options and 'api_version' in self.options:
self.client.headers.pop('api_version', None)
return response
def update(self, data):
"""
Update the global fields entries
:return: Json, with global fields details.
-------------------------------
[Example:]
>>> data = {
>>> "global_field": {
>>> "title": "Servlet",
>>> "uid": "servlet",
>>> "schema": [{
>>> "display_name": "Name",
>>> "uid": "name",
>>> "data_type": "text"
>>> }
>>> }
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken="authtoken")
>>> result = client.stack('api_key').global_fields('global_field_uid').update(data)
-------------------------------
"""
url = f"{_path}/{self.global_field_uid}"
data = json.dumps(data)
response = self.client.put(url, headers=self.client.headers, params=self.params, data=data)
# Remove the api_version header after request
if self.options and 'api_version' in self.options:
self.client.headers.pop('api_version', None)
return response
def delete(self):
"""
Delete the global fields
:return: Json, with status code and message.
-------------------------------
[Example:]
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken="authtoken")
>>> result = client.stack('api_key').global_fields('global_field_uid').delete()
-------------------------------
"""
url = f"{_path}/{self.global_field_uid}"
response = self.client.delete(url, headers=self.client.headers, params=self.params)
# Remove the api_version header after request
if self.options and 'api_version' in self.options:
self.client.headers.pop('api_version', None)
return response
def imports(self, file_path):
"""
Import the global fields
:return: Json, with global fields details.
-------------------------------
[Example:]
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken="authtoken")
>>> path = "tests/resources/mock_global_fields/import_global_fields.json"
>>> result = client.stack('api_key').global_fields().imports(path)
-------------------------------
"""
self.client.headers['Content-Type'] = "multipart/form-data"
files = {'global_field': open(f"{file_path}", 'rb')}
response = self.client.post('global_fields/import', headers=self.client.headers, params=self.params, files=files)
# Remove the api_version header after request
if self.options and 'api_version' in self.options:
self.client.headers.pop('api_version', None)
return response
def export(self):
"""
Export the global fields
:return: Json, with global fields details.
-------------------------------
[Example:]
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack('api_key').global_fields().export().json()
-------------------------------
"""
if self.global_field_uid is None or '':
raise Exception('global_field_uid is required')
url = f"{_path}/{self.global_field_uid}/export"
response = self.client.get(url, headers=self.client.headers, params=self.params)
# Remove the api_version header after request
if self.options and 'api_version' in self.options:
self.client.headers.pop('api_version', None)
return response