Skip to content

Commit

Permalink
Added Python2 handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven authored and EvanBldy committed Dec 12, 2024
1 parent e126336 commit af4392e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
4 changes: 4 additions & 0 deletions gazu/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ def refresh_token(self):
@property
def access_token_has_expired(self):
""" Returns: Whether this client's access token needs to be refreshed. """
# Python 2 is too deprecated to support this feature. (Lack of datetime.timestamp())
if sys.version_info.major == 2:
return False

if not self.access_token:
# No access token is present, refresh only when able with a refresh token.
return True if self.refresh_token else False
Expand Down
19 changes: 16 additions & 3 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import datetime
import json
import random
Expand Down Expand Up @@ -256,15 +257,23 @@ def test_version(self):
self.assertEqual(raw.get_api_version(), "0.2.0")

def test_make_auth_token(self):
tokens = {"access_token": jwt.encode(
payload={'exp': (datetime.datetime.now() + datetime.timedelta(days=30)).timestamp()},
key='secretkey')}
if sys.version_info.major == 2:
tokens = {"access_token": "secretaccesstoken"}
else:
tokens = {"access_token": jwt.encode(
payload={'exp': (datetime.datetime.now() + datetime.timedelta(days=30)).timestamp()},
key='secretkey')}

raw.set_tokens(tokens)
self.assertEqual(raw.make_auth_header(),
{"Authorization": "Bearer " + tokens["access_token"],
"User-Agent": "CGWire Gazu " + __version__})

def test_access_token_has_expired(self):
# Automatic token refresh is not supported in Python 2.
if sys.version_info.major == 2:
return True

client = raw.KitsuClient(host='http://localhost')
test_cases = {'fresh': (datetime.timedelta(days=30), False),
'expired': (datetime.timedelta(days=-1), True)}
Expand All @@ -285,6 +294,10 @@ def test_access_token_has_expired(self):
self.assertEqual(first=client.access_token_has_expired, second=True)

def test_automatic_token_refresh(self):
# Automatic token refresh is not supported in Python 2.
if sys.version_info.major == 2:
return True

def encode(timestamp):
return jwt.encode(payload={'exp': timestamp}, key='secretkey')

Expand Down

0 comments on commit af4392e

Please sign in to comment.