-
Notifications
You must be signed in to change notification settings - Fork 1
/
vco_api_client.py
executable file
·96 lines (81 loc) · 3.23 KB
/
vco_api_client.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import requests
import json
import re
class vco_api_client():
def __init__(self, hostname, verify_ssl=True):
'''
Initiate the Session object, the HTTP headers,
the paths and all the associated parameters.
'''
self.session = requests.Session()
self.hostname = self._hostname_https(hostname)
self.headers = { 'Content-Type': 'application/json' }
self.verify_ssl = verify_ssl
self.seq = 0
def _hostname_https(self, hostname):
'''
Change the scheme to HTTPS if it is not already by
regular expression operation for a secure connection
'''
if hostname.startswith('http'):
re.sub('http(s)?://', '', hostname)
https = 'https://'
return https + hostname
def token_auth(self, token):
'''
Update the HTTP headers with the given API token
'''
self.headers.update( { 'Authorization': 'Token ' + token })
def cookies_auth(self, username, password, is_operator=False):
'''
Authenticate with the given username and password, and
store the cookies in the Session object authentication on success.
'''
self.seq += 1
if is_operator:
url = self.hostname + '/login/operatorLogin'
else:
url = self.hostname + '/login/enterpriseLogin'
payload = { 'username': username, 'password': password }
call = self.session.post(url, headers=self.headers,
data=json.dumps(payload), allow_redirects=False,
verify=self.verify_ssl)
try:
self.session.cookies.get_dict()['velocloud.session']
except KeyError:
# Raise a system exit on error authenticating
try:
message = str.replace(self.session.cookies.get_dict(
)['velocloud.message'], '%20', ' ')
except KeyError:
message = 'Error authenticating'
finally:
raise SystemExit(message)
def call_api(self, path, parameters):
'''
Call the given path with the given parameters
'''
self.seq += 1
path = path.strip('/')
payload = { 'jsonrpc': '2.0',
'id': self.seq,
'method': path,
'params': parameters }
if path in ('liveMode/readLiveData',
'liveMode/requestLiveActions', 'liveMode/clientExitLiveMode'):
# Use the livepull path instead should the call require live mode
url = self.hostname + '/livepull/liveData/'
else:
# Otherwise use the portal path
url = self.hostname + '/portal/'
call = self.session.post(url, headers=self.headers,
data=json.dumps(payload), verify=self.verify_ssl)
response = call.json()
if 'error' in response:
# Raise a system exit on call error
raise SystemExit(response['error']['message'])
if response:
return response['result']
else:
# Raise a system exit on empty response
raise SystemExit('Call returns empty')