LogicGoInfotechSpaces's picture
Add testing scripts and docs for duplicate detection API
705812e
"""Simple CLI to hit the duplicate-detector API."""
from __future__ import annotations
import argparse
import json
import os
import sys
from typing import Any, Dict
import requests
DEFAULT_BASE_URL = os.getenv("API_BASE_URL", "http://127.0.0.1:8000")
def _print_response(label: str, response: requests.Response) -> None:
print(f"\n== {label} ==")
print(f"Status: {response.status_code}")
if response.headers.get("content-type", "").startswith("application/json"):
try:
payload = response.json()
print(json.dumps(payload, indent=2, default=str))
except ValueError:
print(response.text)
else:
print(response.text)
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description="Exercise the duplicate-detector API.")
parser.add_argument(
"--base-url",
default=DEFAULT_BASE_URL,
help=f"API base URL (default: {DEFAULT_BASE_URL}).",
)
args = parser.parse_args(argv)
base_url = args.base_url.rstrip("/")
try:
health = requests.get(f"{base_url}/health", timeout=10)
_print_response("Health", health)
suggestions = requests.get(f"{base_url}/suggestions", timeout=60)
_print_response("Suggestions", suggestions)
return 0
except requests.RequestException as exc:
print(f"Request failed: {exc}", file=sys.stderr)
return 1
if __name__ == "__main__":
raise SystemExit(main())