# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test67
# FileName:      request_method.py
# Author:       lao_zhao
# Datetime:     2025/6/13 15:27
# Description:
# 
# ---------------------------------------------------------------------------
import requests

from APIAutoTest_v3 import log_decorator
from APIAutoTest_v3.utils.read_system_ini import ReadSystemIni


class RequestMethod:
    @log_decorator
    def __init__(self):
        """关联状态"""
        login_url = ReadSystemIni().get_host("bpm") + "/auth"
        login_data = {"username":"admin","password":"EyLpSWz5HAum18d2yHU/HtQ2TLjleP+yAaA5SMFEUiLjwbyg/nTp7M0OoWw38nEheW0IBJd1+2jgpEK28XfzVdMPFICP0myzR1kBmOGC2YCXirmNpPwhhD/0xtHx52imCid0asJyYCXyhfDWJveqXFbwQs+PWsNvdxXOAybozzU="}
        self.bpm_session = requests.sessions.Session()
        self.bpm_session.headers["Authorization"] = "Bearer "+self.bpm_session.post(url=login_url, json=login_data).json().get("token")

    @log_decorator
    def request_all(self, req_method, req_url, req_mime, case_data):
        """公共的请求方法"""
        if req_mime == "json" or req_mime == "application/json":
            self.bpm_session.headers["content-type"] = "application/json"
            if isinstance(case_data, str):
                return self.bpm_session.request(method=req_method, url=req_url, data=case_data)
            else:
                return self.bpm_session.request(method=req_method, url=req_url, json=case_data)
        elif req_mime == "form" or req_mime == "application/x-www-form-urlencoded":
            self.bpm_session.headers["content-type"] = "application/x-www-form-urlencoded"
            return self.bpm_session.request(method=req_method, url=req_url, data=case_data)
        elif req_mime == "query":
            if isinstance(case_data, str):
                req_url = req_url + "?" + case_data
                return self.bpm_session.request(method=req_method, url=req_url)
            else:
                return self.bpm_session.request(method=req_method, url=req_url, params=case_data)
        elif req_mime is None:
            return self.bpm_session.request(method=req_method, url=req_url)
        else:
            raise ValueError("媒体类型错误")
