forked from NimaQu/shadowsocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webapi_utils.py
69 lines (63 loc) · 2.26 KB
/
webapi_utils.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import logging
import requests
from configloader import load_config, get_config
from collections import OrderedDict
class WebApi(object):
def __init__(self):
self.session_pool = requests.Session()
def getApi(self, uri, params={}):
res = None
try:
uri_params = params.copy()
uri_params['key'] = get_config().WEBAPI_TOKEN
res = self.session_pool.get(
'%s/mod_mu/%s' %
(get_config().WEBAPI_URL, uri),
params=uri_params,
timeout=10)
try:
data = res.json()
except Exception:
if res:
logging.error("Error data:%s" % (res.text))
raise Exception('error data!')
if data['ret'] == 0:
logging.error("Error data:%s" % (res.text))
logging.error("request %s error!wrong ret!"%(uri))
raise Exception('wrong ret!')
return data['data']
except Exception:
import traceback
trace = traceback.format_exc()
logging.error(trace)
raise Exception('network issue or server error!')
def postApi(self, uri, params={}, raw_data={}):
res = None
try:
uri_params = params.copy()
uri_params['key'] = get_config().WEBAPI_TOKEN
res = self.session_pool.post(
'%s/mod_mu/%s' %
(get_config().WEBAPI_URL,
uri),
params=uri_params,
json=raw_data,
timeout=10)
try:
data = res.json()
except Exception:
if res:
logging.error("Error data:%s" % (res.text))
raise Exception('error data!')
if data['ret'] == 0:
logging.error("Error data:%s" % (res.text))
logging.error("request %s error!wrong ret!"%(uri))
raise Exception('wrong ret!')
return data['data']
except Exception:
import traceback
trace = traceback.format_exc()
logging.error(trace)
raise Exception('network issue or server error!')