Skip to content

Commit

Permalink
Merge pull request #14 from andrey-git/allow_retry
Browse files Browse the repository at this point in the history
Allow retry on connection failure
  • Loading branch information
gjohansson-ST authored Apr 30, 2022
2 parents 4f426a5 + 1a0e312 commit f45ac31
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
69 changes: 58 additions & 11 deletions pysensibo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Python API for Sensibo."""
from __future__ import annotations
import asyncio

import json
from typing import Any
Expand Down Expand Up @@ -453,44 +454,90 @@ async def async_set_ac_state_property(
APIV2 + "/pods/{}/acStates/{}".format(uid, name), params, data
)

async def _get(self, path: str, params: dict[str, Any]) -> dict[str, Any]:
async def _get(
self, path: str, params: dict[str, Any], retry: bool = False
) -> dict[str, Any]:
"""Make GET api call to Sensibo api."""
async with self._session.get(path, params=params, timeout=self.timeout) as resp:
return await self._response(resp)
try:
return await self._response(resp)
except Exception as error:
if retry is False:
asyncio.sleep(5)
return self._get(path, params, True)
raise error

async def _put(
self, path: str, params: dict[str, Any], data: dict[str, Any]
self,
path: str,
params: dict[str, Any],
data: dict[str, Any],
retry: bool = False,
) -> dict[str, Any]:
"""Make PUT api call to Sensibo api."""
async with self._session.put(
path, params=params, data=json.dumps(data), timeout=self.timeout
) as resp:
return await self._response(resp)
try:
return await self._response(resp)
except Exception as error:
if retry is False:
asyncio.sleep(5)
return self._put(path, params, data, True)
raise error

async def _post(
self, path: str, params: dict[str, Any], data: dict[str, Any]
self,
path: str,
params: dict[str, Any],
data: dict[str, Any],
retry: bool = False,
) -> dict[str, Any]:
"""Make POST api call to Sensibo api."""
async with self._session.post(
path, params=params, data=json.dumps(data), timeout=self.timeout
) as resp:
return await self._response(resp)
try:
return await self._response(resp)
except Exception as error:
if retry is False:
asyncio.sleep(5)
return self._post(path, params, data, True)
raise error

async def _patch(
self, path: str, params: dict[str, Any], data: dict[str, Any]
self,
path: str,
params: dict[str, Any],
data: dict[str, Any],
retry: bool = False,
) -> dict[str, Any]:
"""Make PATCH api call to Sensibo api."""
async with self._session.patch(
path, params=params, data=json.dumps(data), timeout=self.timeout
) as resp:
return await self._response(resp)

async def _delete(self, path: str, params: dict[str, Any]) -> dict[str, Any]:
try:
return await self._response(resp)
except Exception as error:
if retry is False:
asyncio.sleep(5)
return self._patch(path, params, data, True)
raise error

async def _delete(
self, path: str, params: dict[str, Any], retry: bool = False
) -> dict[str, Any]:
"""Make DELETE api call to Sensibo api."""
async with self._session.delete(
path, params=params, timeout=self.timeout
) as resp:
return await self._response(resp)
try:
return await self._response(resp)
except Exception as error:
if retry is False:
asyncio.sleep(5)
return self._delete(path, params, True)
raise error

async def _response(self, resp: ClientResponse) -> dict[str, Any]:
"""Return response from call."""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
print(find_packages())
setup(
name="pysensibo",
version="1.0.12",
version="1.0.13",
description="asyncio-friendly python API for Sensibo",
long_description="asyncio-friendly python API for Sensibo"
"(https://sensibo.com). Requires Python 3.4+",
Expand Down

0 comments on commit f45ac31

Please sign in to comment.