mirror of
https://github.com/bckelley/tconnectsync.git
synced 2026-08-24 03:34:12 -05:00
api: use separate exception class for login errors
This commit is contained in:
+5
-3
@@ -8,7 +8,7 @@ import arrow
|
||||
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
from .common import ApiException
|
||||
from .common import ApiException, ApiLoginException
|
||||
|
||||
class AndroidApi:
|
||||
BASE_URL = 'https://tdcservices.tandemdiabetes.com/'
|
||||
@@ -30,7 +30,6 @@ class AndroidApi:
|
||||
def __init__(self, email, password):
|
||||
self.login(email, password)
|
||||
|
||||
# TODO: auto refresh token
|
||||
def login(self, email, password):
|
||||
r = requests.post(
|
||||
self.BASE_URL + self.OAUTH_TOKEN_PATH,
|
||||
@@ -45,9 +44,12 @@ class AndroidApi:
|
||||
)
|
||||
|
||||
if r.status_code != 200:
|
||||
raise ApiException(r.status_code, 'Received HTTP %s during login: %s' % (r.status_code, r.text))
|
||||
raise ApiLoginException(r.status_code, 'Received HTTP %s during login: %s' % (r.status_code, r.text))
|
||||
|
||||
j = r.json()
|
||||
if "user" not in j or not j["user"]:
|
||||
raise ApiException(r.status_code, 'No user details present in AndroidApi oauth response')
|
||||
|
||||
self.accessToken = j["accessToken"]
|
||||
self.accessTokenExpiresAt = j["accessTokenExpiresAt"]
|
||||
self.refreshToken = j["refreshToken"]
|
||||
|
||||
@@ -12,3 +12,6 @@ class ApiException(Exception):
|
||||
def __init__(self, status_code, text, *args, **kwargs):
|
||||
self.status_code = status_code
|
||||
super().__init__(text, *args, **kwargs)
|
||||
|
||||
class ApiLoginException(ApiException):
|
||||
pass
|
||||
+5
-5
@@ -3,7 +3,7 @@ import urllib
|
||||
import datetime
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
from .common import parse_date, base_headers, ApiException
|
||||
from .common import parse_date, base_headers, ApiException, ApiLoginException
|
||||
|
||||
class ControlIQApi:
|
||||
BASE_URL = 'https://tdcservices.tandemdiabetes.com/tconnect/controliq/api/'
|
||||
@@ -14,8 +14,7 @@ class ControlIQApi:
|
||||
accessTokenExpiresAt = None
|
||||
|
||||
def __init__(self, email, password):
|
||||
if not self.login(email, password):
|
||||
raise Exception('Unable to authenticate')
|
||||
self.login(email, password)
|
||||
|
||||
def login(self, email, password):
|
||||
with requests.Session() as s:
|
||||
@@ -35,11 +34,12 @@ class ControlIQApi:
|
||||
}
|
||||
req = s.post(self.LOGIN_URL, data=data, headers={'Referer': self.LOGIN_URL, **base_headers()}, allow_redirects=False)
|
||||
if req.status_code != 302:
|
||||
return False
|
||||
raise ApiLoginException(req.status_code, 'Error logging in to t:connect. Check your login credentials.')
|
||||
|
||||
|
||||
fwd = s.post(urllib.parse.urljoin(self.LOGIN_URL, req.headers['Location']), cookies=req.cookies, headers=base_headers())
|
||||
if fwd.status_code != 200:
|
||||
return False
|
||||
raise ApiException(fwd.status_code, 'Error retrieving t:connect login cookies.')
|
||||
|
||||
self.userGuid = req.cookies['UserGUID']
|
||||
self.accessToken = req.cookies['accessToken']
|
||||
|
||||
Reference in New Issue
Block a user