diff --git a/pyracing/client.py b/pyracing/client.py index 10e5673..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 @@ -53,16 +54,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 AuthenticationError('Login Failed', auth_response) else: logger.info('Login successful') @@ -96,10 +96,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 +632,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 +646,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 +779,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. 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)