-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
61 lines (46 loc) · 1.82 KB
/
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
# Per richieste di tipo GET:
# api.get('?manage=ricerca&sub=findUsers')
# Per richieste di tipo POST:
# api.post('?manage=ricerca&sub=findUsers', {'term': 'gerp'})
class GerpApi:
def __init__(self, username, password, host):
self.username = username
self.password = password
self.host = host.rstrip('/') + '/'
def get(self, url):
print(self.request('get', url))
exit()
def post(self, url, options={}):
print(self.request('post', url, options))
exit()
def request(self, type, url, options={}):
type = type.lower()
if type != 'get' and type != 'post':
raise Exception('Il tipo di richiesta deve essere o "get" o "post", "' + type + '" fornito.')
params = {}
url_components = urlparse(url)
if 'query' not in url_components:
raise Exception('I parametri nell\'URL devono iniziare con il punto di domanda ("?"). Es.: "?manage=ricerca".')
parse_qs(url_components.query, params)
if 'view' not in params or params['view'] != 'json':
params['view'] = 'json'
url = self.host + 'index.php?' + urlencode(params)
data = {
'auth_username': self.username,
'auth_secret': self.password
}
if options:
data.update(options)
curl = pycurl.Curl()
curl.setopt(pycurl.URL, url)
curl.setopt(pycurl.TIMEOUT, 30)
curl.setopt(pycurl.POST, 1)
curl.setopt(pycurl.POSTFIELDS, urlencode(data))
curl.setopt(pycurl.WRITEFUNCTION, lambda x: None)
try:
curl.perform()
response = curl.getvalue().decode('utf-8')
curl.close()
return response
except pycurl.error:
raise Exception('Errore nella comunicazione con "' + url + '".')