Spaces:
Runtime error
Runtime error
File size: 803 Bytes
1315e90 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import time
import requests
from app.core.config import WATSONX_API_KEY
_iam_cache = {"token": None, "expiry": 0.0}
def get_ibm_iam_token() -> str:
now = time.time()
token = _iam_cache.get("token")
expiry = _iam_cache.get("expiry", 0.0)
if token and now < (expiry - 60):
return token
response = requests.post(
"https://iam.cloud.ibm.com/identity/token",
data={
"grant_type": "urn:ibm:params:oauth:grant-type:apikey",
"apikey": WATSONX_API_KEY,
},
headers={"Content-Type": "application/x-www-form-urlencoded"},
timeout=30,
)
response.raise_for_status()
token = response.json()["access_token"]
_iam_cache["token"] = token
_iam_cache["expiry"] = now + 3000 # ~50 minutes
return token |