@@ -585,49 +585,48 @@ async def get_snapshot(
585
585
if uri is None :
586
586
return None
587
587
588
- # Create a new session with appropriate auth
589
- connector = TCPConnector (ssl = _NO_VERIFY_SSL_CONTEXT )
590
- middlewares = []
591
- auth = None
588
+ auth : BasicAuth | None = None
589
+ middlewares : tuple [DigestAuthMiddleware , ...] | None = None
592
590
593
591
if self .user and self .passwd :
594
592
if basic_auth :
595
593
auth = BasicAuth (self .user , self .passwd )
596
594
else :
597
595
# Use DigestAuthMiddleware for digest auth
598
- middlewares . append (DigestAuthMiddleware (self .user , self .passwd ))
596
+ middlewares = (DigestAuthMiddleware (self .user , self .passwd ), )
599
597
600
- async with ClientSession (
601
- connector = connector , auth = auth , middlewares = middlewares
602
- ) as session :
603
- response = await self ._try_snapshot_uri_with_session (session , uri )
604
- content = await response .read ()
598
+ response = await self ._try_snapshot_uri (uri , auth = auth , middlewares = middlewares )
599
+ content = await response .read ()
605
600
606
- # If the request fails with a 401, make sure to strip any
607
- # sample user/pass from the URL and try again
608
- if (
609
- response .status == 401
610
- and (stripped_uri := strip_user_pass_url (uri ))
611
- and stripped_uri != uri
612
- ):
613
- response = await self ._try_snapshot_uri_with_session (
614
- session , stripped_uri
615
- )
616
- content = await response .read ()
601
+ # If the request fails with a 401, strip user/pass from URL and retry
602
+ if (
603
+ response .status == 401
604
+ and (stripped_uri := strip_user_pass_url (uri ))
605
+ and stripped_uri != uri
606
+ ):
607
+ response = await self ._try_snapshot_uri (
608
+ stripped_uri , auth = auth , middlewares = middlewares
609
+ )
610
+ content = await response .read ()
617
611
618
- if response .status == 401 :
619
- raise ONVIFAuthError (f"Failed to authenticate to { uri } " )
612
+ if response .status == 401 :
613
+ raise ONVIFAuthError (f"Failed to authenticate to { uri } " )
620
614
621
- if response .status < 300 :
622
- return content
615
+ if response .status < 300 :
616
+ return content
623
617
624
- return None
618
+ return None
625
619
626
- async def _try_snapshot_uri_with_session (
627
- self , session : ClientSession , uri : str
620
+ async def _try_snapshot_uri (
621
+ self ,
622
+ uri : str ,
623
+ auth : BasicAuth | None = None ,
624
+ middlewares : tuple [DigestAuthMiddleware , ...] | None = None ,
628
625
) -> aiohttp .ClientResponse :
629
626
try :
630
- return await session .get (uri )
627
+ return await self ._snapshot_client .get (
628
+ uri , auth = auth , middlewares = middlewares
629
+ )
631
630
except TimeoutError as error :
632
631
raise ONVIFTimeoutError (
633
632
f"Timed out fetching { obscure_user_pass_url (uri )} : { error } "
0 commit comments