# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test61
# FileName:      request_method.py
# Author:       lao_zhao
# Datetime:     2024/7/12 14:32
# Description:
# 
# ---------------------------------------------------------------------------
import requests

from APIAutoTest_v3_1.common.read_basic_ini import ReadBasicIni


class RequestMethod:
    def __init__(self):
        """关联登录成功的状态"""
        login_url = ReadBasicIni().get_url("test_host") + "/auth"
        login_data = {"username": "admin",
                      "password": "FMgcBcmzaLp5J8AFyR8XF0MhHRpBhHNFQyDn7HI2nBO0B9RgCEnhVjvXjLMNjwoRKp01dwHueQGZbZcsEdAQQwY1QXvJrr8Z4Jv2Wn5UCC4IKuH9cIeevsc2zSEMmKULN9FJV4e9DBbnTMoxHqyawMlxVN/pidjeA0kkXjOsBE8="}
        self.bpm_session = requests.sessions.Session()
        self.bpm_session.headers.update({"Authorization": "Bearer "+self.bpm_session.post(url=login_url, json=login_data).json().get("token")})

    def request_all(self, req_method, req_url, req_mime, case_data):
        """封装公共的请求方法"""
        if req_mime == "application/json" or req_mime == "json":
            return self.bpm_session.request(method=req_method, url=req_url, json=case_data)

        # 请求体中传参，除了json类型和上传文件以外的任何类型传参，全部使用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=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=case_data)

        elif req_mime == "query" or req_mime == "params":
            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)

        # 判断请求的媒体类型是否为query|json 或者 json|query,表示在请求体以json类型传参 同时还在地址栏中传参
        elif req_mime == "query|json" or req_mime == "json|query":
            # 注意：请求体中传参的关键字data,files,json可以同时和params结合使用，但是data,files,json每次之后使用一个参数
            return self.bpm_session.request(method=req_method, url=req_url, params=case_data["query"], json=case_data["body"])

        else:
            raise ValueError("媒体类型的值错误")