dev.altai / tests /test_async_api.py
prince1604
Deploy: Add query param support + clean requirements for HF
37260b4
Raw
History Blame Contribute Delete
2.66 kB
import requests
import time
import argparse
def test_api(base_url, domain, limit):
print(f"--- Starting Test against {base_url} ---")
# 1. Start Scan
start_url = f"{base_url}/api/scanstart"
print(f"1. Sending POST to {start_url}...")
try:
r = requests.post(start_url, json={"domain": domain, "limit": limit})
if r.status_code != 200:
print(f" FAILED: {r.status_code} - {r.text}")
return
job_id = r.json().get("job_id")
print(f" SUCCESS! Job ID: {job_id}")
except Exception as e:
print(f" CONNECTION ERROR: {e}")
return
# 2. Poll Progress
print(f"2. Polling progress for Job {job_id}...")
status = "pending"
while status not in ["done", "error"]:
time.sleep(2) # Wait a bit between polls
# We can test both formats: Path param or Query param
# Testing Path param format here
progress_url = f"{base_url}/api/progress/{job_id}"
try:
r = requests.get(progress_url)
data = r.json()
status = data.get("status")
percent = data.get("percent", 0)
msg = data.get("message", "")
elapsed = data.get("elapsed_seconds", 0)
print(f" [{status.upper()}] {percent}% | {elapsed}s | {msg}")
if status == "error":
print(f" ERROR Details: {data.get('error')}")
except Exception as e:
print(f" POLLING ERROR: {e}")
break
# 3. Get Result
if status == "done":
print(f"3. Fetching final result from {base_url}/api/result/{job_id}...")
try:
# Testing Query param format here for variety
result_url = f"{base_url}/api/result?job_id={job_id}"
r = requests.get(result_url)
result = r.json()
summary = result.get("summary", {})
print(f" FINISH! Status: {r.status_code}")
print(f" SUMMARY: Found {summary.get('total_images_found')} images across {summary.get('total_pages_scanned')} pages.")
except Exception as e:
print(f" RESULT ERROR: {e}")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--url", default="http://localhost:7860", help="Base URL of the API")
parser.add_argument("--domain", default="https://example.com", help="Domain to scan")
parser.add_argument("--limit", type=int, default=5, help="Page limit")
args = parser.parse_args()
test_api(args.url.rstrip("/"), args.domain, args.limit)