File size: 1,237 Bytes
5012b98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Browserbase client - cloud browser sessions, live view, file download."""
import os
import requests

BASE = os.getenv("BROWSERBASE_API_BASE", "https://api.browserbase.com")
KEY = os.getenv("BROWSERBASE_API_KEY")


def _h():
    if not KEY:
        raise RuntimeError("BROWSERBASE_API_KEY not set")
    return {"X-BB-API-Key": KEY, "Content-Type": "application/json"}


def create_session(project_id=None, **kwargs):
    body = {k: v for k, v in kwargs.items() if v is not None}
    if project_id:
        body["projectId"] = project_id
    return requests.post(f"{BASE}/v1/sessions", headers=_h(), json=body, timeout=60).json()


def get_session(session_id: str):
    return requests.get(f"{BASE}/v1/sessions/{session_id}", headers=_h(), timeout=60).json()


def debug(session_id: str, command: str):
    return requests.post(f"{BASE}/v1/sessions/{session_id}/debug", headers=_h(), json={"cmd": command}, timeout=120).json()


def download_file(session_id: str, path: str):
    return requests.get(f"{BASE}/v1/sessions/{session_id}/file?path={path}", headers=_h(), timeout=120)


def update_session(session_id: str, **kwargs):
    return requests.post(f"{BASE}/v1/sessions/{session_id}", headers=_h(), json=kwargs, timeout=60).json()