Skip to content

Commit

Permalink
v0.6.0 (#7)
Browse files Browse the repository at this point in the history
* Remove timeout code

Remove timeout code from pykuna and leave to be implemented by client code.

* Replace camera dict after sucessful request

* Bump version to 0.6.0
  • Loading branch information
marthoc authored Aug 27, 2019
1 parent b2407de commit f5bf02f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ python example.py USERNAME PASSWORD

pykuna interacts with Kuna's (private) mobile device API, which could change at any time. And, without official documentation or terms of service, there's no way to know what type or rate of usage may result in your account being banned by Kuna. Use carefully!

pykuna does not implement timeouts; use asyncio_timeout in your client code to wrap calls to the API as needed.

pykuna was inspired by the investigative work of @loghound: https://github.com/loghound/kuna-camera-api, but does not yet implement all known endpoints; this project is primarily intended to interface Home Assistant with the Kuna API, and will be further developed with that goal in mind.

pykuna will hit v1.0.0 when it's ready for Home Assistant. Until then, pykuna's API may change at any time!
Expand Down
47 changes: 23 additions & 24 deletions pykuna/kuna.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,19 @@ async def update(self):
"""Refresh the dict of all cameras in the Kuna account."""
result = await self._request("get", CAMERAS_ENDPOINT)

for item in result["results"]:
self.cameras[item["serial_number"]] = KunaCamera(item, self._request)
if result is not None:
new_cameras = {}

for item in result["results"]:
new_cameras[item["serial_number"]] = KunaCamera(item, self._request)

self.cameras = new_cameras

async def _request(
self, method, path, params=None, json=None, image=False, allow_redirects=True
):
"""Make an async API request."""
from aiohttp import ClientResponseError
from async_timeout import timeout
from asyncio import TimeoutError

url = "{}/{}/".format(API_URL, path)
headers = {"User-Agent": USER_AGENT, "Content-Type": "application/json"}
Expand All @@ -68,29 +71,25 @@ async def _request(
headers["User-Agent"] = USER_AGENT_THUMBNAIL

try:
async with timeout(3):
async with self._websession.request(
method,
url,
params=params,
json=json,
headers=headers,
allow_redirects=allow_redirects,
) as result:

result.raise_for_status()
if image:
return await result.read()
elif not allow_redirects:
return result
else:
return await result.json()
async with self._websession.request(
method,
url,
params=params,
json=json,
headers=headers,
allow_redirects=allow_redirects,
) as result:

result.raise_for_status()
if image:
return await result.read()
elif not allow_redirects:
return result
else:
return await result.json()

except ClientResponseError as err:
if result.status == 403:
raise UnauthorizedError("Kuna token empty or expired")
else:
_LOGGER.error("Kuna API request error: {}".format(err))

except TimeoutError:
_LOGGER.error("Request to Kuna API timed out")
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

setuptools.setup(
name="pykuna",
version="0.5.1",
version="0.6.0",
author="Mark Coombes",
author_email="[email protected]",
description="Python3 library for interacting with the Kuna camera mobile API",
description="Python3 async library for interacting with the Kuna camera mobile API",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/marthoc/pykuna",
Expand Down

0 comments on commit f5bf02f

Please sign in to comment.