From 535f44bcb9978b00401878a9fd4a109f71ca7536 Mon Sep 17 00:00:00 2001 From: klinga Date: Thu, 8 Feb 2024 09:06:55 -0500 Subject: [PATCH] token_expires_at as datetime obj --- bookops_worldcat/authorize.py | 22 +++++++++------------- tests/test_authorize.py | 15 +++++++-------- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/bookops_worldcat/authorize.py b/bookops_worldcat/authorize.py index 490cb0a..cf89ab2 100644 --- a/bookops_worldcat/authorize.py +++ b/bookops_worldcat/authorize.py @@ -94,7 +94,7 @@ def __init__( self.secret = secret self.server_response: Optional[requests.Response] = None self.timeout = timeout - self.token_expires_at = "" + self.token_expires_at: Optional[datetime.datetime] = None self.token_str = "" self.token_type = "" @@ -146,7 +146,7 @@ def __init__( def _auth(self) -> Tuple[str, str]: return (self.key, self.secret) - def _hasten_expiration_time(self, utc_stamp_str: str) -> str: + def _hasten_expiration_time(self, utc_stamp_str: str) -> datetime.datetime: """ Resets expiration time one second earlier to account for any delays between expiration check and request for @@ -161,7 +161,7 @@ def _hasten_expiration_time(self, utc_stamp_str: str) -> str: utcstamp = datetime.datetime.strptime( utc_stamp_str, "%Y-%m-%d %H:%M:%SZ" ) - datetime.timedelta(seconds=1) - return datetime.datetime.strftime(utcstamp, "%Y-%m-%d %H:%M:%SZ") + return utcstamp def _parse_server_response(self, response: requests.Response) -> None: """Parses authorization server response""" @@ -235,20 +235,16 @@ def is_expired(self) -> bool: >>> token.is_expired() False """ - try: - if ( - datetime.datetime.strptime(self.token_expires_at, "%Y-%m-%d %H:%M:%SZ") - < datetime.datetime.utcnow() - ): + if isinstance(self.token_expires_at, datetime.datetime): + if self.token_expires_at < datetime.datetime.utcnow(): return True else: return False - except TypeError: - raise - except ValueError: - raise + else: + raise TypeError def __repr__(self): return ( - f"access_token: '{self.token_str}', expires_at: '{self.token_expires_at}'" + f"access_token: '{self.token_str}', " + f"expires_at: '{self.token_expires_at:%Y-%m-%d %H:%M:%SZ}'" ) diff --git a/tests/test_authorize.py b/tests/test_authorize.py index 66faf21..1b6826a 100644 --- a/tests/test_authorize.py +++ b/tests/test_authorize.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- import datetime -from contextlib import nullcontext as does_not_raise import os import pytest @@ -264,7 +263,9 @@ def test_auth(self, mock_successful_post_token_response): def test_hasten_expiration_time(self, mock_token): utc_stamp = "2020-01-01 17:19:59Z" token = mock_token - assert token._hasten_expiration_time(utc_stamp) == "2020-01-01 17:19:58Z" + timestamp = token._hasten_expiration_time(utc_stamp) + assert isinstance(timestamp, datetime.datetime) + assert timestamp == datetime.datetime(2020, 1, 1, 17, 19, 58, 0) def test_payload(self, mock_successful_post_token_response): token = WorldcatAccessToken( @@ -347,16 +348,15 @@ def test_is_expired_false( def test_is_expired_true(self, mock_utcnow, mock_token): mock_token.is_expired() is False - mock_token.token_expires_at = datetime.datetime.strftime( - datetime.datetime.utcnow() - datetime.timedelta(0, 1), - "%Y-%m-%d %H:%M:%SZ", + mock_token.token_expires_at = datetime.datetime.utcnow() - datetime.timedelta( + 0, 1 ) assert mock_token.is_expired() is True @pytest.mark.parametrize( "arg,expectation", - [(None, pytest.raises(TypeError)), ("20-01-01", pytest.raises(ValueError))], + [(None, pytest.raises(TypeError))], ) def test_is_expired_exception(self, arg, expectation, mock_token): mock_token.token_expires_at = arg @@ -439,5 +439,4 @@ def test_post_token_request_with_live_service(self, live_keys): # test if token looks right assert token.token_str.startswith("tk_") assert token.is_expired() is False - with does_not_raise(): - datetime.datetime.strptime(token.token_expires_at, "%Y-%m-%d %H:%M:%SZ") + assert isinstance(token.token_expires_at, datetime.datetime)