Files
SolianForPython/core/SNAPI/CallServer.py

62 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<<<<<<< HEAD
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:
=======
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:
>>>>>>> 967cea4c47a481e79c92fda0760c423de7fbcedf
return {"error": str(e)}