-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_async_api.py
More file actions
executable file
·190 lines (159 loc) · 5.62 KB
/
test_async_api.py
File metadata and controls
executable file
·190 lines (159 loc) · 5.62 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python3
"""
Test script for the async PDF processing API
"""
import os
import time
import requests
from pathlib import Path
# Configuration
BASE_URL = "http://localhost:8000"
AUTH_TOKEN = os.getenv("AUTH_TOKEN", "")
# Headers
headers = {}
if AUTH_TOKEN:
headers["Authorization"] = f"Bearer {AUTH_TOKEN}"
def test_async_processing(pdf_path: str):
"""Test the async PDF processing workflow"""
print("=" * 60)
print("Testing Async PDF Processing API")
print("=" * 60)
# 1. Submit PDF for processing
print(f"\n1. Submitting PDF: {pdf_path}")
with open(pdf_path, "rb") as f:
files = {"file": f}
response = requests.post(
f"{BASE_URL}/process_pdf",
files=files,
headers=headers
)
if response.status_code != 200:
print(f"❌ Failed to submit PDF: {response.status_code}")
print(response.text)
return
result = response.json()
job_id = result["job_id"]
print(f"✅ PDF submitted successfully")
print(f" Job ID: {job_id}")
print(f" Status: {result['status']}")
# 2. Poll for status
print(f"\n2. Polling for status...")
max_attempts = 60
attempt = 0
while attempt < max_attempts:
response = requests.get(
f"{BASE_URL}/result/{job_id}/status",
headers=headers
)
if response.status_code != 200:
print(f"❌ Failed to get status: {response.status_code}")
break
status_data = response.json()
status = status_data["status"]
total_pages = status_data.get("total_pages", 0)
processed_pages = status_data.get("processed_pages", 0)
print(f" Attempt {attempt + 1}: {status}", end="")
if total_pages > 0:
print(f" ({processed_pages}/{total_pages} pages)")
else:
print()
if status == "completed":
print(f"✅ Processing completed!")
print(f" Filename: {status_data['filename']}")
print(f" Total pages: {status_data['total_pages']}")
print(f" Created at: {status_data['created_at']}")
print(f" Updated at: {status_data['updated_at']}")
break
elif status == "failed":
print(f"❌ Processing failed!")
print(f" Error: {status_data['error_message']}")
return
time.sleep(5)
attempt += 1
if attempt >= max_attempts:
print(f"⚠️ Timeout: Processing took too long")
return
# 3. Retrieve markdown result
print(f"\n3. Retrieving markdown result...")
response = requests.get(
f"{BASE_URL}/result/{job_id}/markdown",
headers=headers
)
if response.status_code == 200:
result = response.json()
content = result["content"]
print(f"✅ Retrieved markdown content")
print(f" Length: {len(content)} characters")
print(f" Preview (first 200 chars):\n{content[:200]}...")
elif response.status_code == 202:
print(f"⚠️ Result not ready yet (HTTP 202)")
else:
print(f"❌ Failed to retrieve result: {response.status_code}")
# 4. List extracted images
print(f"\n4. Listing extracted images...")
response = requests.get(
f"{BASE_URL}/result/{job_id}/images",
headers=headers
)
if response.status_code == 200:
result = response.json()
print(f"✅ Found {result['count']} extracted images")
if result['count'] > 0:
print(f" Images: {', '.join(result['images'][:5])}")
if result['count'] > 5:
print(f" ... and {result['count'] - 5} more")
else:
print(f"⚠️ No images found or error: {response.status_code}")
# 5. List all tasks
print(f"\n5. Listing all tasks...")
response = requests.get(
f"{BASE_URL}/tasks",
headers=headers
)
if response.status_code == 200:
result = response.json()
print(f"✅ Found {result['count']} total tasks")
for task in result['tasks'][:3]:
print(f" - {task['job_id'][:8]}... | {task['status']} | {task['filename']}")
if result['count'] > 3:
print(f" ... and {result['count'] - 3} more")
else:
print(f"❌ Failed to list tasks: {response.status_code}")
# 6. Optional: Clean up
print(f"\n6. Clean up (optional)")
cleanup = input(f" Delete job {job_id[:8]}...? (y/N): ").strip().lower()
if cleanup == 'y':
response = requests.delete(
f"{BASE_URL}/result/{job_id}",
headers=headers
)
if response.status_code == 200:
print(f"✅ Job deleted successfully")
else:
print(f"❌ Failed to delete job: {response.status_code}")
else:
print(f" Job kept. Delete manually with: DELETE /result/{job_id}")
print("\n" + "=" * 60)
print("Test completed!")
print("=" * 60)
if __name__ == "__main__":
import sys
if len(sys.argv) < 2:
print("Usage: python test_async_api.py <pdf_file>")
print("Example: python test_async_api.py sample.pdf")
sys.exit(1)
pdf_path = sys.argv[1]
if not Path(pdf_path).exists():
print(f"Error: File not found: {pdf_path}")
sys.exit(1)
if not pdf_path.endswith('.pdf'):
print(f"Error: File must be a PDF: {pdf_path}")
sys.exit(1)
try:
test_async_processing(pdf_path)
except KeyboardInterrupt:
print("\n\n⚠️ Test interrupted by user")
except Exception as e:
print(f"\n\n❌ Test failed with error: {e}")
import traceback
traceback.print_exc()