From ac4ed240d739c00434552c57e5ddb07e08ec55ae Mon Sep 17 00:00:00 2001 From: Xander Riga Date: Mon, 15 Feb 2021 12:00:24 -0800 Subject: [PATCH 1/3] Improve variable names and error reporting We previously had the help message inside the error we are raising, but we really want to be logging that as that is where users will be looking for help. Inside the error we can just dump the entire response. This might be absurd and too much content, but it at least allows them to make decisions based on this if they want. --- pyracing/client.py | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/pyracing/client.py b/pyracing/client.py index 10e5673..6f0ab24 100644 --- a/pyracing/client.py +++ b/pyracing/client.py @@ -53,16 +53,15 @@ async def _authenticate(self): 'todaysdate': '' # Unknown purpose, but exists as a hidden form. } - auth_post = await self.session.post(ct.URL_LOGIN2, data=login_data) + auth_response = await self.session.post(ct.URL_LOGIN2, data=login_data) - if 'failedlogin' in str(auth_post.url): - logger.warning('Login Failed. Please check credentials') - raise UserWarning( + if 'failedlogin' in str(auth_response.url): + logger.warning( 'The login POST request was redirected to /failedlogin, ' 'indicating an authentication failure. If credentials are ' 'correct, check that a captcha is not required by manually ' - 'visiting members.iracing.com' - ) + 'visiting members.iracing.com') + raise UserWarning(auth_response.read()) else: logger.info('Login successful') @@ -96,10 +95,10 @@ async def _build_request(self, url, params): return response async def active_op_counts( - self, - count_max=250, - include_empty='n', - cust_id=None + self, + count_max=250, + include_empty='n', + cust_id=None ): """ Returns session information for all 'open practice' sessions that are currently active. By default this only includes sessions with @@ -632,10 +631,10 @@ async def race_guide( x in response.json()['series']] async def race_laps_all( - self, - subsession_id, - car_class_id=None, - sim_session_type=ct.SimSessionType.race.value + self, + subsession_id, + car_class_id=None, + sim_session_type=ct.SimSessionType.race.value ): """ Returns information about all laps of a race for *every* driver. The class of car can be set for multiclass races. @@ -646,7 +645,7 @@ async def race_laps_all( 'subsessionid': subsession_id, 'carclassid': car_class_id, 'simsesnum': sim_session_type - } + } url = ct.URL_LAPS_ALL response = await self._build_request(url, payload) @@ -779,14 +778,14 @@ async def total_registered_all(self): return [upcoming_events.TotalRegistered(x) for x in response.json()] async def world_records( - self, - year, - quarter, - car_id, - track_id, - result_num_low=1, - result_num_high=25, - cust_id=None + self, + year, + quarter, + car_id, + track_id, + result_num_low=1, + result_num_high=25, + cust_id=None ): """ Returns laptimes with the requested paramaters. Filters can also be seen on the /worldrecords.jsp page on the membersite. From 1674fa98e9ee9758333d39d76bd713430d32ff71 Mon Sep 17 00:00:00 2001 From: Xander Riga Date: Mon, 15 Feb 2021 11:46:50 -0800 Subject: [PATCH 2/3] Create AuthenticationError We currently raise a `UserWarning` when we fail auth which isn't very descriptive and doesn't tell us what actually failed. --- pyracing/exceptions/__init__.py | 0 pyracing/exceptions/authentication_error.py | 5 +++++ 2 files changed, 5 insertions(+) create mode 100644 pyracing/exceptions/__init__.py create mode 100644 pyracing/exceptions/authentication_error.py diff --git a/pyracing/exceptions/__init__.py b/pyracing/exceptions/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pyracing/exceptions/authentication_error.py b/pyracing/exceptions/authentication_error.py new file mode 100644 index 0000000..e694aa0 --- /dev/null +++ b/pyracing/exceptions/authentication_error.py @@ -0,0 +1,5 @@ +class AuthenticationError(Exception): + """Raised when our attempt to authenticate fails""" + def __init__(self, message, response): + self.response = response + super(AuthenticationError, self).__init__(message) From 596cfb6172ff66fc62e0071a33ae9bbe6c26da8b Mon Sep 17 00:00:00 2001 From: Xander Riga Date: Mon, 15 Feb 2021 12:13:16 -0800 Subject: [PATCH 3/3] Replace UserWarning with AuthenticationError This is a more accurate error for what is actually happening and lets us save the actual response in case the user wants to do something with it. --- pyracing/client.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pyracing/client.py b/pyracing/client.py index 6f0ab24..ea090b3 100644 --- a/pyracing/client.py +++ b/pyracing/client.py @@ -10,6 +10,7 @@ session_data, upcoming_events, ) +from .exceptions.authentication_error import AuthenticationError from datetime import datetime import httpx @@ -61,7 +62,7 @@ async def _authenticate(self): 'indicating an authentication failure. If credentials are ' 'correct, check that a captcha is not required by manually ' 'visiting members.iracing.com') - raise UserWarning(auth_response.read()) + raise AuthenticationError('Login Failed', auth_response) else: logger.info('Login successful')