"""
HTTP request functions

modified by donghao 2016.08.04

"""
import json
import requests
requests.adapters.DEFAULT_RETRIES = 10


def send_http_request(req_method, api_host, api_port, req_url, req_headers, req_cookies, req_data, timeout=None):
    try:
        url = "http://" + str(api_host) + ":" + str(api_port) + str(req_url)
        if req_data is not None:
            req_data = json.dumps(req_data)
        print('-------------------------Request---------------------------')
        print("url: " + url)
        print("method: " + str(req_method))
        print("headers: " + str(req_headers))
        print("data: " + str(req_data))
        print('-----------------------------------------------------------')
        response = requests.request(req_method, url=url, headers=req_headers, cookies=req_cookies,
                                    data=req_data, timeout=timeout)
        print('+++++++++++++++++++++++++++Response++++++++++++++++++++++++++')
        print(response.status_code)
        if (not (response.headers.get("Content-length") is None)) \
                and (int(response.headers.get("Content-length")) < 868) \
                or response.headers.get("Content-length") is None:   # donot print piece info of files
            print(response.content)
        # print response.headers.get("Content-length")
        print(response.headers.get("set-cookie"))
        print('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
        return response
    except Exception as err:
        raise AssertionError('Connection error: %s\n%s' % (err, url))


def send_http_request_without_json(req_method, api_host, api_port, req_url, req_headers, req_cookies, req_data, timeout=None):
    try:
        url = "http://" + str(api_host) + ":" + str(api_port) + str(req_url)

        print('-------------------------Request---------------------------')
        print("url: " + url)
        print("method: " + str(req_method))
        print("headers: " + str(req_headers))
        print("data: " + str(req_data))
        print('------------------------------------------------------------------------')
        response = requests.request(req_method, url=url, headers=req_headers, cookies=req_cookies,
                                    data=req_data, timeout=timeout)

        print('+++++++++++++++++++++++++++Response++++++++++++++++++++++++++')
        print(response.status_code)
        print(response.content)
        print(response.headers.get("set-cookie"))
        print('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
        return response
    except Exception as err:
        raise AssertionError('Connection error: %s\n%s' % (err, url))


def send_https_request(req_method, api_host, api_port, req_url, req_headers, req_cookies, req_data, timeout=None):
    try:
        url = "https://" + str(api_host) + ":" + str(443) + str(req_url)
        if req_data is not None:
            req_data = json.dumps(req_data)
        print('-------------------------Request---------------------------')
        print("url: " + url)
        print("method: " + str(req_method))
        print("headers: " + str(req_headers))
        print("data: " + str(req_data))
        print('-----------------------------------------------------------')
        response = requests.request(req_method, url=url, headers=req_headers, cookies=req_cookies,
                                    data=req_data, timeout=timeout)
        print('+++++++++++++++++++++++++++Response++++++++++++++++++++++++++')
        print(response.status_code)
        print(response.content)
        print(response.headers.get("set-cookie"))
        print('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
        return response
    except Exception as err:
        raise AssertionError('Connection error: %s\n%s' % (err, url))


def send_https_request_without_json(req_from, req_method, api_host, api_port, req_url, req_headers, req_cookies,
                                    req_data, timeout=None):
    try:
        url = "https://" + str(api_host) + ":" + str(443) + str(req_url)
        req_data = json.dumps(req_data)
        print('-------------------------%sRequest---------------------------' % req_from)
        print("url: " + url)
        print("method: " + str(req_method))
        print("headers: " + str(req_headers))
        print("data: " + str(req_data))
        print('------------------------------------------------------------------------')
        response = requests.request(req_method, url=url, headers=req_headers, cookies=req_cookies,
                                    data=req_data, timeout=timeout)
        print('+++++++++++++++++++++++++++Response++++++++++++++++++++++++++')
        print(response.status_code)
        print(response.content)
        print(response.headers.get("set-cookie"))
        print('+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
        return response
    except Exception as err:
        raise AssertionError('Connection error: %s\n%s' % (err, url))


def send_request(protocol, req_method, api_host, api_port, req_url, req_headers=None, req_cookies=None,
                 req_data=None, timeout=None):
    if str(protocol) == 'HTTPS':
        return send_https_request(req_method, api_host, api_port, req_url, req_headers, req_cookies, req_data, timeout)
    return send_http_request(req_method, api_host, api_port, req_url, req_headers, req_cookies, req_data, timeout)


def send_request_without_json(protocol, req_method, api_host, api_port, req_url, req_headers=None, req_cookies=None, req_data=None):
    if str(protocol) == 'HTTPS':
        return send_https_request_without_json(req_method, api_host, api_port, req_url, req_headers,
                                               req_cookies, req_data)
    return send_http_request_without_json(req_method, api_host, api_port, req_url, req_headers, req_cookies,
                                          req_data)

