# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   PythonProject_1
# FileName:      requests_method.py
# Author:       H_M
# Datetime:     2024/9/3 19:10
# Description:
# ---------------------------------------------------------------------------
import requests

from MyApiAutoTest_v3.common.basic_read_ini import BasicReadIni


class RequestMethod:

    def __init__(self):
        """获取token配置token到headers中"""
        login_url = BasicReadIni().get_host("bpm_host") + '/auth'
        login_data = {"username": "admin", "password": "Pal0LcFUx+ke5uL88fo7YQzfHZwY5D2Lk76pzG1GjiMLlL6SmA6ls13w2X"
                                                       "zN3iSTHXkyqLuY+VT9W9AVUv3gz+4u97eX2myDxC4o4Ov15QWxTEhrPlN"
                                                       "NZf18zNEdNRstYUcpDYo5pnWI+zHI5TiRUwcTETG9LqC7cidO0u1cFH4="}
        self.sess = requests.sessions.Session()
        self.sess.headers['Authorization'] = 'Bearer ' + self.sess.request(method='post', url=login_url, json=login_data).json().get('token')

    def request_all(self, url, method, mediatype, case_data):
        """
        封装公共的请求方法
        :param method: http协议的请求方法
        :param url: 请求的url
        :param mediatype: 请求的媒体类型
        :param case_data: 用例数据
        :return: Response type
        """
        if mediatype == 'application/json' or mediatype == 'json':
            return self.sess.request(url=url, method=method, json=case_data)

        elif mediatype == 'query' or mediatype == 'params':
            return self.sess.request(url=url, method=method, params=case_data)

        elif mediatype == 'application/x-www-form-urlencoded' or mediatype == 'form':
            return self.sess.request(url=url, method=method, data=case_data)

        elif mediatype == 'multipart/form-data' or mediatype == 'form-data':
            return self.sess.request(url=url, method=method, files=case_data)

        elif mediatype is None:
            return self.sess.request(url=url, method=method)

        elif mediatype == 'query|json' or mediatype == 'json|query':
            return self.sess.request(url=url, method=method, params=case_data['query'], json=case_data['body'])

        else:
            raise ValueError('自行补充媒体类型，封装方法')


if __name__ == '__main__':
    res = RequestMethod()
    print(res.sess.headers)