Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.

Commit d7c3888

Browse files
authored
Merge pull request #49 from cpainchaud/main
AIOHTTTP session reuse for better performances
2 parents 0ea20bb + e1dc3e8 commit d7c3888

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

reolink/camera_api.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ def __init__(
107107

108108
self._is_nvr = False
109109

110+
self._aiohttp_session: aiohttp.ClientSession = aiohttp.ClientSession(timeout=self._timeout,
111+
connector=aiohttp.TCPConnector(verify_ssl=False))
112+
110113
self.refresh_base_url()
111114

112115
def enable_https(self, enable: bool):
@@ -693,6 +696,7 @@ async def logout(self):
693696

694697
await self.send(body, param)
695698
await self.clear_token()
699+
await self._aiohttp_session.close()
696700

697701
async def set_channel(self, channel):
698702
"""Update the channel property."""
@@ -1061,33 +1065,29 @@ async def send(self, body, param=None):
10611065

10621066
try:
10631067
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
10761078
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(
10801080
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
10911091

10921092
except aiohttp.ClientConnectorError as conn_err:
10931093
_LOGGER.debug("Host %s: Connection error %s", self._host, str(conn_err))

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
setup(
44
name='reolink',
55
packages=['reolink'],
6-
version='0.0.27',
6+
version='0.0.28',
77
license='MIT',
88
description='Reolink camera package',
99
author='fwestenberg',

0 commit comments

Comments
 (0)