-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.py
More file actions
36 lines (28 loc) · 986 Bytes
/
Copy pathauth.py
File metadata and controls
36 lines (28 loc) · 986 Bytes
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
30
31
32
33
34
35
36
import json
import httpx
import time
import os
from dotenv import load_dotenv
load_dotenv()
INSTANCE_URL = os.getenv("WO_INSTANCE_URL")
API_KEY = os.getenv("WO_API_KEY")
_token_cache = {"token": None, "expires_at": 0}
async def get_token() -> str:
now = time.time()
if _token_cache["token"] and now < _token_cache["expires_at"] - 60:
return _token_cache["token"]
async with httpx.AsyncClient() as client:
res = await client.request(
method="POST",
url="https://iam.platform.saas.ibm.com/siusermgr/api/1.0/apikeys/token",
headers={
"accept": "application/json",
"content-type": "application/json",
},
content=json.dumps({"apikey": API_KEY}),
)
res.raise_for_status()
data = res.json()
_token_cache["token"] = data["token"]
_token_cache["expires_at"] = now + data.get("expires_in", 3600)
return _token_cache["token"]