diff --git a/api/qinglong.py b/api/qinglong.py index ddb1fb5..184500b 100644 --- a/api/qinglong.py +++ b/api/qinglong.py @@ -1,9 +1,8 @@ from urllib.parse import urljoin import aiohttp -import requests from enum import Enum from typing import Union - +from utils.tools import send_request class QlUri(Enum): user_login = "api/user/login" @@ -33,7 +32,7 @@ class QlApi(object): headers['Authorization'] = self.token self.headers = headers - def login_by_username(self, user: str, password: str): + async def login_by_username(self, user: str, password: str): data = { "username": user, "password": password @@ -41,9 +40,9 @@ class QlApi(object): headers = { 'Content-Type': 'application/json' } - response = requests.post(url=urljoin(self.url, QlUri.user_login.value), json=data, headers=headers) - if response.status_code == 200: - self.token = "Bearer " + response.json()["data"]["token"] + response = await send_request(url=urljoin(self.url, QlUri.user_login.value), method="post", headers=headers, data=data) + if response['code'] == 200: + self.token = "Bearer " + response["data"]["token"] headers['Authorization'] = self.token self.headers = headers return response @@ -75,7 +74,7 @@ class QlOpenApi(object): self.url = url self.headers = None - def login(self, client_id: str, client_secret: str): + async def login(self, client_id: str, client_secret: str): headers = { 'Content-Type': 'application/json' } @@ -83,8 +82,7 @@ class QlOpenApi(object): "client_id": client_id, "client_secret": client_secret } - response = requests.get(url=urljoin(self.url, QlOpenUri.auth_token.value), params=params, headers=headers) - response = response.json() + response = await send_request(url=urljoin(self.url, QlOpenUri.auth_token.value), method="get", headers=headers, params=params) if response['code'] == 200: self.token = "Bearer " + response["data"]["token"] headers['Authorization'] = self.token diff --git a/main.py b/main.py index de086ec..a1ab440 100644 --- a/main.py +++ b/main.py @@ -439,7 +439,7 @@ async def get_ql_api(ql_data): if client_id and client_secret: logger.info("使用client_id和client_secret登录......") qlapi = QlOpenApi(ql_data["url"]) - response = qlapi.login(client_id=client_id, client_secret=client_secret) + response = await qlapi.login(client_id=client_id, client_secret=client_secret) if response['code'] == 200: logger.info("client_id和client_secret正常可用......") return qlapi @@ -458,8 +458,8 @@ async def get_ql_api(ql_data): response = await qlapi.get_envs() if response['code'] == 401: logger.info("Token已失效, 正使用账号密码获取QL登录态......") - response = qlapi.login_by_username(ql_data.get("username"), ql_data.get("password")) - if response.status_code != 200: + response = await qlapi.login_by_username(ql_data.get("username"), ql_data.get("password")) + if response['code'] != 200: logger.error(f"账号密码登录失败. response: {response}") raise Exception(f"账号密码登录失败. response: {response}") else: @@ -467,8 +467,8 @@ async def get_ql_api(ql_data): else: # 最后用账号密码 logger.info("正使用账号密码获取QL登录态......") - response = qlapi.login_by_username(ql_data.get("username"), ql_data.get("password")) - if response.status_code != 200: + response = await qlapi.login_by_username(ql_data.get("username"), ql_data.get("password")) + if response['code'] != 200: logger.error(f"账号密码登录失败. response: {response}") raise Exception(f"账号密码登录失败.response: {response}") return qlapi diff --git a/requirements.txt b/requirements.txt index 6f337ab..3e034d9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ ddddocr -requests aiohttp playwright loguru diff --git a/utils/tools.py b/utils/tools.py index 69f9a2e..f1c3756 100644 --- a/utils/tools.py +++ b/utils/tools.py @@ -356,10 +356,10 @@ def cv2_save_img(img_name, img, tmp_dir:str = './tmp'): return img_path -async def send_request(url: str, method: str, headers: Dict[str, Any], data: Dict[str, Any]) -> Dict[str, Any]: +async def send_request(url: str, method: str, headers: Dict[str, Any], data: Dict[str, Any] = None, **kwargs) -> Dict[str, Any]: """ 发请求的通用方法 """ async with aiohttp.ClientSession() as session: - async with session.request(method, url=url, json=data, headers=headers) as response: + async with session.request(method, url=url, json=data, headers=headers, **kwargs) as response: return await response.json()