-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLiveRailClass.py
81 lines (70 loc) · 2.75 KB
/
LiveRailClass.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
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
__author__ = 'omriariav'
import requests
import xmltodict
from pprint import pprint
import hashlib
import urllib
#CONSTS:
PRODUCTION_URL = "http://api4.liverail.com"
DEV_URL = "http://api4.int.liverail.com"
class LiveRailClass():
def __init__(self,env="dev"):
self._headers = {"content-type": "application/x-www-form-urlencoded"}
if env == "dev":
self._url = DEV_URL
else:
self._url = PRODUCTION_URL
self._login_flag = False
self._token = ""
self._entity = ""
def _get(self,rest_path, payload={}):
_request_url = self._url + rest_path
r = requests.get(_request_url, params=payload, headers=self._headers)
return r.content
def _post(self,rest_path, payload={}):
res = {}
_request_url = self._url + rest_path
if self._token != "":
payload['token'] = self._token
payload = urllib.urlencode(payload)
r = requests.post(_request_url, data=payload, headers=self._headers)
xmlParsed = xmltodict.parse(r.content)
pprint(xmlParsed)
res['python_used_url'] = _request_url
res['python_used_payload'] = payload
res['liverailapiused'] = xmlParsed['liverailapi']['@requested']
res['api_version'] = xmlParsed['liverailapi']['@api_version']
if xmlParsed['liverailapi']['status'] == "fail":
res['error'] = True
res['error_description'] = xmlParsed['liverailapi']['error']['message']
res['error_code'] = xmlParsed['liverailapi']['error']['code']
if xmlParsed['liverailapi']['error'].has_key('field'):
res['error_field'] = xmlParsed['liverailapi']['error']['field']
res['payload'] = ""
else:
res['error'] = False
res['error_description'] = None
res['error_code'] = None
res['error_field'] = None
res["payload"] = xmlParsed['liverailapi']
return res
def login(self, username, password):
hashedPassword = hashlib.md5(password).hexdigest()
username = username.strip()
res = self._post("/login", {"username": username, "password": hashedPassword})
if res['error']:
self._login_flag = False
print "Login failure"
return False
self._token = res['payload']['auth']['token']
self._entity = res['payload']['auth']['entity']['entity_id']
self._login_flag = True
return True
def setEntity(self,entity_id):
rest_path = "/set/entity"
res = self._post(rest_path,{"entity_id":entity_id})
return res
def revenueReport(self, payload):
rest_path = "/statistics/aggregated"
res = self._post(rest_path, payload)
return res