-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathasync_client.py
448 lines (378 loc) Β· 15.9 KB
/
async_client.py
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import asyncio
from typing import Any, Optional, Dict, Callable, Awaitable, TypeVar, Generic
from enum import Enum
from dataclasses import dataclass
from datetime import datetime
from uuid import uuid4
from aiohttp import ClientSession, ClientTimeout, TCPConnector
from aiohttp.client_exceptions import ClientError
from pydantic import BaseModel
from scrapegraph_py.config import API_BASE_URL, DEFAULT_HEADERS
from scrapegraph_py.exceptions import APIError
from scrapegraph_py.logger import sgai_logger as logger
from scrapegraph_py.models.feedback import FeedbackRequest
from scrapegraph_py.models.markdownify import GetMarkdownifyRequest, MarkdownifyRequest
from scrapegraph_py.models.searchscraper import (
GetSearchScraperRequest,
SearchScraperRequest,
)
from scrapegraph_py.models.smartscraper import (
GetSmartScraperRequest,
SmartScraperRequest,
)
from scrapegraph_py.utils.helpers import handle_async_response, validate_api_key
T = TypeVar('T')
class JobStatus(Enum):
PENDING = "pending"
RUNNING = "running"
COMPLETED = "completed"
FAILED = "failed"
@dataclass
class Job(Generic[T]):
id: str
status: JobStatus
created_at: datetime
started_at: Optional[datetime] = None
completed_at: Optional[datetime] = None
result: Optional[T] = None
error: Optional[Exception] = None
task: Optional[Callable[..., Awaitable[T]]] = None
args: tuple = ()
kwargs: dict = None
class AsyncClient:
@classmethod
def from_env(
cls,
verify_ssl: bool = True,
timeout: Optional[float] = None,
max_retries: int = 3,
retry_delay: float = 1.0,
):
"""Initialize AsyncClient using API key from environment variable.
Args:
verify_ssl: Whether to verify SSL certificates
timeout: Request timeout in seconds. None means no timeout (infinite)
max_retries: Maximum number of retry attempts
retry_delay: Delay between retries in seconds
"""
from os import getenv
api_key = getenv("SGAI_API_KEY")
if not api_key:
raise ValueError("SGAI_API_KEY environment variable not set")
return cls(
api_key=api_key,
verify_ssl=verify_ssl,
timeout=timeout,
max_retries=max_retries,
retry_delay=retry_delay,
)
def __init__(
self,
api_key: str = None,
verify_ssl: bool = True,
timeout: Optional[float] = None,
max_retries: int = 3,
retry_delay: float = 1.0,
max_queue_size: int = 1000,
):
"""Initialize AsyncClient with configurable parameters.
Args:
api_key: API key for authentication. If None, will try to load from environment
verify_ssl: Whether to verify SSL certificates
timeout: Request timeout in seconds. None means no timeout (infinite)
max_retries: Maximum number of retry attempts
retry_delay: Delay between retries in seconds
max_queue_size: Maximum number of jobs in the queue
"""
logger.info("π Initializing AsyncClient")
# Try to get API key from environment if not provided
if api_key is None:
from os import getenv
api_key = getenv("SGAI_API_KEY")
if not api_key:
raise ValueError(
"SGAI_API_KEY not provided and not found in environment"
)
validate_api_key(api_key)
logger.debug(
f"π οΈ Configuration: verify_ssl={verify_ssl}, timeout={timeout}, max_retries={max_retries}"
)
self.api_key = api_key
self.headers = {**DEFAULT_HEADERS, "SGAI-APIKEY": api_key}
self.max_retries = max_retries
self.retry_delay = retry_delay
ssl = None if verify_ssl else False
self.timeout = ClientTimeout(total=timeout) if timeout is not None else None
self.session = ClientSession(
headers=self.headers, connector=TCPConnector(ssl=ssl), timeout=self.timeout
)
# Initialize job queue
self.job_queue: asyncio.Queue[Job] = asyncio.Queue(maxsize=max_queue_size)
self.jobs: Dict[str, Job] = {}
self._queue_processor_task = None
logger.info("β
AsyncClient initialized successfully")
async def start_queue_processor(self):
"""Start the background job queue processor."""
if self._queue_processor_task is None:
self._queue_processor_task = asyncio.create_task(self._process_queue())
logger.info("π Job queue processor started")
async def stop_queue_processor(self):
"""Stop the background job queue processor."""
if self._queue_processor_task is not None:
self._queue_processor_task.cancel()
try:
await self._queue_processor_task
except asyncio.CancelledError:
pass
self._queue_processor_task = None
logger.info("βΉοΈ Job queue processor stopped")
async def _process_queue(self):
"""Process jobs from the queue."""
while True:
try:
job = await self.job_queue.get()
job.status = JobStatus.RUNNING
job.started_at = datetime.now()
try:
if job.task:
job.result = await job.task(*job.args, **(job.kwargs or {}))
job.status = JobStatus.COMPLETED
except Exception as e:
job.error = e
job.status = JobStatus.FAILED
logger.error(f"β Job {job.id} failed: {str(e)}")
finally:
job.completed_at = datetime.now()
self.job_queue.task_done()
except asyncio.CancelledError:
break
except Exception as e:
logger.error(f"β Queue processor error: {str(e)}")
async def submit_job(self, task: Callable[..., Awaitable[T]], *args, **kwargs) -> str:
"""Submit a new job to the queue.
Args:
task: Async function to execute
*args: Positional arguments for the task
**kwargs: Keyword arguments for the task
Returns:
str: Job ID
"""
job_id = str(uuid4())
job = Job(
id=job_id,
status=JobStatus.PENDING,
created_at=datetime.now(),
task=task,
args=args,
kwargs=kwargs
)
self.jobs[job_id] = job
await self.job_queue.put(job)
logger.info(f"π Job {job_id} submitted to queue")
# Ensure queue processor is running
if self._queue_processor_task is None:
await self.start_queue_processor()
return job_id
async def get_job_status(self, job_id: str) -> Dict[str, Any]:
"""Get the status of a job.
Args:
job_id: The ID of the job to check
Returns:
Dict containing job status information
"""
if job_id not in self.jobs:
raise ValueError(f"Job {job_id} not found")
job = self.jobs[job_id]
return {
"id": job.id,
"status": job.status.value,
"created_at": job.created_at,
"started_at": job.started_at,
"completed_at": job.completed_at,
"result": job.result,
"error": str(job.error) if job.error else None
}
async def wait_for_job(self, job_id: str, timeout: Optional[float] = None) -> Any:
"""Wait for a job to complete and return its result.
Args:
job_id: The ID of the job to wait for
timeout: Maximum time to wait in seconds
Returns:
The result of the job
"""
if job_id not in self.jobs:
raise ValueError(f"Job {job_id} not found")
job = self.jobs[job_id]
while job.status in (JobStatus.PENDING, JobStatus.RUNNING):
await asyncio.sleep(0.1)
if job.error:
raise job.error
return job.result
async def _make_request(self, method: str, url: str, **kwargs) -> Any:
"""Make HTTP request with retry logic."""
for attempt in range(self.max_retries):
try:
logger.info(
f"π Making {method} request to {url} (Attempt {attempt + 1}/{self.max_retries})"
)
logger.debug(f"π Request parameters: {kwargs}")
async with self.session.request(method, url, **kwargs) as response:
logger.debug(f"π₯ Response status: {response.status}")
result = await handle_async_response(response)
logger.info(f"β
Request completed successfully: {method} {url}")
return result
except ClientError as e:
logger.warning(f"β οΈ Request attempt {attempt + 1} failed: {str(e)}")
if hasattr(e, "status") and e.status is not None:
try:
error_data = await e.response.json()
error_msg = error_data.get("error", str(e))
logger.error(f"π΄ API Error: {error_msg}")
raise APIError(error_msg, status_code=e.status)
except ValueError:
logger.error("π΄ Could not parse error response")
raise APIError(
str(e),
status_code=e.status if hasattr(e, "status") else None,
)
if attempt == self.max_retries - 1:
logger.error(f"β All retry attempts failed for {method} {url}")
raise ConnectionError(f"Failed to connect to API: {str(e)}")
retry_delay = self.retry_delay * (attempt + 1)
logger.info(f"β³ Waiting {retry_delay}s before retry {attempt + 2}")
await asyncio.sleep(retry_delay)
async def markdownify(
self, website_url: str, headers: Optional[dict[str, str]] = None
):
"""Send a markdownify request"""
logger.info(f"π Starting markdownify request for {website_url}")
if headers:
logger.debug("π§ Using custom headers")
request = MarkdownifyRequest(website_url=website_url, headers=headers)
logger.debug("β
Request validation passed")
result = await self._make_request(
"POST", f"{API_BASE_URL}/markdownify", json=request.model_dump()
)
logger.info("β¨ Markdownify request completed successfully")
return result
async def get_markdownify(self, request_id: str):
"""Get the result of a previous markdownify request"""
logger.info(f"π Fetching markdownify result for request {request_id}")
# Validate input using Pydantic model
GetMarkdownifyRequest(request_id=request_id)
logger.debug("β
Request ID validation passed")
result = await self._make_request(
"GET", f"{API_BASE_URL}/markdownify/{request_id}"
)
logger.info(f"β¨ Successfully retrieved result for request {request_id}")
return result
async def smartscraper(
self,
user_prompt: str,
website_url: Optional[str] = None,
website_html: Optional[str] = None,
headers: Optional[dict[str, str]] = None,
output_schema: Optional[BaseModel] = None,
):
"""Send a smartscraper request"""
logger.info("π Starting smartscraper request")
if website_url:
logger.debug(f"π URL: {website_url}")
if website_html:
logger.debug("π Using provided HTML content")
if headers:
logger.debug("π§ Using custom headers")
logger.debug(f"π Prompt: {user_prompt}")
request = SmartScraperRequest(
website_url=website_url,
website_html=website_html,
headers=headers,
user_prompt=user_prompt,
output_schema=output_schema,
)
logger.debug("β
Request validation passed")
result = await self._make_request(
"POST", f"{API_BASE_URL}/smartscraper", json=request.model_dump()
)
logger.info("β¨ Smartscraper request completed successfully")
return result
async def get_smartscraper(self, request_id: str):
"""Get the result of a previous smartscraper request"""
logger.info(f"π Fetching smartscraper result for request {request_id}")
# Validate input using Pydantic model
GetSmartScraperRequest(request_id=request_id)
logger.debug("β
Request ID validation passed")
result = await self._make_request(
"GET", f"{API_BASE_URL}/smartscraper/{request_id}"
)
logger.info(f"β¨ Successfully retrieved result for request {request_id}")
return result
async def submit_feedback(
self, request_id: str, rating: int, feedback_text: Optional[str] = None
):
"""Submit feedback for a request"""
logger.info(f"π Submitting feedback for request {request_id}")
logger.debug(f"β Rating: {rating}, Feedback: {feedback_text}")
feedback = FeedbackRequest(
request_id=request_id, rating=rating, feedback_text=feedback_text
)
logger.debug("β
Feedback validation passed")
result = await self._make_request(
"POST", f"{API_BASE_URL}/feedback", json=feedback.model_dump()
)
logger.info("β¨ Feedback submitted successfully")
return result
async def get_credits(self):
"""Get credits information"""
logger.info("π³ Fetching credits information")
result = await self._make_request(
"GET",
f"{API_BASE_URL}/credits",
)
logger.info(
f"β¨ Credits info retrieved: {result.get('remaining_credits')} credits remaining"
)
return result
async def searchscraper(
self,
user_prompt: str,
headers: Optional[dict[str, str]] = None,
output_schema: Optional[BaseModel] = None,
):
"""Send a searchscraper request"""
logger.info("π Starting searchscraper request")
logger.debug(f"π Prompt: {user_prompt}")
if headers:
logger.debug("π§ Using custom headers")
request = SearchScraperRequest(
user_prompt=user_prompt,
headers=headers,
output_schema=output_schema,
)
logger.debug("β
Request validation passed")
result = await self._make_request(
"POST", f"{API_BASE_URL}/searchscraper", json=request.model_dump()
)
logger.info("β¨ Searchscraper request completed successfully")
return result
async def get_searchscraper(self, request_id: str):
"""Get the result of a previous searchscraper request"""
logger.info(f"π Fetching searchscraper result for request {request_id}")
# Validate input using Pydantic model
GetSearchScraperRequest(request_id=request_id)
logger.debug("β
Request ID validation passed")
result = await self._make_request(
"GET", f"{API_BASE_URL}/searchscraper/{request_id}"
)
logger.info(f"β¨ Successfully retrieved result for request {request_id}")
return result
async def close(self):
"""Close the session and stop the queue processor."""
logger.info("π Closing AsyncClient session")
await self.stop_queue_processor()
await self.session.close()
logger.debug("β
Session closed successfully")
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
await self.close()