import requests
from v3.read_data.Readbasicini import *
from v3.read_data.db import *
from v3 import log

class Requests_code:
    def __init__(self):
        log.info(f"连接数据库并且获取token")
        self.db=DB()
        login_data={"username": "admin",
                    "password": "U+nxJW50BjY7Y8Lxu2SzdNqqzJefDLgnR5OK1cdqcDe5hY+0RNiKJvblUIzHgUr5y7J87C6/XUvY7N+Gh6S2vkNvuest2+OJgp/HAfhckiwL9foWh9lIAnzRWEv5FUD0j/itSmSpt0gmMC7rYp5BVBhnRxBJEW50yHLJI36MqGk="}
        ini = Readbasicini()
        login_url = ini.get_host("bpm")+"/auth"
        self.bpm_session=requests.sessions.Session()
        token=self.bpm_session.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_media=None,req_case_data=None,sql_case_data=None,sql_type=None,sql_update=None):
        if sql_type == "delete":
            self.db.delete(sql_case_data)
        elif sql_type == "select":
            sql_res=self.db.select(sql_case_data)
            req_case_data[sql_update]=sql_res
        elif sql_type in ("delete|select","select|delete"):
            self.db.delete(sql_case_data)
            select_res=self.db.select(sql_case_data["select"])
            req_case_data[sql_update] = select_res

        if req_media in ("application/x-www-form-urlencoded","x-www-form-urlencoded","form"):
            return self.bpm_session.request(method=req_method,url=req_url,data=req_case_data)
        elif req_media in ("application/json","json"):
            return self.bpm_session.request(method=req_method,url=req_url,json=req_case_data)
        elif req_media in ("multipart/form-data","form-data"):
            return self.bpm_session.request(method=req_method,url=req_url,files=req_case_data)
        elif req_media == "query":
            return self.bpm_session.request(method=req_method,url=req_url,params=req_case_data)
        elif req_media == "None":
            return self.bpm_session.request(method=req_method,url=req_url)
        elif req_media in ("query|json","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_media}, 没有符合的判断分支，可以自行封装，也可以继承之后覆盖")
            raise ValueError("没有符合使用的分支")


if __name__== "__main__":
    code=Requests_code()
    code1=code.request_all(req_method="get",req_url="https://cn.bing.com/",req_media="json")
    