Skip to content

Commit 44a0774

Browse files
committed
refresh the NewMode token when needed
1 parent 74e9cbd commit 44a0774

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

parsons/newmode/newmode.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
from typing import Any, Dict, List, Optional, Union
3+
from oauthlib.oauth2 import TokenExpiredError
34

45
from Newmode import Client
56

@@ -429,7 +430,10 @@ def __init__(
429430
self.client_id: str = check_env.check("NEWMODE_API_CLIENT_ID", client_id)
430431
self.client_secret: str = check_env.check("NEWMODE_API_CLIENT_SECRET", client_secret)
431432
self.headers: Dict[str, str] = {"content-type": "application/json"}
432-
self.default_client: OAuth2APIConnector = OAuth2APIConnector(
433+
self.default_client: OAuth2APIConnector = self.get_default_oauth_client()
434+
435+
def get_default_oauth_client(self) -> OAuth2APIConnector:
436+
return OAuth2APIConnector(
433437
uri=self.base_url,
434438
auto_refresh_url=V2_API_AUTH_URL,
435439
client_id=self.client_id,
@@ -472,10 +476,14 @@ def base_request(
472476

473477
for attempt in range(retries + 1):
474478
try:
475-
response = client.request(
476-
url=url, req_type=method, json=json, data=data, params=params
477-
)
478-
return self.checked_response(response, client)
479+
try:
480+
response = client.request(
481+
url=url, req_type=method, json=json, data=data, params=params
482+
)
483+
return self.checked_response(response, client)
484+
except TokenExpiredError as e:
485+
logger.warning(f"Token expired: {e}. Refreshing it...")
486+
self.default_client = self.get_default_oauth_client()
479487
except Exception as e:
480488
if attempt < retries:
481489
logger.warning(f"Request failed (attempt {attempt + 1}/{retries}). Retrying...")

0 commit comments

Comments
 (0)