@@ -280,6 +280,9 @@ class Session:
280280 user's device
281281 :param dxfeed_tos_compliant:
282282 whether to use the dxfeed TOS-compliant API endpoint for the streamer
283+ :param proxy:
284+ if provided, all requests will be made through this proxy, as well as
285+ web socket connections for streamers.
283286 """
284287
285288 def __init__ (
@@ -291,6 +294,7 @@ def __init__(
291294 is_test : bool = False ,
292295 two_factor_authentication : Optional [str ] = None ,
293296 dxfeed_tos_compliant : bool = False ,
297+ proxy : Optional [str ] = None ,
294298 ):
295299 body = {"login" : login , "remember-me" : remember_me }
296300 if password is not None :
@@ -303,14 +307,16 @@ def __init__(
303307 )
304308 #: Whether this is a cert or real session
305309 self .is_test = is_test
310+ #: Proxy URL to use for requests and web sockets
311+ self .proxy = proxy
306312 # The headers to use for API requests
307313 headers = {
308314 "Accept" : "application/json" ,
309315 "Content-Type" : "application/json" ,
310316 }
311317 #: httpx client for sync requests
312318 self .sync_client = Client (
313- base_url = (CERT_URL if is_test else API_URL ), headers = headers
319+ base_url = (CERT_URL if is_test else API_URL ), headers = headers , proxy = proxy
314320 )
315321 if two_factor_authentication is not None :
316322 response = self .sync_client .post (
@@ -330,7 +336,9 @@ def __init__(
330336 self .sync_client .headers .update ({"Authorization" : self .session_token })
331337 #: httpx client for async requests
332338 self .async_client = AsyncClient (
333- base_url = self .sync_client .base_url , headers = self .sync_client .headers .copy ()
339+ base_url = self .sync_client .base_url ,
340+ headers = self .sync_client .headers .copy (),
341+ proxy = proxy ,
334342 )
335343
336344 # Pull streamer tokens and urls
@@ -345,6 +353,12 @@ def __init__(
345353 #: URL for dxfeed websocket
346354 self .dxlink_url = data ["dxlink-url" ]
347355
356+ def __enter__ (self ):
357+ return self
358+
359+ def __exit__ (self , * exc ):
360+ self .destroy ()
361+
348362 async def _a_get (self , url , ** kwargs ) -> dict [str , Any ]:
349363 response = await self .async_client .get (url , timeout = 30 , ** kwargs )
350364 return validate_and_parse (response )
@@ -468,6 +482,8 @@ def deserialize(cls, serialized: str) -> Self:
468482 "Content-Type" : "application/json" ,
469483 "Authorization" : self .session_token ,
470484 }
471- self .sync_client = Client (base_url = base_url , headers = headers )
472- self .async_client = AsyncClient (base_url = base_url , headers = headers )
485+ self .sync_client = Client (base_url = base_url , headers = headers , proxy = self .proxy )
486+ self .async_client = AsyncClient (
487+ base_url = base_url , headers = headers , proxy = self .proxy
488+ )
473489 return self
0 commit comments