File size: 2,655 Bytes
1b7c76e 37260b4 1b7c76e 37260b4 1b7c76e 37260b4 1b7c76e 37260b4 1b7c76e 37260b4 1b7c76e 37260b4 1b7c76e 37260b4 1b7c76e 37260b4 1b7c76e 37260b4 1b7c76e 37260b4 1b7c76e 37260b4 1b7c76e 37260b4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | 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)
|