def http_request(path, query='', json_body=None, method='get', config=None):
config = read_home_config() if config is None else config
try:
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
except Exception: pass
if query is not None:
if isinstance(query, dict):
query = urlencode(query)
if '?' in path:
path += '&' + query
else:
path += '?' + query
url = 'https://' + config['host'] + '/api/' + path
auth = None
if 'auth_user' in config:
auth = HTTPBasicAuth(config['auth_user'], config['auth_pw'])
if json_body is not None and method == 'get':
method = 'post'
response = requests.request(
method, url, data=json_body,
auth=auth, verify=config['ssl_verify'],
headers={'Accept': 'application/json'}
)
if response.status_code >= 400:
raise_response_exception('Failed request ' + path, response)
return parse_json(response.content.decode('utf-8'))
评论列表
文章目录