webfuse 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- webfuse-0.0.1/.gitignore +32 -0
- webfuse-0.0.1/PKG-INFO +173 -0
- webfuse-0.0.1/README.md +138 -0
- webfuse-0.0.1/pyproject.toml +78 -0
- webfuse-0.0.1/src/webfuse/__init__.py +52 -0
- webfuse-0.0.1/src/webfuse/async_client.py +186 -0
- webfuse-0.0.1/src/webfuse/async_session.py +362 -0
- webfuse-0.0.1/src/webfuse/client.py +197 -0
- webfuse-0.0.1/src/webfuse/exceptions.py +36 -0
- webfuse-0.0.1/src/webfuse/session.py +417 -0
- webfuse-0.0.1/test_sdk.py +64 -0
webfuse-0.0.1/.gitignore
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# OS
|
|
2
|
+
.DS_Store
|
|
3
|
+
|
|
4
|
+
# Python
|
|
5
|
+
__pycache__/
|
|
6
|
+
*.py[cod]
|
|
7
|
+
*$py.class
|
|
8
|
+
*.so
|
|
9
|
+
.Python
|
|
10
|
+
venv/
|
|
11
|
+
.venv/
|
|
12
|
+
mcp-venv/
|
|
13
|
+
*.egg-info/
|
|
14
|
+
.eggs/
|
|
15
|
+
dist/
|
|
16
|
+
build/
|
|
17
|
+
|
|
18
|
+
# IDE
|
|
19
|
+
.idea/
|
|
20
|
+
.vscode/
|
|
21
|
+
*.swp
|
|
22
|
+
*.swo
|
|
23
|
+
|
|
24
|
+
# Environment
|
|
25
|
+
.env
|
|
26
|
+
.env.local
|
|
27
|
+
|
|
28
|
+
# Node
|
|
29
|
+
node_modules/
|
|
30
|
+
|
|
31
|
+
# Logs
|
|
32
|
+
*.log
|
webfuse-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: webfuse
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Python SDK for automating browser sessions via Webfuse
|
|
5
|
+
Project-URL: Homepage, https://webfuse.com
|
|
6
|
+
Project-URL: Documentation, https://docs.webfuse.com/sdk/python
|
|
7
|
+
Project-URL: Repository, https://github.com/webfuse/webfuse-python
|
|
8
|
+
Project-URL: Changelog, https://github.com/webfuse/webfuse-python/blob/main/CHANGELOG.md
|
|
9
|
+
Author-email: Surfly <support@surfly.com>
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
Keywords: automation,browser,rpc,scraping,testing,web,webfuse
|
|
12
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Topic :: Internet :: WWW/HTTP :: Browsers
|
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
+
Classifier: Topic :: Software Development :: Testing
|
|
25
|
+
Classifier: Typing :: Typed
|
|
26
|
+
Requires-Python: >=3.8
|
|
27
|
+
Requires-Dist: httpx>=0.24.0
|
|
28
|
+
Provides-Extra: dev
|
|
29
|
+
Requires-Dist: black>=23.0.0; extra == 'dev'
|
|
30
|
+
Requires-Dist: mypy>=1.0.0; extra == 'dev'
|
|
31
|
+
Requires-Dist: pytest-asyncio>=0.21.0; extra == 'dev'
|
|
32
|
+
Requires-Dist: pytest>=7.0.0; extra == 'dev'
|
|
33
|
+
Requires-Dist: ruff>=0.1.0; extra == 'dev'
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
|
|
36
|
+
# Webfuse Python SDK
|
|
37
|
+
|
|
38
|
+
A Python client for automating browser sessions via the Webfuse RPC service.
|
|
39
|
+
|
|
40
|
+
## Installation
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pip install webfuse
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Quick Start
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
from webfuse import WebfuseClient
|
|
50
|
+
|
|
51
|
+
# Initialize the client with your API key
|
|
52
|
+
client = WebfuseClient(api_key="rk_your_api_key")
|
|
53
|
+
|
|
54
|
+
# Create a browser session
|
|
55
|
+
session = client.create_session(space_id="1234")
|
|
56
|
+
|
|
57
|
+
# Automate the browser
|
|
58
|
+
session.goto("https://example.com")
|
|
59
|
+
session.click("#login-button")
|
|
60
|
+
session.type("#username", "user@example.com")
|
|
61
|
+
session.type("#password", "secret123")
|
|
62
|
+
session.click("#submit")
|
|
63
|
+
|
|
64
|
+
# Take a screenshot
|
|
65
|
+
screenshot = session.screenshot()
|
|
66
|
+
with open("result.png", "wb") as f:
|
|
67
|
+
f.write(screenshot)
|
|
68
|
+
|
|
69
|
+
# End the session when done
|
|
70
|
+
session.end()
|
|
71
|
+
|
|
72
|
+
# Don't forget to close the client
|
|
73
|
+
client.close()
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Using Context Managers
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
from webfuse import WebfuseClient
|
|
80
|
+
|
|
81
|
+
with WebfuseClient(api_key="rk_your_api_key") as client:
|
|
82
|
+
session = client.create_session(space_id="1234")
|
|
83
|
+
session.goto("https://example.com")
|
|
84
|
+
session.click("button.submit")
|
|
85
|
+
session.end()
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Async Support
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
import asyncio
|
|
92
|
+
from webfuse import AsyncWebfuseClient
|
|
93
|
+
|
|
94
|
+
async def main():
|
|
95
|
+
async with AsyncWebfuseClient(api_key="rk_your_api_key") as client:
|
|
96
|
+
session = await client.create_session(space_id="1234")
|
|
97
|
+
|
|
98
|
+
await session.goto("https://example.com")
|
|
99
|
+
await session.click("#button")
|
|
100
|
+
await session.type("#input", "Hello World")
|
|
101
|
+
|
|
102
|
+
screenshot = await session.screenshot()
|
|
103
|
+
|
|
104
|
+
await session.end()
|
|
105
|
+
|
|
106
|
+
asyncio.run(main())
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Available Methods
|
|
110
|
+
|
|
111
|
+
### Navigation
|
|
112
|
+
|
|
113
|
+
- `session.goto(url, new_tab=False)` - Navigate to a URL
|
|
114
|
+
- `session.open_tab(url)` - Open a new browser tab
|
|
115
|
+
|
|
116
|
+
### Mouse Actions
|
|
117
|
+
|
|
118
|
+
- `session.click(selector)` - Left-click an element
|
|
119
|
+
- `session.right_click(selector)` - Right-click an element
|
|
120
|
+
- `session.middle_click(selector)` - Middle-click an element
|
|
121
|
+
- `session.hover(selector)` - Move mouse to element
|
|
122
|
+
- `session.scroll(selector, amount, direction="vertical")` - Scroll an element
|
|
123
|
+
|
|
124
|
+
### Keyboard Actions
|
|
125
|
+
|
|
126
|
+
- `session.type(selector, text)` - Type text into an input
|
|
127
|
+
- `session.press(selector, key, modifiers=None)` - Press a keyboard key
|
|
128
|
+
|
|
129
|
+
### Screenshots & DOM
|
|
130
|
+
|
|
131
|
+
- `session.screenshot()` - Take a screenshot (returns PNG bytes)
|
|
132
|
+
- `session.dom_snapshot()` - Get text representation of the DOM
|
|
133
|
+
|
|
134
|
+
### Utilities
|
|
135
|
+
|
|
136
|
+
- `session.wait(ms)` - Wait for specified milliseconds
|
|
137
|
+
- `session.get_functions()` - List available automation functions
|
|
138
|
+
|
|
139
|
+
### Session Management
|
|
140
|
+
|
|
141
|
+
- `session.end()` - End the session
|
|
142
|
+
- `session.is_active` - Check if session is still active
|
|
143
|
+
|
|
144
|
+
## Configuration
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
client = WebfuseClient(
|
|
148
|
+
api_key="rk_your_api_key",
|
|
149
|
+
rpc_url="https://rpc.webfuse.com", # Custom RPC URL
|
|
150
|
+
timeout=60.0, # Request timeout in seconds
|
|
151
|
+
)
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Error Handling
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
from webfuse import WebfuseClient, WebfuseError, AuthenticationError, AutomationError
|
|
158
|
+
|
|
159
|
+
try:
|
|
160
|
+
with WebfuseClient(api_key="rk_your_api_key") as client:
|
|
161
|
+
session = client.create_session(space_id="1234")
|
|
162
|
+
session.click("#nonexistent-element")
|
|
163
|
+
except AuthenticationError:
|
|
164
|
+
print("Invalid API key")
|
|
165
|
+
except AutomationError as e:
|
|
166
|
+
print(f"Automation failed: {e.message}")
|
|
167
|
+
except WebfuseError as e:
|
|
168
|
+
print(f"Error: {e.message}")
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## License
|
|
172
|
+
|
|
173
|
+
MIT
|
webfuse-0.0.1/README.md
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# Webfuse Python SDK
|
|
2
|
+
|
|
3
|
+
A Python client for automating browser sessions via the Webfuse RPC service.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install webfuse
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
from webfuse import WebfuseClient
|
|
15
|
+
|
|
16
|
+
# Initialize the client with your API key
|
|
17
|
+
client = WebfuseClient(api_key="rk_your_api_key")
|
|
18
|
+
|
|
19
|
+
# Create a browser session
|
|
20
|
+
session = client.create_session(space_id="1234")
|
|
21
|
+
|
|
22
|
+
# Automate the browser
|
|
23
|
+
session.goto("https://example.com")
|
|
24
|
+
session.click("#login-button")
|
|
25
|
+
session.type("#username", "user@example.com")
|
|
26
|
+
session.type("#password", "secret123")
|
|
27
|
+
session.click("#submit")
|
|
28
|
+
|
|
29
|
+
# Take a screenshot
|
|
30
|
+
screenshot = session.screenshot()
|
|
31
|
+
with open("result.png", "wb") as f:
|
|
32
|
+
f.write(screenshot)
|
|
33
|
+
|
|
34
|
+
# End the session when done
|
|
35
|
+
session.end()
|
|
36
|
+
|
|
37
|
+
# Don't forget to close the client
|
|
38
|
+
client.close()
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Using Context Managers
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from webfuse import WebfuseClient
|
|
45
|
+
|
|
46
|
+
with WebfuseClient(api_key="rk_your_api_key") as client:
|
|
47
|
+
session = client.create_session(space_id="1234")
|
|
48
|
+
session.goto("https://example.com")
|
|
49
|
+
session.click("button.submit")
|
|
50
|
+
session.end()
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Async Support
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
import asyncio
|
|
57
|
+
from webfuse import AsyncWebfuseClient
|
|
58
|
+
|
|
59
|
+
async def main():
|
|
60
|
+
async with AsyncWebfuseClient(api_key="rk_your_api_key") as client:
|
|
61
|
+
session = await client.create_session(space_id="1234")
|
|
62
|
+
|
|
63
|
+
await session.goto("https://example.com")
|
|
64
|
+
await session.click("#button")
|
|
65
|
+
await session.type("#input", "Hello World")
|
|
66
|
+
|
|
67
|
+
screenshot = await session.screenshot()
|
|
68
|
+
|
|
69
|
+
await session.end()
|
|
70
|
+
|
|
71
|
+
asyncio.run(main())
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Available Methods
|
|
75
|
+
|
|
76
|
+
### Navigation
|
|
77
|
+
|
|
78
|
+
- `session.goto(url, new_tab=False)` - Navigate to a URL
|
|
79
|
+
- `session.open_tab(url)` - Open a new browser tab
|
|
80
|
+
|
|
81
|
+
### Mouse Actions
|
|
82
|
+
|
|
83
|
+
- `session.click(selector)` - Left-click an element
|
|
84
|
+
- `session.right_click(selector)` - Right-click an element
|
|
85
|
+
- `session.middle_click(selector)` - Middle-click an element
|
|
86
|
+
- `session.hover(selector)` - Move mouse to element
|
|
87
|
+
- `session.scroll(selector, amount, direction="vertical")` - Scroll an element
|
|
88
|
+
|
|
89
|
+
### Keyboard Actions
|
|
90
|
+
|
|
91
|
+
- `session.type(selector, text)` - Type text into an input
|
|
92
|
+
- `session.press(selector, key, modifiers=None)` - Press a keyboard key
|
|
93
|
+
|
|
94
|
+
### Screenshots & DOM
|
|
95
|
+
|
|
96
|
+
- `session.screenshot()` - Take a screenshot (returns PNG bytes)
|
|
97
|
+
- `session.dom_snapshot()` - Get text representation of the DOM
|
|
98
|
+
|
|
99
|
+
### Utilities
|
|
100
|
+
|
|
101
|
+
- `session.wait(ms)` - Wait for specified milliseconds
|
|
102
|
+
- `session.get_functions()` - List available automation functions
|
|
103
|
+
|
|
104
|
+
### Session Management
|
|
105
|
+
|
|
106
|
+
- `session.end()` - End the session
|
|
107
|
+
- `session.is_active` - Check if session is still active
|
|
108
|
+
|
|
109
|
+
## Configuration
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
client = WebfuseClient(
|
|
113
|
+
api_key="rk_your_api_key",
|
|
114
|
+
rpc_url="https://rpc.webfuse.com", # Custom RPC URL
|
|
115
|
+
timeout=60.0, # Request timeout in seconds
|
|
116
|
+
)
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Error Handling
|
|
120
|
+
|
|
121
|
+
```python
|
|
122
|
+
from webfuse import WebfuseClient, WebfuseError, AuthenticationError, AutomationError
|
|
123
|
+
|
|
124
|
+
try:
|
|
125
|
+
with WebfuseClient(api_key="rk_your_api_key") as client:
|
|
126
|
+
session = client.create_session(space_id="1234")
|
|
127
|
+
session.click("#nonexistent-element")
|
|
128
|
+
except AuthenticationError:
|
|
129
|
+
print("Invalid API key")
|
|
130
|
+
except AutomationError as e:
|
|
131
|
+
print(f"Automation failed: {e.message}")
|
|
132
|
+
except WebfuseError as e:
|
|
133
|
+
print(f"Error: {e.message}")
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## License
|
|
137
|
+
|
|
138
|
+
MIT
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "webfuse"
|
|
7
|
+
version = "0.0.1"
|
|
8
|
+
description = "Python SDK for automating browser sessions via Webfuse"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.8"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Surfly", email = "support@surfly.com" }
|
|
14
|
+
]
|
|
15
|
+
keywords = [
|
|
16
|
+
"webfuse",
|
|
17
|
+
"browser",
|
|
18
|
+
"automation",
|
|
19
|
+
"rpc",
|
|
20
|
+
"web",
|
|
21
|
+
"scraping",
|
|
22
|
+
"testing",
|
|
23
|
+
]
|
|
24
|
+
classifiers = [
|
|
25
|
+
"Development Status :: 2 - Pre-Alpha",
|
|
26
|
+
"Intended Audience :: Developers",
|
|
27
|
+
"License :: OSI Approved :: MIT License",
|
|
28
|
+
"Operating System :: OS Independent",
|
|
29
|
+
"Programming Language :: Python :: 3",
|
|
30
|
+
"Programming Language :: Python :: 3.8",
|
|
31
|
+
"Programming Language :: Python :: 3.9",
|
|
32
|
+
"Programming Language :: Python :: 3.10",
|
|
33
|
+
"Programming Language :: Python :: 3.11",
|
|
34
|
+
"Programming Language :: Python :: 3.12",
|
|
35
|
+
"Topic :: Internet :: WWW/HTTP :: Browsers",
|
|
36
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
37
|
+
"Topic :: Software Development :: Testing",
|
|
38
|
+
"Typing :: Typed",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
dependencies = [
|
|
42
|
+
"httpx>=0.24.0",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
[project.optional-dependencies]
|
|
46
|
+
dev = [
|
|
47
|
+
"pytest>=7.0.0",
|
|
48
|
+
"pytest-asyncio>=0.21.0",
|
|
49
|
+
"black>=23.0.0",
|
|
50
|
+
"ruff>=0.1.0",
|
|
51
|
+
"mypy>=1.0.0",
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
[project.urls]
|
|
55
|
+
Homepage = "https://webfuse.com"
|
|
56
|
+
Documentation = "https://docs.webfuse.com/sdk/python"
|
|
57
|
+
Repository = "https://github.com/webfuse/webfuse-python"
|
|
58
|
+
Changelog = "https://github.com/webfuse/webfuse-python/blob/main/CHANGELOG.md"
|
|
59
|
+
|
|
60
|
+
[tool.hatch.build.targets.wheel]
|
|
61
|
+
packages = ["src/webfuse"]
|
|
62
|
+
|
|
63
|
+
[tool.black]
|
|
64
|
+
line-length = 100
|
|
65
|
+
target-version = ["py38"]
|
|
66
|
+
|
|
67
|
+
[tool.ruff]
|
|
68
|
+
line-length = 100
|
|
69
|
+
target-version = "py38"
|
|
70
|
+
select = ["E", "F", "I", "N", "W"]
|
|
71
|
+
|
|
72
|
+
[tool.mypy]
|
|
73
|
+
python_version = "3.8"
|
|
74
|
+
strict = true
|
|
75
|
+
|
|
76
|
+
[tool.pytest.ini_options]
|
|
77
|
+
asyncio_mode = "auto"
|
|
78
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Webfuse Python SDK
|
|
3
|
+
|
|
4
|
+
A Python client for automating browser sessions via the Webfuse RPC service.
|
|
5
|
+
|
|
6
|
+
Example:
|
|
7
|
+
from webfuse import WebfuseClient
|
|
8
|
+
|
|
9
|
+
client = WebfuseClient(api_key="rk_your_api_key")
|
|
10
|
+
|
|
11
|
+
# Create a session
|
|
12
|
+
session = client.create_session(space_id="1234")
|
|
13
|
+
|
|
14
|
+
# Automate the browser
|
|
15
|
+
session.goto("https://example.com")
|
|
16
|
+
session.click("#login-button")
|
|
17
|
+
session.type("#username", "user@example.com")
|
|
18
|
+
|
|
19
|
+
# Take a screenshot
|
|
20
|
+
screenshot = session.screenshot()
|
|
21
|
+
|
|
22
|
+
# End the session
|
|
23
|
+
session.end()
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
from .client import WebfuseClient
|
|
27
|
+
from .session import Session
|
|
28
|
+
from .async_client import AsyncWebfuseClient
|
|
29
|
+
from .async_session import AsyncSession
|
|
30
|
+
from .exceptions import (
|
|
31
|
+
WebfuseError,
|
|
32
|
+
AuthenticationError,
|
|
33
|
+
SessionError,
|
|
34
|
+
AutomationError,
|
|
35
|
+
TimeoutError,
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
__version__ = "0.1.0"
|
|
39
|
+
__all__ = [
|
|
40
|
+
# Sync API
|
|
41
|
+
"WebfuseClient",
|
|
42
|
+
"Session",
|
|
43
|
+
# Async API
|
|
44
|
+
"AsyncWebfuseClient",
|
|
45
|
+
"AsyncSession",
|
|
46
|
+
# Exceptions
|
|
47
|
+
"WebfuseError",
|
|
48
|
+
"AuthenticationError",
|
|
49
|
+
"SessionError",
|
|
50
|
+
"AutomationError",
|
|
51
|
+
"TimeoutError",
|
|
52
|
+
]
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Webfuse Async Client
|
|
3
|
+
|
|
4
|
+
Async version of the Webfuse client for use with asyncio.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import httpx
|
|
8
|
+
from typing import Optional, Dict, Any, List
|
|
9
|
+
|
|
10
|
+
from .async_session import AsyncSession
|
|
11
|
+
from .exceptions import WebfuseError, AuthenticationError, SessionError
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
DEFAULT_RPC_URL = "https://rpc.webfuse.com"
|
|
15
|
+
DEFAULT_TIMEOUT = 30.0
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class AsyncWebfuseClient:
|
|
19
|
+
"""
|
|
20
|
+
Async client for interacting with the Webfuse RPC service.
|
|
21
|
+
|
|
22
|
+
Args:
|
|
23
|
+
api_key: Your Webfuse API key (starts with 'rk_' or 'ck_')
|
|
24
|
+
rpc_url: URL of the RPC service (default: https://rpc.webfuse.com)
|
|
25
|
+
timeout: Default timeout for requests in seconds (default: 30)
|
|
26
|
+
|
|
27
|
+
Example:
|
|
28
|
+
async with AsyncWebfuseClient(api_key="rk_your_api_key") as client:
|
|
29
|
+
session = await client.create_session(space_id="1234")
|
|
30
|
+
await session.goto("https://example.com")
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
def __init__(
|
|
34
|
+
self,
|
|
35
|
+
api_key: str,
|
|
36
|
+
rpc_url: str = DEFAULT_RPC_URL,
|
|
37
|
+
timeout: float = DEFAULT_TIMEOUT,
|
|
38
|
+
):
|
|
39
|
+
if not api_key:
|
|
40
|
+
raise AuthenticationError("API key is required")
|
|
41
|
+
|
|
42
|
+
self.api_key = api_key
|
|
43
|
+
self.rpc_url = rpc_url.rstrip("/")
|
|
44
|
+
self.timeout = timeout
|
|
45
|
+
|
|
46
|
+
self._http_client = httpx.AsyncClient(
|
|
47
|
+
base_url=self.rpc_url,
|
|
48
|
+
headers={
|
|
49
|
+
"Authorization": f"Bearer {self.api_key}",
|
|
50
|
+
"Content-Type": "application/json",
|
|
51
|
+
},
|
|
52
|
+
timeout=timeout,
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
async def __aenter__(self):
|
|
56
|
+
return self
|
|
57
|
+
|
|
58
|
+
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
|
59
|
+
await self.close()
|
|
60
|
+
|
|
61
|
+
async def close(self):
|
|
62
|
+
"""Close the HTTP client."""
|
|
63
|
+
await self._http_client.aclose()
|
|
64
|
+
|
|
65
|
+
async def _request(
|
|
66
|
+
self,
|
|
67
|
+
method: str,
|
|
68
|
+
path: str,
|
|
69
|
+
json: Dict[str, Any] = None,
|
|
70
|
+
params: Dict[str, Any] = None,
|
|
71
|
+
) -> Dict[str, Any]:
|
|
72
|
+
"""Make an async HTTP request to the RPC service."""
|
|
73
|
+
try:
|
|
74
|
+
response = await self._http_client.request(
|
|
75
|
+
method=method,
|
|
76
|
+
url=path,
|
|
77
|
+
json=json,
|
|
78
|
+
params=params,
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
if response.status_code == 401:
|
|
82
|
+
raise AuthenticationError("Invalid API key")
|
|
83
|
+
elif response.status_code == 404:
|
|
84
|
+
raise SessionError("Resource not found", {"path": path})
|
|
85
|
+
elif response.status_code >= 400:
|
|
86
|
+
error_detail = response.text
|
|
87
|
+
try:
|
|
88
|
+
error_detail = response.json()
|
|
89
|
+
except Exception:
|
|
90
|
+
pass
|
|
91
|
+
raise WebfuseError(
|
|
92
|
+
f"Request failed with status {response.status_code}",
|
|
93
|
+
{"status": response.status_code, "detail": error_detail},
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
return response.json()
|
|
97
|
+
|
|
98
|
+
except httpx.TimeoutException:
|
|
99
|
+
from .exceptions import TimeoutError
|
|
100
|
+
raise TimeoutError(f"Request timed out after {self.timeout}s")
|
|
101
|
+
except httpx.RequestError as e:
|
|
102
|
+
raise WebfuseError(f"Request failed: {str(e)}")
|
|
103
|
+
|
|
104
|
+
async def health(self) -> Dict[str, Any]:
|
|
105
|
+
"""
|
|
106
|
+
Check the health of the RPC service.
|
|
107
|
+
|
|
108
|
+
Returns:
|
|
109
|
+
Health status information
|
|
110
|
+
"""
|
|
111
|
+
return await self._request("GET", "/health")
|
|
112
|
+
|
|
113
|
+
async def create_session(
|
|
114
|
+
self,
|
|
115
|
+
space_id: str,
|
|
116
|
+
metadata: Optional[Dict[str, Any]] = None,
|
|
117
|
+
) -> AsyncSession:
|
|
118
|
+
"""
|
|
119
|
+
Create a new browser session.
|
|
120
|
+
|
|
121
|
+
Args:
|
|
122
|
+
space_id: The Webfuse space ID to create the session in
|
|
123
|
+
metadata: Optional metadata to attach to the session
|
|
124
|
+
|
|
125
|
+
Returns:
|
|
126
|
+
An AsyncSession object for automating the browser
|
|
127
|
+
|
|
128
|
+
Example:
|
|
129
|
+
session = await client.create_session(space_id="1234")
|
|
130
|
+
await session.goto("https://example.com")
|
|
131
|
+
"""
|
|
132
|
+
response = await self._request(
|
|
133
|
+
"POST",
|
|
134
|
+
"/api/v1/sessions",
|
|
135
|
+
json={
|
|
136
|
+
"space_id": space_id,
|
|
137
|
+
"metadata": metadata or {},
|
|
138
|
+
},
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
session_id = response.get("session_id")
|
|
142
|
+
if not session_id:
|
|
143
|
+
raise SessionError("Failed to create session", response)
|
|
144
|
+
|
|
145
|
+
return AsyncSession(
|
|
146
|
+
client=self,
|
|
147
|
+
session_id=session_id,
|
|
148
|
+
space_id=space_id,
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
async def get_session(self, session_id: str, space_id: str) -> AsyncSession:
|
|
152
|
+
"""
|
|
153
|
+
Get an existing session by ID.
|
|
154
|
+
|
|
155
|
+
Args:
|
|
156
|
+
session_id: The session ID
|
|
157
|
+
space_id: The space ID the session belongs to
|
|
158
|
+
|
|
159
|
+
Returns:
|
|
160
|
+
An AsyncSession object for automating the browser
|
|
161
|
+
"""
|
|
162
|
+
# Verify the session exists
|
|
163
|
+
await self._request("GET", f"/api/v1/sessions/{session_id}")
|
|
164
|
+
|
|
165
|
+
return AsyncSession(
|
|
166
|
+
client=self,
|
|
167
|
+
session_id=session_id,
|
|
168
|
+
space_id=space_id,
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
async def list_sessions(self, space_id: Optional[str] = None) -> List[Dict[str, Any]]:
|
|
172
|
+
"""
|
|
173
|
+
List active sessions.
|
|
174
|
+
|
|
175
|
+
Args:
|
|
176
|
+
space_id: Optional space ID to filter by
|
|
177
|
+
|
|
178
|
+
Returns:
|
|
179
|
+
List of session information dictionaries
|
|
180
|
+
"""
|
|
181
|
+
params = {}
|
|
182
|
+
if space_id:
|
|
183
|
+
params["space_id"] = space_id
|
|
184
|
+
|
|
185
|
+
response = await self._request("GET", "/api/v1/sessions", params=params)
|
|
186
|
+
return response.get("sessions", [])
|