Files
more_dots/tests/test_endpoints.py
2026-03-24 18:07:22 +08:00

149 lines
4.9 KiB
Python

import json
import sys
from typing import Any, Dict, List
import httpx
from config import Config
def _base_url() -> str:
app = Config.get_section("app")
host = app.get("host", "127.0.0.1")
port = app.get("port", "8000")
if host in ("0.0.0.0", "::"):
host = "127.0.0.1"
return f"http://{host}:{port}"
def _post(client: httpx.Client, url: str, payload: Dict[str, Any]) -> None:
resp = client.post(url, json=payload)
print(f"POST {url} -> {resp.status_code}")
print(resp.text)
def _put(client: httpx.Client, url: str, payload: Dict[str, Any]) -> None:
resp = client.put(url, json=payload)
print(f"PUT {url} -> {resp.status_code}")
print(resp.text)
def _get(client: httpx.Client, url: str) -> None:
resp = client.get(url)
print(f"GET {url} -> {resp.status_code}")
print(resp.text)
def _stream_sse(client: httpx.Client, url: str, payload: Dict[str, Any]) -> None:
with client.stream("POST", url, json=payload) as resp:
print(f"POST {url} -> {resp.status_code}")
current_event = "message"
for raw in resp.iter_lines():
if raw is None:
continue
line = raw.strip()
if not line:
continue
if line.startswith("event:"):
current_event = line.split(":", 1)[1].strip() or "message"
continue
if line.startswith("data:"):
data = line.split(":", 1)[1].strip()
print(f"[{current_event}] {data}")
def main() -> None:
base = _base_url()
menu: List[str] = [
"1) GET /health",
"2) GET /nacos/status",
"3) POST /api/workflows (conversation)",
"4) POST /api/workflows/stream (conversation)",
"5) POST /api/sql/generate",
"6) POST /api/tools/execute",
"7) POST /api/prompts/reload",
"8) POST /api/ragflow/table-retrieval/upload",
"9) PUT /api/ragflow/table-retrieval/update",
"10) POST /api/ragflow/sql-gen/upload",
"11) PUT /api/ragflow/sql-gen/update",
"0) Exit",
]
with httpx.Client(timeout=60) as client:
while True:
print("\n可用接口:")
for line in menu:
print(line)
choice = input("\n请选择编号: ").strip()
if choice == "0":
break
if choice == "1":
_get(client, f"{base}/health")
elif choice == "2":
_get(client, f"{base}/nacos/status")
elif choice == "3":
payload = {
"query": "查询 SO 4020438779 的 eta 信息",
"conversation_id": None,
"response_mode": "blocking",
"user": "tester",
"inputs": {},
"files": [],
}
_post(client, f"{base}/api/workflows", payload)
elif choice == "4":
payload = {
"query": "查询 SO 4019671497 的 eta 信息",
"conversation_id": None,
"response_mode": "streaming",
"user": "tester",
"inputs": {},
"files": [],
}
_stream_sse(client, f"{base}/api/workflows/stream", payload)
elif choice == "5":
payload = {
"query": "查询 SO 4020438779 的 eta 信息",
"conversation_id": None,
"response_mode": "blocking",
"user": "tester",
"inputs": {},
"files": [],
}
_post(client, f"{base}/api/sql/generate", payload)
elif choice == "6":
payload = {
"tool_name": "sr_api_query",
"payload": {
"sql": "SELECT 1",
"page": 1,
"rows": 1,
"orderBySelect": True,
"timeout": 30,
},
}
_post(client, f"{base}/api/tools/execute", payload)
elif choice == "7":
_post(client, f"{base}/api/prompts/reload", {})
elif choice == "8":
_post(client, f"{base}/api/ragflow/table-retrieval/upload", {})
elif choice == "9":
cfg = {"name": "table_retrieval_dataset"}
_put(client, f"{base}/api/ragflow/table-retrieval/update", cfg)
elif choice == "10":
_post(client, f"{base}/api/ragflow/sql-gen/upload", {})
elif choice == "11":
cfg = {"name": "sql_gen_dataset"}
_put(client, f"{base}/api/ragflow/sql-gen/update", cfg)
else:
print("无效选择")
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
sys.exit(0)