-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebhook.py
More file actions
324 lines (248 loc) · 13.1 KB
/
webhook.py
File metadata and controls
324 lines (248 loc) · 13.1 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
"""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
class Webhook(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, webhook_uid):
self.client = client
self.webhook_uid = webhook_uid
super().__init__(self.client)
self.path = "webhooks"
def find(self):
"""
The Get all Webhooks request returns comprehensive information on all the available webhooks in the specified stack
:return: the result of a GET request to the specified URL, using the headers specified in the
client object.The URL being used for the API call is "webhooks". The headers and parameters
for the API call are being passed as arguments to the `get` method. The result of the API call
is being returned.
-------------------------------
[Example:]
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack("api_key").webhooks().find().json()
-------------------------------
"""
url = self.path
return self.client.get(url, headers = self.client.headers, params = self.params)
def fetch(self):
"""
The `Get webhook` request returns comprehensive information on a specific webhook.
:return: The fetch method returns the response from the Get an webhook request, which contains
comprehensive information about a specific version of an webhook of a stack.
-------------------------------
[Example:]
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack('api_key').webhooks('webhook_uid').fetch().json()
-------------------------------
"""
if self.webhook_uid is None:
raise Exception('Webhook uid is required')
url = f"{self.path}/{self.webhook_uid}"
return self.client.get(url, headers = self.client.headers, params = self.params)
def create(self, data):
"""
The Create a webhook request allows you to create a new webhook in a specific stack.
:param data: In the `data` section, you need to enter the name of the webhook; the destination details i.e.,
target urls, basic authentication details, and custom headers;
and the channels; and set the disabled and concise_payload parameters as per requirement.
:return: Json, with webhook details.
-------------------------------
[Example:]
>>> data = {
>>> "webhook":{
>>> "name":"Test",
>>> "destinations":[
>>> {
>>> "target_url":"http://example.com",
>>> "http_basic_auth":"basic",
>>> "http_basic_password":"test",
>>> "custom_header":[
>>> {
>>> "header_name":"Custom",
>>> "value":"testing"
>>> }
>>> ]
>>> }
>>> ],
>>> "notifiers": "dave.joe@gmail.com",
>>> "channels":[
>>> "assets.create"
>>> ],
>>> "branches":[
>>> "main"
>>> ],
>>> "retry_policy":"manual",
>>> "disabled":false,
>>> "concise_payload":true
>>> }
>>> }
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack('api_key').webhooks().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):
"""
The Update webhook request allows you to update the details of an existing webhook in the stack.
:param data: In the data section, you need to enter new details such as the name of the webhook; the destination details
i.e., target urls, basic authentication details, and custom headers; and the channels; or reset the disabled or concise_payload parameters as per requirement
:return: Json, with updated webhook details.
-------------------------------
[Example:]
>>>
>>> data = {
>>> "webhook":{
>>> "name":"Updated webhook",
>>> "destinations":[
>>> {
>>> "target_url":"http://example.com",
>>> "http_basic_auth":"basic",
>>> "http_basic_password":"test",
>>> "custom_header":[
>>> {
>>> "header_name":"Custom",
>>> "value":"testing"
>>> }
>>> ]
>>> }
>>> ],
>>> "channels":[
>>> "assets.create"
>>> ],
>>> "branches":[
>>> "main"
>>> ],
>>> "retry_policy":"manual",
>>> "disabled":true,
>>> "concise_payload":true
>>> }
>>> }
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack('api_key').webhooks('webhook_uid').update(data).json()
-------------------------------
"""
if self.webhook_uid is None:
raise Exception('Webhook uid is required')
url = f"{self.path}/{self.webhook_uid}"
data = json.dumps(data)
return self.client.put(url, headers = self.client.headers, data=data, params = self.params)
def delete(self):
"""
The Delete webhook call deletes an existing webhook from a 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').webhooks('webhook_uid').delete().json()
-------------------------------
"""
if self.webhook_uid is None:
raise Exception('Webhook uid is required')
if self.client.headers['Content-Type'] is not None:
self.client.headers.pop('Content-Type')
url = f"{self.path}/{self.webhook_uid}"
return self.client.delete(url, headers = self.client.headers, params = self.params)
def imports(self, file_path):
"""
The 'Import Webhook' section consists of the following two requests that will help you to
import new Webhooks or update existing ones by uploading JSON files.
:param file_path: The `file_path` parameter is a string that represents the path to the file
that you want to import. It should be the absolute or relative path to the file on your local
machine
:return: The imports() method returns the status code and message as a response.
-------------------------------
[Example:]
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> file_path = "tests/resources/mock_content_types/import_content_types.json"
>>> result = client.stack('api_key').webhooks().imports(file_path).json()
-------------------------------
"""
if file_path is None:
raise Exception('File path is required')
url = f"{self.path}/import"
self.client.headers['Content-Type'] = "multipart/form-data"
files = {'entry': open(f"{file_path}",'rb')}
return self.client.post(url, headers = self.client.headers, files = files, params = self.params)
def export(self):
"""
The Export a Webhook request exports an existing webhook.
The exported webhook data is saved in a downloadable JSON file.
The exported file won't get downloaded automatically.
To download the exported file, a REST API client, such as Postman can be used.
:return: Json, with webhook details.
-------------------------------
[Example:]
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack('api_key').webhooks('webhook_uid').export().json()
-------------------------------
"""
if self.webhook_uid is None:
raise Exception('Webhok uid is required')
url = f"{self.path}/{self.webhook_uid}/export"
return self.client.get(url, headers = self.client.headers, params = self.params)
def executions(self):
"""
The Get executions of a webhook request allows you to fetch the execution details of a specific webhook,
which includes the execution UID. These details are instrumental in retrieving webhook logs and retrying a failed webhook.
:return: Json, with webhook details.
-------------------------------
[Example:]
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack('api_key').webhooks('webhook_execution_uid').executions().json()
-------------------------------
"""
if self.webhook_uid is None:
raise Exception('Webhook uid is required')
url = f"{self.path}/{self.webhook_uid}/executions"
return self.client.get(url, headers = self.client.headers, params = self.params)
def retry(self, execution_uid):
"""
This call makes a manual attempt to execute a webhook after the webhook has finished executing its automatic attempts.
:param execution_uid: The `execution_uid` parameter is a unique identifier for a specific
execution. It is used to identify which execution should be retried
:return: Json, with webhook details.
-------------------------------
[Example:]
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack('api_key').webhooks().retry('execution_uid').json()
-------------------------------
"""
if execution_uid is None:
raise Exception('Execution uid is required')
url = f"{self.path}/{execution_uid}/retry"
return self.client.post(url, headers = self.client.headers, params = self.params)
def logs(self, execution_uid):
"""
Get latest execution log of a webhook call will return a comprehensive detail of all the webhooks
that were executed at a particular execution cycle.
:param execution_uid: The `execution_uid` parameter is a unique identifier for a specific
execution. It is used to identify which execution should be retried
:return: Json, with webhook details.
-------------------------------
[Example:]
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack('api_key').webhooks().logs('execution_uid').json()
-------------------------------
"""
if execution_uid is None:
raise Exception('Execution uid is required')
url = f"{self.path}/{execution_uid}/logs"
return self.client.get(url, headers = self.client.headers, params = self.params)