@@ -19,6 +19,9 @@ class ProxyFormat(Enum):
1919 AIOHTTP = "aiohttp"
2020 """AIOHTTP format, for use in aiohttp library HTTP calls"""
2121
22+ PLAYWRIGHT = "playwright"
23+ """Playwright format, for use in Playwright browser automation"""
24+
2225 URL = "url"
2326 """URL format, for use in URL strings"""
2427
@@ -90,29 +93,51 @@ def format(
9093 >>> proxy.format(ProxyFormat.HTTPX)
9194 {'http://': 'http://user:[email protected] :8080', 'https://': 'http://user:[email protected] :8080'} 9295 """
96+ # Convert to ProxyFormat enum, handling both string and enum inputs
9397 if isinstance (format_type , str ):
94- format_type = ProxyFormat (format_type )
98+ try :
99+ format_type = ProxyFormat (format_type )
100+ except ValueError :
101+ raise ValueError (
102+ f"Invalid format type: '{ format_type } '. Valid options are: { [f .value for f in ProxyFormat ]} "
103+ )
104+ elif not isinstance (format_type , ProxyFormat ):
105+ raise ValueError (
106+ f"Invalid format type: { type (format_type ).__name__ } . Expected ProxyFormat enum or string."
107+ )
108+
109+ format_handlers = {
110+ ProxyFormat .URL : lambda : self .to_url (kwargs .get ("protocol" , "http" )),
111+ ProxyFormat .REQUESTS : lambda : self ._format_requests (kwargs ),
112+ ProxyFormat .CURL : lambda : ["-x" , self .to_url ("http" )],
113+ ProxyFormat .HTTPX : lambda : self ._format_httpx (),
114+ ProxyFormat .AIOHTTP : lambda : self .to_url ("http" ),
115+ ProxyFormat .PLAYWRIGHT : lambda : self ._format_playwright (),
116+ }
117+
118+ handler = format_handlers .get (format_type )
119+ if handler is None :
120+ raise ValueError (f"Unsupported format: { format_type } " )
95121
96- if format_type == ProxyFormat .URL :
97- protocol = kwargs .get ("protocol" , "http" )
98- return self .to_url (protocol )
122+ return handler ()
99123
100- elif format_type == ProxyFormat .REQUESTS :
101- protocols = kwargs .get ("protocols" , self .protocols or ["http" , "https" ])
102- proxy_url = self .to_url ("http" )
103- return {protocol : proxy_url for protocol in protocols }
124+ def _format_requests (self , kwargs ):
125+ """Format proxy for requests library."""
126+ protocols = kwargs .get ("protocols" , self .protocols or ["http" , "https" ])
127+ proxy_url = self .to_url ("http" )
128+ return {protocol : proxy_url for protocol in protocols }
104129
105- elif format_type == ProxyFormat .CURL :
106- return ["-x" , self .to_url ("http" )]
130+ def _format_httpx (self ):
131+ """Format proxy for httpx library."""
132+ proxy_url = self .to_url ("http" )
133+ return {"http://" : proxy_url , "https://" : proxy_url }
107134
108- elif format_type == ProxyFormat .HTTPX :
109- # httpx uses 'http://' and 'https://' as keys
110- proxy_url = self .to_url ("http" )
111- return {"http://" : proxy_url , "https://" : proxy_url }
135+ def _format_playwright (self ):
136+ """Format proxy for Playwright."""
137+ playwright_proxy = {"server" : f"{ self .proxy_address } :{ self .port } " }
112138
113- elif format_type == ProxyFormat . AIOHTTP :
114- # aiohttp takes a single URL string
115- return self .to_url ( "http" )
139+ if self . username and self . password :
140+ playwright_proxy [ "username" ] = self . username
141+ playwright_proxy [ "password" ] = self .password
116142
117- else :
118- raise ValueError (f"Unsupported format: { format_type } " )
143+ return playwright_proxy
0 commit comments