Skip to content

Commit

Permalink
Add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mbthornton-lbl committed Dec 17, 2024
1 parent 736fe82 commit 744a1df
Show file tree
Hide file tree
Showing 4 changed files with 506 additions and 426 deletions.
12 changes: 11 additions & 1 deletion nmdc_automation/api/nmdcapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from datetime import datetime, timedelta, timezone
from nmdc_automation.config import SiteConfig, UserConfig
import logging
from tenacity import retry, wait_exponential, stop_after_attempt

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -73,9 +74,15 @@ def _get_token(self, *args, **kwargs):

return _get_token

@retry(
wait=wait_exponential(multiplier=1, min=30, max=240),
stop=stop_after_attempt(4),
reraise=True,
)
def get_token(self):
"""
Get a token using a client id/secret.
Retries up to 4 times with exponential backoff.
"""
h = {
"accept": "application/json",
Expand All @@ -89,7 +96,9 @@ def get_token(self):
url = self._base_url + "token"

resp = requests.post(url, headers=h, data=data)

if not resp.ok:
logging.error(f"Failed to get token: {resp.text}")
resp.raise_for_status()
response_body = resp.json()

# Expires can be in days, hours, minutes, seconds - sum them up and convert to seconds
Expand All @@ -111,6 +120,7 @@ def get_token(self):
"Content-Type": "application/json",
"Authorization": "Bearer %s" % (self.token),
}
logging.info(f"New token expires at {self.expires_at}")
return response_body

def get_header(self):
Expand Down
Loading

0 comments on commit 744a1df

Please sign in to comment.