Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions GroheClient/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from datetime import datetime, timedelta
import time

from GroheClient.tokens import get_refresh_tokens, get_tokens_from_credentials
from settings import get_setting as _
Expand Down Expand Up @@ -31,10 +32,19 @@ def get_initial_tokens() -> dict:
def refresh_tokens():
logging.info("Refreshing tokens")
global access_token, refresh_token, access_token_expiring_date
tokens = get_refresh_tokens(refresh_token)
access_token = tokens['access_token']
refresh_token = tokens['refresh_token']
access_token_expiring_date = datetime.now() + timedelta(seconds=tokens['access_token_expires_in'] - 60)
attempts = 0
while attempts < 3:
try:
tokens = get_refresh_tokens(refresh_token)
access_token = tokens['access_token']
refresh_token = tokens['refresh_token']
access_token_expiring_date = datetime.now() + timedelta(seconds=tokens['access_token_expires_in'] - 60)
return
except Exception as e:
attempts += 1
logging.error(f"Attempt {attempts} failed: {e}")
time.sleep(5)
logging.error("Failed to refresh tokens after 3 attempts")


def get_access_token() -> str:
Expand Down