# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   Selenium_Project
# FileName:      request_method.py
# Author:       xxxxxxx
# Datetime:     2024/5/20 19:59
# Description:
# 命名规范：文件名全小写+下划线，类名大驼峰，方法和变量小写+下划线连接，
# 常量大写，变量和常量用名词，方法用动词
# ---------------------------------------------------------------------------
import base64

import requests.sessions

from api_test_0520 import log_decorator
from api_test_0520.common.readini import ReadIni


class RequestMethod:
    @log_decorator
    def __init__(self):
        ini = ReadIni()
        login_url = ini.get_host("bpm")+"/auth"
        login_data = {"username": "admin", "password": base64.b64encode("123456".encode()).decode()}
        self.sess = requests.sessions.Session()
        result = self.sess.request(method="post", url=login_url, json=login_data)
        token = result.json().get("token")
        self.sess.headers.update({"Authorization": "Bearer "+token})

    @log_decorator
    def request_all(self, req_method, req_url, req_mime, req_data):
        if req_mime == "application/json" or req_mime == "json":
            return self.sess.request(method=req_method, url=req_url, json=req_data)
        elif req_mime == "application/x-www-form-urlencoded" or req_mime == "form" or req_mime == "text/plain":
            return self.sess.request(method=req_method, url=req_url, data=req_data)
        elif req_mime == "multipart/form-data" or req_mime == "form-data":
            return self.sess.request(method=req_method, url=req_url, files=req_data)
        elif req_mime == "query" or req_mime == "params":
            return self.sess.request(method=req_method, url=req_url, params=req_data)
        elif req_mime == "query|json" or req_mime == "json|query":
            query = req_data["query"]
            json = req_data["json"]
            return self.sess.request(method=req_method, url=req_url, params=query, json=json)
        elif req_mime is None:
            return self.sess.request(method=req_method, url=req_url)
        else:
            raise ValueError ("重新封装新的请求分支")