# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   python_study
# FileName:      requests_method.py
# Author:       mwx
# Datetime:     2024/11/5 23:46
# Description:
# 
# ---------------------------------------------------------------------------
import requests

from Interface_automation.APIAutoText_v3_1_1 import log
from Interface_automation.APIAutoText_v3_1_1.commn.db import DB
from Interface_automation.APIAutoText_v3_1_1.commn.read_basic_ini import ReadBasicIni


class RequestsMethod:
    def __init__(self):
        self.db = DB()
        self.bpm_session = requests.sessions.Session()
        login_url = ReadBasicIni().get_host("bpm") + "/auth"
        login_data = {"username": "admin", "password": "QKpkgIwnyACrUXQrQVRaSUbhPSOsKnj5hoRN5LGKH"
                                                       "T3NmLCwGIN+aGa981YZtlLoEfpVvGDnILn0QU09VHSkgL+Or6"
                                                       "oTG1E8OtVTmAEjivAe/a4zLPwd/OfvKYIYbBP5ExZyy+wlVTbLgDfqkuK"
                                                       "PGOMPPSLlZWLfvG7yzdc+CyE="}
        token = self.bpm_session.request(method="post", url=login_url, json=login_data).json().get("token")
        self.bpm_session.headers.update({"Authorization": f"Bearer {token}"})

    def request_all(self,req_method,req_url,req_mime=None,req_case_data=None,sql_type=None,sql_data=None,update_key=None):
        """
        封装公共的请求方法
        :param req_method: 请求方法
        :param req_url: 请求url
        :param req_mime: 请求的媒体类型
        :param req_case_data: 请求数据
        :param sql_type: sql语句类型
        :param sql_data: sql语句
        :param update_key: 更新的key
        :return: Response type
        """
        if sql_type == "delete":
            self.db.delete(sql_data)
        elif sql_type == "select":
            select_result = self.db.select(sql_data)
            req_case_data[update_key] = select_result
        elif sql_type == "select|delete" or sql_type == "delete|select":
            self.db.delete(sql_data["delete"])
            select_result = self.db.select(sql_data["select"])
            req_case_data[update_key] = select_result
        if req_mime == "json" or req_mime == "application/json":
            return self.bpm_session.request(method=req_method, url=req_url, json=req_case_data)
        elif req_mime == "application/x-www-form-urlencoded" or req_mime == "x-www-form-urlencoded" or req_mime == "form":
            return self.bpm_session.request(method=req_method, url=req_url, data=req_case_data)
        elif req_mime == "multipart/form-data" or req_mime == "form-data":
            return self.bpm_session.request(method=req_method, url=req_url, files=req_case_data)
        elif req_mime == "query":
            return self.bpm_session.request(method=req_method, url=req_url, params=req_case_data)
        elif req_mime is None:
            return self.bpm_session.request(method=req_method, url=req_url)
        elif req_mime == "query|json" or req_mime == "json|query":
            return self.bpm_session.request(method=req_method, url=req_url, params=req_case_data["query"],json=req_case_data["body"])
        else:
            log.error(f"方法：request_all，传入的媒体类型为：{req_mime}, 没有符合的判断分支，可以自行封装，也可以继承之后覆盖")
            raise ValueError("没有符合的判断分支，可以自行封装，也可以继承之后覆盖")



