Skip to content

Commit 86d7d69

Browse files
Add sync and async extension create examples
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 8df7530 commit 86d7d69

3 files changed

Lines changed: 69 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ Ready-to-run examples are available in `examples/`:
259259

260260
- `examples/async_batch_fetch.py`
261261
- `examples/async_crawl.py`
262+
- `examples/async_extension_create.py`
262263
- `examples/async_extension_list.py`
263264
- `examples/async_extract.py`
264265
- `examples/async_profile_list.py`
@@ -270,6 +271,7 @@ Ready-to-run examples are available in `examples/`:
270271
- `examples/async_web_search.py`
271272
- `examples/sync_batch_fetch.py`
272273
- `examples/sync_crawl.py`
274+
- `examples/sync_extension_create.py`
273275
- `examples/sync_extension_list.py`
274276
- `examples/sync_extract.py`
275277
- `examples/sync_profile_list.py`

examples/async_extension_create.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Asynchronous extension create example.
3+
4+
Run:
5+
export HYPERBROWSER_API_KEY="your_api_key"
6+
export HYPERBROWSER_EXTENSION_ZIP="/absolute/path/to/extension.zip"
7+
python3 examples/async_extension_create.py
8+
"""
9+
10+
import asyncio
11+
import os
12+
13+
from hyperbrowser import AsyncHyperbrowser
14+
from hyperbrowser.exceptions import HyperbrowserError
15+
from hyperbrowser.models.extension import CreateExtensionParams
16+
17+
18+
async def main() -> None:
19+
extension_zip_path = os.getenv("HYPERBROWSER_EXTENSION_ZIP")
20+
if extension_zip_path is None:
21+
raise HyperbrowserError("Set HYPERBROWSER_EXTENSION_ZIP before running")
22+
23+
async with AsyncHyperbrowser() as client:
24+
extension = await client.extensions.create(
25+
CreateExtensionParams(
26+
name="my-extension",
27+
file_path=extension_zip_path,
28+
)
29+
)
30+
print(f"Created extension: {extension.id} ({extension.name})")
31+
32+
33+
if __name__ == "__main__":
34+
asyncio.run(main())

examples/sync_extension_create.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Synchronous extension create example.
3+
4+
Run:
5+
export HYPERBROWSER_API_KEY="your_api_key"
6+
export HYPERBROWSER_EXTENSION_ZIP="/absolute/path/to/extension.zip"
7+
python3 examples/sync_extension_create.py
8+
"""
9+
10+
import os
11+
12+
from hyperbrowser import Hyperbrowser
13+
from hyperbrowser.exceptions import HyperbrowserError
14+
from hyperbrowser.models.extension import CreateExtensionParams
15+
16+
17+
def main() -> None:
18+
extension_zip_path = os.getenv("HYPERBROWSER_EXTENSION_ZIP")
19+
if extension_zip_path is None:
20+
raise HyperbrowserError("Set HYPERBROWSER_EXTENSION_ZIP before running")
21+
22+
with Hyperbrowser() as client:
23+
extension = client.extensions.create(
24+
CreateExtensionParams(
25+
name="my-extension",
26+
file_path=extension_zip_path,
27+
)
28+
)
29+
print(f"Created extension: {extension.id} ({extension.name})")
30+
31+
32+
if __name__ == "__main__":
33+
main()

0 commit comments

Comments
 (0)