-
Notifications
You must be signed in to change notification settings - Fork 2
/
connection.py
executable file
·51 lines (43 loc) · 1.57 KB
/
connection.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
from six.moves.urllib import request
from six.moves import urlparse
import hexonet.apiconnector.util
from hexonet.apiconnector.response import Response
"""
APICONNECTOR Connection
"""
class Connection:
def __init__(self, config):
"""
Constructor
"""
self._config = config
def call_raw_http(self, command, config=None):
"""
Make a curl API call over HTTP(S) and returns the response as a string
"""
post = {}
if ('login' in self._config):
post['s_login'] = self._config['login']
if ('password' in self._config):
post['s_pw'] = self._config['password']
if ('entity' in self._config):
post['s_entity'] = self._config['entity']
if ('user' in self._config):
post['s_user'] = self._config['user']
if ('role' in self._config):
post['s_login'] = self._config['login'] + "!" + self._config['role']
post['s_command'] = hexonet.apiconnector.util.command_encode(command)
post = urlparse.urlencode(post)
response = request.urlopen(self._config['url'], post.encode('UTF-8'))
content = response.read()
return content
def call_raw(self, command, config=None):
"""
Make a curl API call and returns the response as a string
"""
return self.call_raw_http(command, config)
def call(self, command, config=None):
"""
Make a curl API call and returns the response as a response object
"""
return Response(self.call_raw(command, config))