11import time
2- from typing import Any , Dict
32
43from playwright .sync_api import Page , Playwright , sync_playwright
5- from pydantic import TypeAdapter
64
75from examples import (
86 BROWSERBASE_API_KEY ,
97 BROWSERBASE_PROJECT_ID ,
108 BROWSERBASE_CONNECT_URL ,
119 bb ,
1210)
13- from browserbase .types .session_create_params import (
14- ProxiesUnionMember1 ,
15- ProxiesUnionMember1ExternalProxyConfig ,
16- ProxiesUnionMember1BrowserbaseProxyConfig ,
17- ProxiesUnionMember1BrowserbaseProxyConfigGeolocation ,
18- )
1911
2012GRACEFUL_SHUTDOWN_TIMEOUT = 30000 # Assuming 30 seconds, adjust as needed
2113
@@ -26,52 +18,11 @@ def check_proxy_bytes(session_id: str) -> None:
2618 )
2719 time .sleep (GRACEFUL_SHUTDOWN_TIMEOUT / 1000 )
2820 updated_session = bb .sessions .retrieve (id = session_id )
29- print ("UPDATED SESSION" , updated_session )
3021 assert (
3122 updated_session .proxy_bytes is not None and updated_session .proxy_bytes > 0
3223 ), f"Proxy bytes: { updated_session .proxy_bytes } "
3324
3425
35- def generate_proxy_config (proxy_data : Dict [str , Any ]) -> ProxiesUnionMember1 :
36- """
37- Generate the appropriate ProxiesUnionMember1 type given a deeply nested JSON.
38-
39- :param proxy_data: A dictionary containing proxy configuration data
40- :return: An instance of ProxiesUnionMember1
41- """
42- if proxy_data .get ("type" ) == "browserbase" :
43- for key in ["geolocation" ]:
44- if proxy_data .get (key ) is None :
45- raise ValueError (f"Missing required key in proxy config: { key } " )
46-
47- geolocation = proxy_data ["geolocation" ]
48- for key in ["country" , "city" , "state" ]:
49- if geolocation .get (key ) is None :
50- raise ValueError (f"Missing required key in geolocation: { key } " )
51- return ProxiesUnionMember1BrowserbaseProxyConfig (
52- type = "browserbase" ,
53- domain_pattern = proxy_data .get ("domainPattern" , "" ),
54- geolocation = ProxiesUnionMember1BrowserbaseProxyConfigGeolocation (
55- country = geolocation .get ("country" , "" ),
56- city = geolocation .get ("city" , "" ),
57- state = geolocation .get ("state" , "" ),
58- ),
59- )
60- elif proxy_data .get ("type" ) == "external" :
61- for key in ["server" , "username" , "password" ]:
62- if proxy_data .get (key ) is None :
63- raise ValueError (f"Missing required key in proxy config: { key } " )
64- return ProxiesUnionMember1ExternalProxyConfig (
65- type = "external" ,
66- server = proxy_data ["server" ],
67- domain_pattern = proxy_data ["domainPattern" ],
68- username = proxy_data ["username" ],
69- password = proxy_data ["password" ],
70- )
71- else :
72- raise ValueError (f"Invalid proxy type: { proxy_data .get ('type' )} " )
73-
74-
7526def run_enable_via_create_session (playwright : Playwright ) -> None :
7627 session = bb .sessions .create (project_id = BROWSERBASE_PROJECT_ID , proxies = True )
7728
@@ -126,13 +77,10 @@ def run_geolocation_country(playwright: Playwright) -> None:
12677 session = bb .sessions .create (
12778 project_id = BROWSERBASE_PROJECT_ID ,
12879 proxies = [
129- TypeAdapter (ProxiesUnionMember1 ).validate_python (
130- {
131- "geolocation" : {"country" : "CA" },
132- "type" : "browserbase" ,
133- "test" : "swag" ,
134- }
135- )
80+ {
81+ "geolocation" : {"country" : "CA" },
82+ "type" : "browserbase" ,
83+ }
13684 ],
13785 )
13886
@@ -155,15 +103,13 @@ def run_geolocation_state(playwright: Playwright) -> None:
155103 session = bb .sessions .create (
156104 project_id = BROWSERBASE_PROJECT_ID ,
157105 proxies = [
158- generate_proxy_config (
159- {
160- "geolocation" : {
161- "country" : "US" ,
162- "state" : "NY" ,
163- },
164- "type" : "browserbase" ,
165- }
166- )
106+ {
107+ "geolocation" : {
108+ "country" : "US" ,
109+ "state" : "NY" ,
110+ },
111+ "type" : "browserbase" ,
112+ }
167113 ],
168114 )
169115
@@ -186,16 +132,14 @@ def run_geolocation_american_city(playwright: Playwright) -> None:
186132 session = bb .sessions .create (
187133 project_id = BROWSERBASE_PROJECT_ID ,
188134 proxies = [
189- generate_proxy_config (
190- {
191- "geolocation" : {
192- "city" : "Los Angeles" ,
193- "country" : "US" ,
194- "state" : "CA" ,
195- },
196- "type" : "browserbase" ,
197- }
198- )
135+ {
136+ "geolocation" : {
137+ "city" : "Los Angeles" ,
138+ "country" : "US" ,
139+ "state" : "CA" ,
140+ },
141+ "type" : "browserbase" ,
142+ }
199143 ],
200144 )
201145
@@ -218,15 +162,13 @@ def run_geolocation_non_american_city(playwright: Playwright) -> None:
218162 session = bb .sessions .create (
219163 project_id = BROWSERBASE_PROJECT_ID ,
220164 proxies = [
221- generate_proxy_config (
222- {
223- "geolocation" : {
224- "city" : "London" ,
225- "country" : "GB" ,
226- },
227- "type" : "browserbase" ,
228- }
229- )
165+ {
166+ "geolocation" : {
167+ "city" : "London" ,
168+ "country" : "GB" ,
169+ },
170+ "type" : "browserbase" ,
171+ }
230172 ],
231173 )
232174
@@ -247,6 +189,7 @@ def run_geolocation_non_american_city(playwright: Playwright) -> None:
247189
248190if __name__ == "__main__" :
249191 with sync_playwright () as playwright :
192+ # You can run any of these tests by uncommenting them
250193 run_enable_via_create_session (playwright )
251194 # run_enable_via_querystring_with_created_session(playwright)
252195 # run_geolocation_country(playwright)
0 commit comments