对服务器API调用文件进行拆解与更新

This commit is contained in:
2025-09-12 23:31:26 +08:00
parent 740a4f67aa
commit f7b4f7158a
9 changed files with 713 additions and 221 deletions

30
core/SNAPI/CallServer.py Normal file
View File

@@ -0,0 +1,30 @@
import requests
import json
from requests.exceptions import RequestException
import platform
UA = f"SolianForPythonApp/0.000.001 ({platform.system()})"
def _make_request(method: str, url: str, headers: dict, params: dict = None, normal_codes: list = [200], request_body: dict = None) -> dict:
"""内部辅助函数用于发送HTTP请求并处理响应"""
headers['User-Agent'] = UA
try:
if method == 'GET':
response = requests.get(url, headers=headers, params=params)
elif method == 'POST':
response = requests.post(url, headers=headers, data=json.dumps(request_body))
elif method == 'DELETE':
response = requests.delete(url, headers=headers, params=params)
elif method == 'PATCH':
response = requests.patch(url, headers=headers, data=json.dumps(request_body))
else:
return {"error": "Unsupported HTTP method"}
if response.status_code not in normal_codes:
return {"error": f"Unexpected status code: {response.status_code}"}
return response.json()
except json.JSONDecodeError:
return {"error": response.text}
except RequestException as e:
return {"error": str(e)}