Skip to content
Snippets Groups Projects
Commit 1c7f0fbb authored by Jorrit Schaap's avatar Jorrit Schaap
Browse files

minor patches

parent 90188a24
No related branches found
No related tags found
No related merge requests found
...@@ -58,6 +58,7 @@ class TMSSWindmillStandstillEventMessageHandler(TMSSEventMessageHandler): ...@@ -58,6 +58,7 @@ class TMSSWindmillStandstillEventMessageHandler(TMSSEventMessageHandler):
self.client_id = client_id self.client_id = client_id
self.client_secret = client_secret self.client_secret = client_secret
self.access_token_expiration_date = datetime.min self.access_token_expiration_date = datetime.min
self.access_token = None
def start_handling(self): def start_handling(self):
self.refresh_access_token() self.refresh_access_token()
...@@ -80,7 +81,7 @@ class TMSSWindmillStandstillEventMessageHandler(TMSSEventMessageHandler): ...@@ -80,7 +81,7 @@ class TMSSWindmillStandstillEventMessageHandler(TMSSEventMessageHandler):
"client_id": self.client_id, "client_id": self.client_id,
"client_secret": self.client_secret}) "client_secret": self.client_secret})
except Exception as e: except Exception as e:
logger.error("could not refresh access token: %s", str(e)) logger.exception("could not refresh access token: %s", str(e))
try: try:
json_response = response.json() json_response = response.json()
...@@ -88,10 +89,11 @@ class TMSSWindmillStandstillEventMessageHandler(TMSSEventMessageHandler): ...@@ -88,10 +89,11 @@ class TMSSWindmillStandstillEventMessageHandler(TMSSEventMessageHandler):
self.access_token_expiration_date = datetime.utcnow() + timedelta(seconds=json_response['expires_in']-10) self.access_token_expiration_date = datetime.utcnow() + timedelta(seconds=json_response['expires_in']-10)
logger.info('new token valid till %s' % self.access_token_expiration_date.isoformat()) logger.info('new token valid till %s' % self.access_token_expiration_date.isoformat())
except Exception as e: except Exception as e:
logger.error("could not refresh access token: http_status_code=%s error: %s", response.status_code, str(e)) logger.exception("could not refresh access token: http_status_code=%s error: %s", response.status_code, str(e))
def get_api_path_as_json(self, path): def get_api_path_as_json(self, path):
if datetime.utcnow() >= self.access_token_expiration_date: logger.info('fetching %s...', path)
if datetime.utcnow() >= self.access_token_expiration_date or self.access_token is None:
self.refresh_access_token() self.refresh_access_token()
headers = {"Authorization": "Bearer " + self.access_token} headers = {"Authorization": "Bearer " + self.access_token}
response = requests.get(self.windmill_api_url + path, headers=headers) response = requests.get(self.windmill_api_url + path, headers=headers)
...@@ -126,7 +128,6 @@ class TMSSWindmillStandstillEventMessageHandler(TMSSEventMessageHandler): ...@@ -126,7 +128,6 @@ class TMSSWindmillStandstillEventMessageHandler(TMSSEventMessageHandler):
path += '?' if path.endswith('/') else '&' path += '?' if path.endswith('/') else '&'
path += 'till' + scheduled_start_time_before.isoformat() path += 'till' + scheduled_start_time_before.isoformat()
logger.info('fetching %s...')
response = self.get_api_path_as_json(path) response = self.get_api_path_as_json(path)
return response['blocks'] return response['blocks']
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment