Skip to content

Commit 7f4be31

Browse files
committed
replace dict with Viewport type
1 parent 523882f commit 7f4be31

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

sentience/async_api.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
Snapshot,
2727
SnapshotOptions,
2828
StorageState,
29+
Viewport,
2930
WaitResult,
3031
)
3132

@@ -51,7 +52,7 @@ def __init__(
5152
storage_state: str | Path | StorageState | dict | None = None,
5253
record_video_dir: str | Path | None = None,
5354
record_video_size: dict[str, int] | None = None,
54-
viewport: dict[str, int] | None = None,
55+
viewport: Viewport | dict[str, int] | None = None,
5556
):
5657
"""
5758
Initialize Async Sentience browser
@@ -66,8 +67,11 @@ def __init__(
6667
storage_state: Optional storage state to inject (cookies + localStorage)
6768
record_video_dir: Optional directory path to save video recordings
6869
record_video_size: Optional video resolution as dict with 'width' and 'height' keys
69-
viewport: Optional viewport size as dict with 'width' and 'height' keys.
70-
Defaults to {"width": 1280, "height": 800}
70+
viewport: Optional viewport size as Viewport object or dict with 'width' and 'height' keys.
71+
Examples: Viewport(width=1280, height=800) (default)
72+
Viewport(width=1920, height=1080) (Full HD)
73+
{"width": 1280, "height": 800} (dict also supported)
74+
If None, defaults to Viewport(width=1280, height=800).
7175
"""
7276
self.api_key = api_key
7377
# Only set api_url if api_key is provided, otherwise None (free tier)
@@ -94,8 +98,13 @@ def __init__(
9498
self.record_video_dir = record_video_dir
9599
self.record_video_size = record_video_size or {"width": 1280, "height": 800}
96100

97-
# Viewport configuration
98-
self.viewport = viewport or {"width": 1280, "height": 800}
101+
# Viewport configuration - convert dict to Viewport if needed
102+
if viewport is None:
103+
self.viewport = Viewport(width=1280, height=800)
104+
elif isinstance(viewport, dict):
105+
self.viewport = Viewport(width=viewport["width"], height=viewport["height"])
106+
else:
107+
self.viewport = viewport
99108

100109
self.playwright: Playwright | None = None
101110
self.context: BrowserContext | None = None
@@ -185,7 +194,7 @@ async def start(self) -> None:
185194
"user_data_dir": user_data_dir,
186195
"headless": False,
187196
"args": args,
188-
"viewport": self.viewport,
197+
"viewport": {"width": self.viewport.width, "height": self.viewport.height},
189198
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
190199
}
191200

sentience/browser.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from playwright.sync_api import BrowserContext, Page, Playwright, sync_playwright
1313

1414
from sentience._extension_loader import find_extension_path
15-
from sentience.models import ProxyConfig, StorageState
15+
from sentience.models import ProxyConfig, StorageState, Viewport
1616

1717
# Import stealth for bot evasion (optional - graceful fallback if not available)
1818
try:
@@ -36,7 +36,7 @@ def __init__(
3636
storage_state: str | Path | StorageState | dict | None = None,
3737
record_video_dir: str | Path | None = None,
3838
record_video_size: dict[str, int] | None = None,
39-
viewport: dict[str, int] | None = None,
39+
viewport: Viewport | dict[str, int] | None = None,
4040
):
4141
"""
4242
Initialize Sentience browser
@@ -69,11 +69,11 @@ def __init__(
6969
Examples: {"width": 1280, "height": 800} (default)
7070
{"width": 1920, "height": 1080} (1080p)
7171
If None, defaults to 1280x800.
72-
viewport: Optional viewport size as dict with 'width' and 'height' keys.
73-
Examples: {"width": 1280, "height": 800} (default)
74-
{"width": 1920, "height": 1080} (Full HD)
75-
{"width": 375, "height": 667} (iPhone)
76-
If None, defaults to 1280x800.
72+
viewport: Optional viewport size as Viewport object or dict with 'width' and 'height' keys.
73+
Examples: Viewport(width=1280, height=800) (default)
74+
Viewport(width=1920, height=1080) (Full HD)
75+
{"width": 1280, "height": 800} (dict also supported)
76+
If None, defaults to Viewport(width=1280, height=800).
7777
"""
7878
self.api_key = api_key
7979
# Only set api_url if api_key is provided, otherwise None (free tier)
@@ -101,8 +101,13 @@ def __init__(
101101
self.record_video_dir = record_video_dir
102102
self.record_video_size = record_video_size or {"width": 1280, "height": 800}
103103

104-
# Viewport configuration
105-
self.viewport = viewport or {"width": 1280, "height": 800}
104+
# Viewport configuration - convert dict to Viewport if needed
105+
if viewport is None:
106+
self.viewport = Viewport(width=1280, height=800)
107+
elif isinstance(viewport, dict):
108+
self.viewport = Viewport(width=viewport["width"], height=viewport["height"])
109+
else:
110+
self.viewport = viewport
106111

107112
self.playwright: Playwright | None = None
108113
self.context: BrowserContext | None = None
@@ -201,7 +206,7 @@ def start(self) -> None:
201206
"user_data_dir": user_data_dir,
202207
"headless": False, # IMPORTANT: See note above
203208
"args": args,
204-
"viewport": self.viewport,
209+
"viewport": {"width": self.viewport.width, "height": self.viewport.height},
205210
# Remove "HeadlessChrome" from User Agent automatically
206211
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",
207212
}

0 commit comments

Comments
 (0)