-
Notifications
You must be signed in to change notification settings - Fork 1
/
elks.py
45 lines (37 loc) · 1.3 KB
/
elks.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
39
40
41
42
43
44
from urllib.error import HTTPError
from urllib.request import urlopen, Request
from urllib.parse import urlencode
from base64 import b64encode
import os
import json
class Elks:
client_key = None
client_secret = None
baseurl = "https://api.46elks.com/a1/"
def __init__(self):
if 'PASSER_ELKS_KEY' in os.environ:
self.client_key = os.environ['PASSER_ELKS_KEY']
if 'PASSER_ELKS_SECRET' in os.environ:
self.client_secret = os.environ['PASSER_ELKS_SECRET']
def query_api(self, endpoint, data=None):
b = lambda x: bytes(x, 'utf-8')
s = lambda x: x.decode('utf-8')
keysecret = '%s:%s' % (self.client_key, self.client_secret)
auth = b('BASIC %s' % s(b64encode(b(keysecret))))
call_url = "%s%s" % (self.baseurl, endpoint)
if data:
conn = Request(call_url, b(urlencode(data)))
else:
conn = Request(call_url)
conn.add_header('Authorization', auth)
try:
response = urlopen(conn)
except HTTPError as err:
return False
return s(response.read())
def get_text_by_id(self, id_):
elks_data = self.query_api('SMS/%s' % id_)
if not elks_data:
return False
data = json.loads(elks_data)
return data