interactions
Build and Push Docker Images / build-and-push (push) Successful in 24s

This commit is contained in:
2026-03-30 10:23:18 +02:00
parent df44afeaff
commit af3c06ec29
2 changed files with 107 additions and 14 deletions
+28 -14
View File
@@ -1,13 +1,13 @@
from fastapi import APIRouter, HTTPException, Header, Response
from typing import Optional
from typing import Optional, List, Literal, Union, Annotated
from urllib.parse import unquote
from app.config import API_KEY
from app.database import get_cached_data, save_to_cache
from pydantic import BaseModel
from pydantic import BaseModel, Field
from app.services.browser import (
visit_url_service,
screenshot_url_service,
click_selector_service,
interactions_service,
extract_seo_service,
extract_meta_tags_service,
capture_outgoing_calls_service,
@@ -16,11 +16,27 @@ from app.services.browser import (
router = APIRouter()
class ClickRequest(BaseModel):
url: str
class ClickInteraction(BaseModel):
action: Literal["click"]
selector: str
# Default behavior: return screenshot after clicking.
returnType: Optional[str] = "screenshot"
class TypeInteraction(BaseModel):
action: Literal["type"]
selector: str
text: str
Interaction = Annotated[
Union[ClickInteraction, TypeInteraction],
Field(discriminator="action"),
]
class InteractionsRequest(BaseModel):
url: str
returnType: Optional[Literal["screenshot", "html"]] = "screenshot"
interactions: List[Interaction]
@router.get("/")
async def visit_url(url: str, skipCache: bool = False, x_api_key: Optional[str] = Header(None)):
@@ -74,21 +90,19 @@ async def screenshot_url(url: str, fullPage: bool = True, x_api_key: Optional[st
print(f"Error taking screenshot for URL {decoded_url}: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.post("/click")
async def click_selector(payload: ClickRequest, x_api_key: Optional[str] = Header(None)):
@router.post("/interactions")
async def run_interactions(payload: InteractionsRequest, x_api_key: Optional[str] = Header(None)):
if not x_api_key or x_api_key != API_KEY:
raise HTTPException(status_code=401, detail="Invalid API key")
decoded_url = unquote(payload.url)
return_type = (payload.returnType or "screenshot").lower()
if return_type not in ("screenshot", "html"):
raise HTTPException(status_code=400, detail="Invalid returnType. Use 'screenshot' or 'html'.")
try:
result = await click_selector_service(decoded_url, payload.selector, return_type)
result = await interactions_service(decoded_url, payload.interactions, return_type)
if isinstance(result, dict) and result.get("status") == "error":
raise HTTPException(status_code=500, detail=result.get("error", "Click failed"))
raise HTTPException(status_code=500, detail=result.get("error", "Interaction failed"))
if return_type == "html":
if not isinstance(result, str):
@@ -101,7 +115,7 @@ async def click_selector(payload: ClickRequest, x_api_key: Optional[str] = Heade
except HTTPException:
raise
except Exception as e:
print(f"Error clicking selector on URL {decoded_url}: {e}")
print(f"Error running interactions on URL {decoded_url}: {e}")
raise HTTPException(status_code=500, detail=str(e))
@router.get("/seo")