add new tool: to_captcha
This commit is contained in:
parent
50589232d6
commit
f1f32d8366
14 changed files with 1008 additions and 130 deletions
|
|
@ -7,19 +7,21 @@ from api.clients.browser_rpc_contracts import BrowserRpcError
|
|||
|
||||
class BrowserRpcClient:
|
||||
def __init__(self, rpc_url: str, session: aiohttp.ClientSession) -> None:
|
||||
self._rpc_url = rpc_url
|
||||
self._run_url = rpc_url.rstrip("/")
|
||||
if self._run_url.endswith("/run"):
|
||||
self._base_url = self._run_url[: -len("/run")]
|
||||
else:
|
||||
self._base_url = self._run_url
|
||||
self._run_url = f"{self._base_url}/run"
|
||||
self._session = session
|
||||
|
||||
async def run(self, task: str, timeout_sec: float) -> dict[str, Any]:
|
||||
payload = {"task": task}
|
||||
timeout = aiohttp.ClientTimeout(total=timeout_sec)
|
||||
|
||||
async def _post_json(self, url: str, payload: dict[str, Any], timeout_sec: float | None = None) -> dict[str, Any]:
|
||||
timeout = aiohttp.ClientTimeout(total=timeout_sec) if timeout_sec is not None else None
|
||||
try:
|
||||
async with self._session.post(self._rpc_url, json=payload, timeout=timeout) as response:
|
||||
async with self._session.post(url, json=payload, timeout=timeout) as response:
|
||||
if response.status >= 400:
|
||||
body = await response.text()
|
||||
raise BrowserRpcError(f"RPC HTTP: {response.status}: {body}")
|
||||
|
||||
try:
|
||||
data = await response.json(content_type=None)
|
||||
except aiohttp.ContentTypeError as exc:
|
||||
|
|
@ -29,10 +31,22 @@ class BrowserRpcClient:
|
|||
|
||||
if not isinstance(data, dict):
|
||||
raise BrowserRpcError("RPC returned invalid payload type")
|
||||
|
||||
return data
|
||||
|
||||
async def run(self, task_id: str, task: str, timeout_sec: float) -> dict[str, Any]:
|
||||
return await self._post_json(self._run_url, {"task_id": task_id, "task": task}, timeout_sec=timeout_sec)
|
||||
|
||||
async def run_browser_task(rpc_url: str, task: str, timeout_sec: float) -> dict[str, Any]:
|
||||
async def verify_captcha(self, task_id: str) -> dict[str, Any]:
|
||||
return await self._post_json(f"{self._base_url}/verify", {"task_id": task_id}, timeout_sec=15)
|
||||
|
||||
async def resume(self, task_id: str, timeout_sec: float) -> dict[str, Any]:
|
||||
return await self._post_json(f"{self._base_url}/resume", {"task_id": task_id}, timeout_sec=timeout_sec)
|
||||
|
||||
async def abort(self, task_id: str, reason: str | None = None) -> dict[str, Any]:
|
||||
return await self._post_json(f"{self._base_url}/abort", {"task_id": task_id, "reason": reason}, timeout_sec=15)
|
||||
|
||||
|
||||
async def run_browser_task(rpc_url: str, task_id: str, task: str, timeout_sec: float) -> dict[str, Any]:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
return await BrowserRpcClient(rpc_url, session=session).run(task=task, timeout_sec=timeout_sec)
|
||||
client = BrowserRpcClient(rpc_url, session=session)
|
||||
return await client.run(task_id=task_id, task=task, timeout_sec=timeout_sec)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue