1
|
|
|
import requests |
2
|
|
|
import json |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
class Utils(object): |
6
|
|
|
def __init__(self): |
7
|
|
|
self.api_url = '%s/api/v1' % self.readConfig()['api_url'] |
8
|
|
|
self.api_token = self.readConfig()['api_token'] |
9
|
|
|
|
10
|
|
|
def readConfig(self): |
11
|
|
|
# Open Json File |
12
|
|
|
with open('settings/config.json', 'r') as json_data: |
13
|
|
|
return json.load(json_data) |
14
|
|
|
|
15
|
|
|
def __getRequest(self, path): |
16
|
|
|
return requests.get(self.api_url + path) |
17
|
|
|
|
18
|
|
|
def __postRequest(self, path, data): |
19
|
|
|
return requests.post(self.api_url + path, data, headers={'X-Cachet-Token': self.api_token}) |
20
|
|
|
|
21
|
|
|
def __putRequest(self, path, data): |
22
|
|
|
return requests.put(self.api_url + path, data, headers={'X-Cachet-Token': self.api_token}) |
23
|
|
|
|
24
|
|
|
def __delRequest(self, path): |
25
|
|
|
return requests.delete(self.api_url + path, headers={'X-Cachet-Token': self.api_token}) |
26
|
|
|
|
27
|
|
|
def ping(self): |
28
|
|
|
return self.__getRequest('/ping') |
29
|
|
|
|
30
|
|
|
def getComponents(self): |
31
|
|
|
return self.__getRequest('/components') |
32
|
|
|
|
33
|
|
|
def getComponentsByID(self, c_id): |
34
|
|
|
return self.__getRequest('/components/%s' % c_id) |
35
|
|
|
|
36
|
|
|
def postComponents(self, name, status, **kwargs): |
37
|
|
|
""" |
38
|
|
|
Create a new component. |
39
|
|
|
|
40
|
|
|
:param name: Name of the component |
41
|
|
|
:param status: Status of the component; 1-4 |
42
|
|
|
:param description: (optional) Description of the component |
43
|
|
|
:param link: (optional) A hyperlink to the component |
44
|
|
|
:param order: (optional) Order of the component |
45
|
|
|
:param group_id: (optional) The group id that the component is within |
46
|
|
|
:param enabled: (optional) |
47
|
|
|
:return: :class:`Response <Response>` object |
48
|
|
|
:rtype: requests.Response |
49
|
|
|
""" |
50
|
|
|
|
51
|
|
|
kwargs['name'] = name |
52
|
|
|
kwargs['status'] = status |
53
|
|
|
return self.__postRequest('/components', kwargs) # |
54
|
|
|
|
55
|
|
|
def putComponentsByID(self, c_id, **kwargs): |
56
|
|
|
""" |
57
|
|
|
Updates a component. |
58
|
|
|
|
59
|
|
|
:param id: Component ID |
60
|
|
|
:param name: (optional) Name of the component |
61
|
|
|
:param status: (optional) Status of the component; 1-4 |
62
|
|
|
:param link: (optional) A hyperlink to the component |
63
|
|
|
:param order: (optional) Order of the component |
64
|
|
|
:param group_id: (optional) The group id that the component is within |
65
|
|
|
:return: :class:`Response <Response>` object |
66
|
|
|
:rtype: requests.Response |
67
|
|
|
""" |
68
|
|
|
|
69
|
|
|
return self.__putRequest('/components/%s' % c_id, kwargs) |
70
|
|
|
|
71
|
|
|
def deleteComponentsByID(self, c_id): |
72
|
|
|
"""Delete a component. |
73
|
|
|
|
74
|
|
|
:param id: Component ID |
75
|
|
|
:return: :class:`Response <Response>` object |
76
|
|
|
:rtype: requests.Response |
77
|
|
|
""" |
78
|
|
|
|
79
|
|
|
return self.__delRequest('/components/%s' % c_id) |
80
|
|
|
|
81
|
|
|
def getComponentsGroups(self): |
82
|
|
|
""" |
83
|
|
|
|
84
|
|
|
:return: :class:`Response <Response>` object |
85
|
|
|
:rtype: requests.Response |
86
|
|
|
""" |
87
|
|
|
|
88
|
|
|
return self.__getRequest('/components/groups') |
89
|
|
|
|
90
|
|
|
def getComponentsGroupsByID(self, c_id): |
91
|
|
|
""" |
92
|
|
|
|
93
|
|
|
:param id: ID of the group you want to fetch |
94
|
|
|
:return: :class:`Response <Response>` object |
95
|
|
|
:rtype: requests.Response |
96
|
|
|
""" |
97
|
|
|
|
98
|
|
|
return self.__getRequest('/components/groups/%s' % c_id) |
99
|
|
|
|
100
|
|
|
def postComponentsGroups(self, name, **kwargs): |
101
|
|
|
""" |
102
|
|
|
|
103
|
|
|
:param name: Name of the component group |
104
|
|
|
:param order: (optional) Order of the component group |
105
|
|
|
:param collapsed: (optional) Whether to collapse the group by default |
106
|
|
|
:return: :class:`Response <Response>` object |
107
|
|
|
:rtype: requests.Response |
108
|
|
|
""" |
109
|
|
|
|
110
|
|
|
kwargs['name'] = name |
111
|
|
|
return self.__postRequest('/components/groups', kwargs) |
112
|
|
|
|
113
|
|
|
def putComponentsGroupsByID(self, c_id, **kwargs): |
114
|
|
|
""" |
115
|
|
|
|
116
|
|
|
:param id: Component group to update |
117
|
|
|
:param name: (optional) Name of the component group |
118
|
|
|
:param order: (optional) Order of the group |
119
|
|
|
:param collapsed: (optional) Whether to collapse the group by default |
120
|
|
|
:return: :class:`Response <Response>` object |
121
|
|
|
:rtype: requests.Response |
122
|
|
|
""" |
123
|
|
|
|
124
|
|
|
return self.__putRequest('/components/groups/%s' % c_id, kwargs) |
125
|
|
|
|
126
|
|
|
def deleteComponentsGroupsByID(self, c_id): |
127
|
|
|
""" |
128
|
|
|
|
129
|
|
|
:param id: Component group to delete |
130
|
|
|
:return: :class:`Response <Response>` object |
131
|
|
|
:rtype: requests.Response |
132
|
|
|
""" |
133
|
|
|
|
134
|
|
|
return self.__getRequest('/components/groups/%s' % c_id) |
135
|
|
|
|
136
|
|
|
def getIncidents(self): |
137
|
|
|
"""Return all incidents. |
138
|
|
|
|
139
|
|
|
:return: :class:`Response <Response>` object |
140
|
|
|
:rtype: requests.Response |
141
|
|
|
""" |
142
|
|
|
|
143
|
|
|
return self.__getRequest('/incidents') |
144
|
|
|
|
145
|
|
|
def getIncidentsByID(self, i_id): |
146
|
|
|
"""Returns a single incident. |
147
|
|
|
|
148
|
|
|
:param id: Incident ID |
149
|
|
|
:return: :class:`Response <Response>` object |
150
|
|
|
:rtype: requests.Response |
151
|
|
|
""" |
152
|
|
|
|
153
|
|
|
return self.__getRequest('/incidents/%s' % i_id) |
154
|
|
|
|
155
|
|
|
def postIncidents(self, name, message, status, visible, **kwargs): |
156
|
|
|
"""Create a new incident. |
157
|
|
|
|
158
|
|
|
:param name: Name of the incident |
159
|
|
|
:param message: A message (supporting Markdown) to explain more. |
160
|
|
|
:param status: Status of the incident. |
161
|
|
|
:param visible: Whether the incident is publicly visible. |
162
|
|
|
:param component_id: (optional) Component to update. |
163
|
|
|
:param component_status: (optional) The status to update the given component with. |
164
|
|
|
:param notify: (optional) Whether to notify subscribers. |
165
|
|
|
:return: :class:`Response <Response>` object |
166
|
|
|
:rtype: requests.Response |
167
|
|
|
""" |
168
|
|
|
|
169
|
|
|
kwargs['name'] = name |
170
|
|
|
kwargs['message'] = message |
171
|
|
|
kwargs['status'] = status |
172
|
|
|
kwargs['visible'] = visible |
173
|
|
|
return self.__postRequest('/incidents', kwargs) |
174
|
|
|
|
175
|
|
|
def putIncidentsByID(self, i_id, **kwargs): |
176
|
|
|
""" |
177
|
|
|
|
178
|
|
|
:param id: ID of the incident to update. |
179
|
|
|
:param name: (optional) Name of the incident |
180
|
|
|
:param message: (optional) A message (supporting Markdown) to explain more. |
181
|
|
|
:param status: (optional) Status of the incident. |
182
|
|
|
:param visible: (optional) Whether the incident is publicly visible. |
183
|
|
|
:param component_id: (optional) Component to update. |
184
|
|
|
:param notify: (optional) Whether to notify subscribers. |
185
|
|
|
:return: :class:`Response <Response>` object |
186
|
|
|
:rtype: requests.Response |
187
|
|
|
""" |
188
|
|
|
|
189
|
|
|
return self.__putRequest('/incidents/%s' % i_id, kwargs) |
190
|
|
|
|
191
|
|
|
def deleteIncidentsByID(self, i_id): |
192
|
|
|
"""Delete an incident. |
193
|
|
|
|
194
|
|
|
:param id: Incident ID |
195
|
|
|
:return: :class:`Response <Response>` object |
196
|
|
|
:rtype: requests.Response |
197
|
|
|
""" |
198
|
|
|
|
199
|
|
|
return self.__delRequest('/incidents/%s' % i_id) |
200
|
|
|
|
201
|
|
|
def getMetrics(self): |
202
|
|
|
"""Returns all metrics that have been setup. |
203
|
|
|
|
204
|
|
|
:return: :class:`Response <Response>` object |
205
|
|
|
:rtype: requests.Response |
206
|
|
|
""" |
207
|
|
|
|
208
|
|
|
return self.__getRequest('/metrics') |
209
|
|
|
|
210
|
|
|
def postMetrics(self, name, suffix, description, default_value, **kwargs): |
211
|
|
|
"""Create a new metric. |
212
|
|
|
|
213
|
|
|
:param name: Name of metric |
214
|
|
|
:param suffix: Measurments in |
215
|
|
|
:param description: Description of what the metric is measuring |
216
|
|
|
:param default_value: The default value to use when a point is added |
217
|
|
|
:param display_chart: (optional) Whether to display the chart on the status page |
218
|
|
|
:return: :class:`Response <Response>` object |
219
|
|
|
:rtype: requests.Response |
220
|
|
|
""" |
221
|
|
|
|
222
|
|
|
kwargs['name'] = name |
223
|
|
|
kwargs['suffix'] = suffix |
224
|
|
|
kwargs['description'] = description |
225
|
|
|
kwargs['default_value'] = default_value |
226
|
|
|
return self.__postRequest('/metrics', kwargs) |
227
|
|
|
|
228
|
|
|
def getMetricsByID(self, c_id): |
229
|
|
|
"""Returns a single metric, without points. |
230
|
|
|
|
231
|
|
|
:param id: Metric ID |
232
|
|
|
:return: :class:`Response <Response>` object |
233
|
|
|
:rtype: requests.Response |
234
|
|
|
""" |
235
|
|
|
|
236
|
|
|
return self.__getRequest('/metrics/%s' % c_id) |
237
|
|
|
|
238
|
|
|
def deleteMetricsByID(self, c_id): |
239
|
|
|
"""Delete a metric. |
240
|
|
|
|
241
|
|
|
:param id: Metric ID |
242
|
|
|
:return: :class:`Response <Response>` object |
243
|
|
|
:rtype: requests.Response |
244
|
|
|
""" |
245
|
|
|
|
246
|
|
|
return self.__delRequest('/metrics/%s' % c_id) |
247
|
|
|
|
248
|
|
|
def getMetricsPointsByID(self, c_id): |
249
|
|
|
"""Return a list of metric points. |
250
|
|
|
|
251
|
|
|
:param id: Metric ID |
252
|
|
|
:return: :class:`Response <Response>` object |
253
|
|
|
:rtype: requests.Response |
254
|
|
|
""" |
255
|
|
|
|
256
|
|
|
return self.__getRequest('/metrics/%s/points' % c_id) |
257
|
|
|
|
258
|
|
|
def postMetricsPointsByID(self, c_id, value, **kwargs): |
259
|
|
|
"""Add a metric point to a given metric. |
260
|
|
|
|
261
|
|
|
:param id: Metric ID |
262
|
|
|
:param value: Value to plot on the metric graph |
263
|
|
|
:param timestamp: Unix timestamp of the point was measured |
264
|
|
|
:return: :class:`Response <Response>` object |
265
|
|
|
:rtype: requests.Response |
266
|
|
|
""" |
267
|
|
|
|
268
|
|
|
kwargs['value'] = value |
269
|
|
|
return self.__postRequest('/metrics/%s/points' % c_id, kwargs) |
270
|
|
|
|
271
|
|
|
def deleteMetricsPointsByID(self, c_id, point_id): |
272
|
|
|
"""Delete a metric point. |
273
|
|
|
|
274
|
|
|
:param id: Metric ID |
275
|
|
|
:param point_id: Metric Point ID |
276
|
|
|
:return: :class:`Response <Response>` object |
277
|
|
|
:rtype: requests.Response |
278
|
|
|
""" |
279
|
|
|
|
280
|
|
|
return self.__delRequest('/metrics/%s/points/%s' % (c_id, point_id)) |
281
|
|
|
|
282
|
|
|
def getSubscribers(self): |
283
|
|
|
"""Returns all subscribers. |
284
|
|
|
|
285
|
|
|
:return: :class:`Response <Response>` object |
286
|
|
|
:rtype: requests.Response |
287
|
|
|
""" |
288
|
|
|
|
289
|
|
|
return self.__getRequest('/subscribers') |
290
|
|
|
|
291
|
|
|
def postSubscribers(self, email, **kwargs): |
292
|
|
|
"""Create a new subscriber. |
293
|
|
|
|
294
|
|
|
:param email: Email address to subscribe |
295
|
|
|
:param verify: (optional) Whether to send verification email |
296
|
|
|
:return: :class:`Response <Response>` object |
297
|
|
|
:rtype: requests.Response |
298
|
|
|
""" |
299
|
|
|
|
300
|
|
|
kwargs['email'] = email |
301
|
|
|
return self.__postRequest('/subscribers', kwargs) |
302
|
|
|
|
303
|
|
|
def deleteSubscribersByID(self, c_id): |
304
|
|
|
"""Delete a subscriber. |
305
|
|
|
|
306
|
|
|
:param id: ID of the subscriber to delete |
307
|
|
|
:return: :class:`Response <Response>` object |
308
|
|
|
:rtype: requests.Response |
309
|
|
|
""" |
310
|
|
|
|
311
|
|
|
return self.__delRequest('/subscribers/%s' % c_id) |
312
|
|
|
|