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

Commit

Permalink
Merge pull request #49 from cpainchaud/main
Browse files Browse the repository at this point in the history
AIOHTTTP session reuse for better performances
  • Loading branch information
cpainchaud authored Oct 12, 2021
2 parents 0ea20bb + e1dc3e8 commit d7c3888
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
50 changes: 25 additions & 25 deletions reolink/camera_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ def __init__(

self._is_nvr = False

self._aiohttp_session: aiohttp.ClientSession = aiohttp.ClientSession(timeout=self._timeout,
connector=aiohttp.TCPConnector(verify_ssl=False))

self.refresh_base_url()

def enable_https(self, enable: bool):
Expand Down Expand Up @@ -693,6 +696,7 @@ async def logout(self):

await self.send(body, param)
await self.clear_token()
await self._aiohttp_session.close()

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

try:
if body is None:
async with aiohttp.ClientSession(timeout=self._timeout,
connector=aiohttp.TCPConnector(verify_ssl=False)) as session:
async with session.get(url=self._url, params=param, allow_redirects=False) as response:
_LOGGER.debug("send()= HTTP Request params =%s", str(param).replace(self._password, "<password>"))
json_data = await response.read()
_LOGGER.debug("send HTTP Response status=%s", str(response.status))
if param.get("cmd") == "Snap":
_LOGGER_DATA.debug("send() HTTP Response data scrapped because it's too large")
else:
_LOGGER_DATA.debug("send() HTTP Response data: %s", json_data)

return json_data
async with self._aiohttp_session.get(url=self._url, params=param, allow_redirects=False) as response:
_LOGGER.debug("send()= HTTP Request params =%s", str(param).replace(self._password, "<password>"))
json_data = await response.read()
_LOGGER.debug("send HTTP Response status=%s", str(response.status))
if param.get("cmd") == "Snap":
_LOGGER_DATA.debug("send() HTTP Response data scrapped because it's too large")
else:
_LOGGER_DATA.debug("send() HTTP Response data: %s", json_data)

return json_data
else:
async with aiohttp.ClientSession(timeout=self._timeout,
connector=aiohttp.TCPConnector(verify_ssl=False)) as session:
async with session.post(
async with self._aiohttp_session.post(
url=self._url, json=body, params=param, allow_redirects=False
) as response:
_LOGGER.debug("send() HTTP Request params =%s", str(param).replace(self._password, "<password>"))
_LOGGER.debug("send() HTTP Request body =%s", str(body).replace(self._password, "<password>"))
json_data = await response.text()
_LOGGER.debug("send() HTTP Response status=%s", str(response.status))
if param.get("cmd") == "Search" and len(json_data) > 500:
_LOGGER_DATA.debug("send() HTTP Response data scrapped because it's too large")
else:
_LOGGER_DATA.debug("send() HTTP Response data: %s", json_data)
return json_data
) as response:
_LOGGER.debug("send() HTTP Request params =%s", str(param).replace(self._password, "<password>"))
_LOGGER.debug("send() HTTP Request body =%s", str(body).replace(self._password, "<password>"))
json_data = await response.text()
_LOGGER.debug("send() HTTP Response status=%s", str(response.status))
if param.get("cmd") == "Search" and len(json_data) > 500:
_LOGGER_DATA.debug("send() HTTP Response data scrapped because it's too large")
else:
_LOGGER_DATA.debug("send() HTTP Response data: %s", json_data)
return json_data

except aiohttp.ClientConnectorError as conn_err:
_LOGGER.debug("Host %s: Connection error %s", self._host, str(conn_err))
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup(
name='reolink',
packages=['reolink'],
version='0.0.27',
version='0.0.28',
license='MIT',
description='Reolink camera package',
author='fwestenberg',
Expand Down

0 comments on commit d7c3888

Please sign in to comment.