@@ -107,6 +107,9 @@ def __init__(
107
107
108
108
self ._is_nvr = False
109
109
110
+ self ._aiohttp_session : aiohttp .ClientSession = aiohttp .ClientSession (timeout = self ._timeout ,
111
+ connector = aiohttp .TCPConnector (verify_ssl = False ))
112
+
110
113
self .refresh_base_url ()
111
114
112
115
def enable_https (self , enable : bool ):
@@ -693,6 +696,7 @@ async def logout(self):
693
696
694
697
await self .send (body , param )
695
698
await self .clear_token ()
699
+ await self ._aiohttp_session .close ()
696
700
697
701
async def set_channel (self , channel ):
698
702
"""Update the channel property."""
@@ -1061,33 +1065,29 @@ async def send(self, body, param=None):
1061
1065
1062
1066
try :
1063
1067
if body is None :
1064
- async with aiohttp .ClientSession (timeout = self ._timeout ,
1065
- connector = aiohttp .TCPConnector (verify_ssl = False )) as session :
1066
- async with session .get (url = self ._url , params = param , allow_redirects = False ) as response :
1067
- _LOGGER .debug ("send()= HTTP Request params =%s" , str (param ).replace (self ._password , "<password>" ))
1068
- json_data = await response .read ()
1069
- _LOGGER .debug ("send HTTP Response status=%s" , str (response .status ))
1070
- if param .get ("cmd" ) == "Snap" :
1071
- _LOGGER_DATA .debug ("send() HTTP Response data scrapped because it's too large" )
1072
- else :
1073
- _LOGGER_DATA .debug ("send() HTTP Response data: %s" , json_data )
1074
-
1075
- return json_data
1068
+ async with self ._aiohttp_session .get (url = self ._url , params = param , allow_redirects = False ) as response :
1069
+ _LOGGER .debug ("send()= HTTP Request params =%s" , str (param ).replace (self ._password , "<password>" ))
1070
+ json_data = await response .read ()
1071
+ _LOGGER .debug ("send HTTP Response status=%s" , str (response .status ))
1072
+ if param .get ("cmd" ) == "Snap" :
1073
+ _LOGGER_DATA .debug ("send() HTTP Response data scrapped because it's too large" )
1074
+ else :
1075
+ _LOGGER_DATA .debug ("send() HTTP Response data: %s" , json_data )
1076
+
1077
+ return json_data
1076
1078
else :
1077
- async with aiohttp .ClientSession (timeout = self ._timeout ,
1078
- connector = aiohttp .TCPConnector (verify_ssl = False )) as session :
1079
- async with session .post (
1079
+ async with self ._aiohttp_session .post (
1080
1080
url = self ._url , json = body , params = param , allow_redirects = False
1081
- ) as response :
1082
- _LOGGER .debug ("send() HTTP Request params =%s" , str (param ).replace (self ._password , "<password>" ))
1083
- _LOGGER .debug ("send() HTTP Request body =%s" , str (body ).replace (self ._password , "<password>" ))
1084
- json_data = await response .text ()
1085
- _LOGGER .debug ("send() HTTP Response status=%s" , str (response .status ))
1086
- if param .get ("cmd" ) == "Search" and len (json_data ) > 500 :
1087
- _LOGGER_DATA .debug ("send() HTTP Response data scrapped because it's too large" )
1088
- else :
1089
- _LOGGER_DATA .debug ("send() HTTP Response data: %s" , json_data )
1090
- return json_data
1081
+ ) as response :
1082
+ _LOGGER .debug ("send() HTTP Request params =%s" , str (param ).replace (self ._password , "<password>" ))
1083
+ _LOGGER .debug ("send() HTTP Request body =%s" , str (body ).replace (self ._password , "<password>" ))
1084
+ json_data = await response .text ()
1085
+ _LOGGER .debug ("send() HTTP Response status=%s" , str (response .status ))
1086
+ if param .get ("cmd" ) == "Search" and len (json_data ) > 500 :
1087
+ _LOGGER_DATA .debug ("send() HTTP Response data scrapped because it's too large" )
1088
+ else :
1089
+ _LOGGER_DATA .debug ("send() HTTP Response data: %s" , json_data )
1090
+ return json_data
1091
1091
1092
1092
except aiohttp .ClientConnectorError as conn_err :
1093
1093
_LOGGER .debug ("Host %s: Connection error %s" , self ._host , str (conn_err ))
0 commit comments