# -*- coding: utf-8 -*-
# 基础接口请求类
# 作者: 陈磊
# 时间: 2019-10-21
import requests
import urllib3

from WorkUtils.UtilsLog import UtilsLog
from requests.exceptions import ConnectTimeout, ReadTimeout, ConnectionError, RequestException


class UtilsRequest:
    def __init__(self):
        # 禁用验证
        urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
        self.log = UtilsLog()
        self.log.info("调用基础请求封装类")
        self.log.info(self.__class__)

        self.session = requests.Session()

        self.api = requests

    def post(self, url, headers=None, params=None, json=None, data=None, files=None):
        """
        Post请求方法
        :param url: 请求地址
        :param headers: 请求头
        :param params: 请求头参数
        :param json: 请求JSON参数
        :param data: 请求DATA参数
        :param files: 请求FILE参数
        :return:
        """
        self.log.info("调用Post请求方法")
        self.log.debug("接口请求url:")
        self.log.debug(url)
        self.log.debug("接口请求headers:")
        if headers is None:
            headers = {}
        self.log.debug(str(headers).replace('\'', '\"'))
        self.log.debug("接口请求params:")
        if params is None:
            params = {}
        self.log.debug(str(params).replace('\'', '\"'))
        self.log.debug("接口请求json:")
        if json is None:
            json = {}
        self.log.debug(str(json).replace('\'', '\"'))
        self.log.debug("接口请求data:")
        if data is None:
            data = {}
        self.log.debug(str(data).replace('\'', '\"'))
        self.log.debug("接口请求files:")
        if data is None:
            data = {}
        self.log.debug(files)
        try:
            response = self.api.post(url=url, headers=headers, params=params, json=json, data=data, files=files, timeout=60, verify=False)
            code = response.status_code
            text = response.text
            if code != 200:
                self.log.error("接口请求返回状态码错误:" + str(code))
                self.log.info(text)
                return text
            else:
                self.log.info("接口请求返回正常:")
                self.log.info(text)
                return text
        except ConnectTimeout:
            self.log.error("接口连接超时!")
            return False
        except ReadTimeout:
            self.log.error("接口请求超时!")
            return False
        except ConnectionError:
            self.log.error("接口请求错误!")
            return False
        except RequestException:
            self.log.error("接口请求返回错误!")
            return False

    def get(self, url, headers=None, params=None):
        """
        Get请求方法
        :param url: 请求地址
        :param headers: 请求头
        :param params: 请求头参数
        :return:
        """
        self.log.info("调用Get请求方法")
        self.log.debug("接口请求url:")
        self.log.debug(url)
        self.log.debug("接口请求headers:")
        if headers is None:
            headers = {}
        self.log.debug(str(headers).replace('\'', '\"'))
        self.log.debug("接口请求params:")
        if params is None:
            params = {}
        self.log.debug(str(params).replace('\'', '\"'))
        try:
            response = self.api.get(url=url, headers=headers, params=params, timeout=60, verify=False)
            code = response.status_code
            text = response.text
            if code != 200:
                self.log.error("接口请求返回状态码错误:" + str(code))
                self.log.info(text)
                return text
            else:
                self.log.info("接口请求返回正常:")
                self.log.info(text)
                return text
        except ConnectTimeout:
            self.log.error("接口连接超时!")
            return False
        except ReadTimeout:
            self.log.error("接口请求超时!")
            return False
        except ConnectionError:
            self.log.error("接口请求错误!")
            return False
        except RequestException:
            self.log.error("接口请求返回错误!")
            return False
