Skip to content

Commit

Permalink
Merge pull request #8 from zhulik/better-error-wrapping
Browse files Browse the repository at this point in the history
Better error wrapping
  • Loading branch information
zhulik authored May 20, 2021
2 parents 3fad75e + 55ae2e9 commit 86e0054
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
12 changes: 8 additions & 4 deletions aiotractive/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import asyncio
import json
import time

import aiohttp
from aiohttp.client_exceptions import ClientResponseError
Expand Down Expand Up @@ -42,11 +43,11 @@ def __init__( # pylint: disable=too-many-arguments
self._auth_headers = None

async def user_id(self):
await self._authenticate()
await self.authenticate()
return self._user_credentials["user_id"]

async def auth_headers(self):
await self._authenticate()
await self.authenticate()
return {**self.BASE_HEADERS, **self._auth_headers}

async def request(self, *args, **kwargs):
Expand Down Expand Up @@ -76,9 +77,12 @@ async def raw_request(self, uri, params=None, data=None, method="GET"):
return await response.json()
return await response.read()

async def _authenticate(self):
async def authenticate(self):
"""Perform authenticateion."""
# TODO: update credentials if expired
if self._user_credentials is not None and self._user_credentials["expires_at"] - time.time() < 3600:
self._user_credentials = None
self._auth_headers = None

if self._user_credentials is not None:
return self._user_credentials

Expand Down
4 changes: 2 additions & 2 deletions aiotractive/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import time
from asyncio.exceptions import TimeoutError as AIOTimeoutError

from .exceptions import DisconnectedError
from .exceptions import DisconnectedError, TractiveError


class Channel:
Expand Down Expand Up @@ -34,7 +34,7 @@ async def listen(self):
self._check_connection_task.cancel()

await self._check_connection_task
raise event["error"]
raise TractiveError() from event["error"]

if event["type"] == "cancelled":
self._listen_task.cancel()
Expand Down
3 changes: 3 additions & 0 deletions aiotractive/tractive.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ def __init__(self, *args, **kwargs):
"""Initialize the client."""
self._api = API(*args, **kwargs)

async def authenticate(self):
return await self._api.authenticate()

async def trackers(self):
trackers = await self._api.request(f"user/{await self._api.user_id()}/trackers")
return [Tracker(self._api, t) for t in trackers]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name="aiotractive",
version="0.3.0",
version="0.4.0",
author="Gleb Sinyavskiy",
author_email="zhulik.gleb@gmail.com",
description="Asynchronous Python client for the Tractive REST API",
Expand Down

0 comments on commit 86e0054

Please sign in to comment.