-
Notifications
You must be signed in to change notification settings - Fork 0
/
algalon_cli.py
38 lines (32 loc) · 1.03 KB
/
algalon_cli.py
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
from requests import post
from urlparse import urlparse
import json
class AlgalonClient(object):
def __init__(self, dsn, csrf_token):
self.url = None
self._dsn = None
self.token = None
self.dsn = dsn
self.csrf_token = csrf_token
def send_alarm(self, title, text):
headers = {'content-type': 'application/json',
'X-CSRFToken': self.csrf_token}
message = {'token': self.token, 'title': title, 'text': text}
return post(self.url, data=json.dumps(message), headers=headers)
@property
def dsn(self):
return self._dsn
@dsn.setter
def dsn(self, dsn):
try:
urlpart = urlparse(dsn)
token, host = urlpart.netloc.split('@')
except ValueError:
self._dsn = ''
self.token = ''
self.url = ''
else:
self._dsn = dsn
self.token = token
self.url = 'http://{host}{path}api/alarm/'.format(
host=host, path=urlpart.path)