Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion nokia/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@
from requests_oauthlib import OAuth2Session
from oauthlib.oauth2 import WebApplicationClient


class Error(Exception):
def __init__(self, msg):
super(Error, self).__init__(msg)


class ApiError(Error):
"""See: https://developer.withings.com/oauth2/#tag/status"""
def __init__(self, msg, status_code):
super(Error, self).__init__(msg)
self.status_code = status_code


class NokiaCredentials(object):
def __init__(self, access_token=None, token_expiry=None, token_type=None,
refresh_token=None, user_id=None,
Expand Down Expand Up @@ -203,7 +216,9 @@ def request(self, service, action, params=None, method='GET',
r = self.client.request(method, '/'.join(url_parts), params=params)
response = json.loads(r.content.decode())
if response['status'] != 0:
raise Exception("Error code %s" % response['status'])
raise ApiError('Code %s, Error "%s"' % (
response.get('status'), response.get('error')),
int(response['status']))
return response.get('body', None)

def get_user(self):
Expand Down