api.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:aetros-cli 作者: aetros 项目源码 文件源码
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'))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号