Fixes for storing timestamp

This commit is contained in:
ChrisTerBeke 2019-02-08 22:24:14 +01:00
parent 05c4b6012e
commit 48c756b01d
No known key found for this signature in database
GPG key ID: A49F1AB9D7E0C263
3 changed files with 16 additions and 13 deletions

View file

@ -2,7 +2,7 @@
# Cura is released under the terms of the LGPLv3 or higher.
import json
import webbrowser
from datetime import timedelta, datetime
from datetime import datetime, timedelta
from typing import Optional, TYPE_CHECKING
from urllib.parse import urlencode
import requests.exceptions
@ -12,7 +12,7 @@ from UM.Logger import Logger
from UM.Signal import Signal
from cura.OAuth2.LocalAuthorizationServer import LocalAuthorizationServer
from cura.OAuth2.AuthorizationHelpers import AuthorizationHelpers
from cura.OAuth2.AuthorizationHelpers import AuthorizationHelpers, TOKEN_TIMESTAMP_FORMAT
from cura.OAuth2.Models import AuthenticationResponse
if TYPE_CHECKING:
@ -89,17 +89,18 @@ class AuthorizationService:
# Get the access token as provided by the response data.
def getAccessToken(self) -> Optional[str]:
# Check if the current access token is expired and refresh it if that is the case.
creation_date = self._auth_data.received_at or datetime(2000, 1, 1)
expiry_date = creation_date + timedelta(seconds = float(self._auth_data.expires_in))
if datetime.now() > expiry_date:
self.refreshAccessToken()
if self._auth_data is None:
Logger.log("d", "No auth data to retrieve the access_token from")
return None
return self._auth_data.access_token
# Check if the current access token is expired and refresh it if that is the case.
received_at = datetime.strptime(self._auth_data.received_at, TOKEN_TIMESTAMP_FORMAT) \
if self._auth_data.received_at else datetime(2000, 1, 1)
expiry_date = received_at + timedelta(seconds = float(self._auth_data.expires_in or 0))
if datetime.now() > expiry_date:
self.refreshAccessToken()
return self._auth_data.access_token if self._auth_data else None
# Try to refresh the access token. This should be used when it has expired.
def refreshAccessToken(self) -> None: