Merge pull request #1179 from dhsifss/feat/mcp

fix: get_api response
This commit is contained in:
safe1ine
2025-04-07 18:49:47 +08:00
committed by GitHub
2 changed files with 23 additions and 5 deletions

View File

@@ -18,7 +18,6 @@ class ABCTool(ABC):
class ToolRegister:
_dict: dict[str, ABCTool] = {}
@classmethod
def register(self, tool: ABCTool) -> ABCTool:
tool_name = tool.tool().name
logging.info(f"Registering tool: {tool_name}")

View File

@@ -1,20 +1,39 @@
from httpx import AsyncClient
from config import GLOBAL_CONFIG
import httpx
import json
def check_slce_response(response: httpx.Response) -> str:
def get_response_data(response: httpx.Response) -> dict:
if response.status_code != 200:
return f"response status code: {response.status_code}"
data = response.json()
if data["msg"] is not None and data["msg"] != "":
return f"request SafeLine API failed: {data['msg']}"
raise Exception(f"request SafeLine API failed: {data['msg']}")
if data["err"] is not None and data["err"] != "":
return f"request SafeLine API failed: {data['err']}"
raise Exception(f"request SafeLine API failed: {data['err']}")
return data['data']
def check_slce_response(response: httpx.Response) -> str:
try:
get_response_data(response)
except Exception as e:
return str(e)
return "success"
def check_slce_get_response(response: httpx.Response) -> str:
try:
data = get_response_data(response)
if data:
return json.dumps(data)
return "empty response data"
except Exception as e:
return str(e)
async def get_slce_api(path: str) -> str:
if not path.startswith("/"):
path = f"/{path}"
@@ -24,7 +43,7 @@ async def get_slce_api(path: str) -> str:
response = await client.get(f"{GLOBAL_CONFIG.SAFELINE_ADDRESS}{path}", headers={
"X-SLCE-API-TOKEN": f"{GLOBAL_CONFIG.SAFELINE_API_TOKEN}"
})
return check_slce_response(response)
return check_slce_get_response(response)
except Exception as e:
return str(e)