Skip to content

Commit cf19c91

Browse files
authored
Merge pull request #5 from yepcode/feature/YEP-2933
YEP-2933 Implement cached access token endpoint for yepcode run
2 parents 376ab4b + 754b311 commit cf19c91

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

yepcode_run/api/yepcode_api.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,6 @@ def __init__(self, config: YepCodeApiConfig = None):
6565
"timeout": 60000,
6666
**config_dict,
6767
}
68-
if not final_config.get("auth_url"):
69-
final_config["auth_url"] = (
70-
f"{final_config['api_host']}/auth/realms/yepcode/protocol/openid-connect/token"
71-
)
7268

7369
if (
7470
not final_config.get("access_token")
@@ -110,6 +106,7 @@ def __init__(self, config: YepCodeApiConfig = None):
110106
self.api_host = final_config.get("api_host")
111107
self.client_id = final_config.get("client_id")
112108
self.client_secret = final_config.get("client_secret")
109+
self.api_token = final_config.get("api_token")
113110
self.auth_url = final_config.get("auth_url")
114111
self.team_id = final_config.get("team_id")
115112
self.access_token = final_config.get("access_token")
@@ -118,6 +115,8 @@ def __init__(self, config: YepCodeApiConfig = None):
118115
self.client_id = self._client_id_from_access_token()
119116
if not self.team_id and self.client_id:
120117
self.team_id = self._team_id_from_client_id()
118+
if not self.auth_url:
119+
self.auth_url = self._get_auth_url()
121120

122121
def get_client_id(self) -> str:
123122
if not self.client_id:
@@ -154,23 +153,27 @@ def _team_id_from_client_id(self) -> str:
154153
def _get_base_url(self) -> str:
155154
return f"{self.api_host}/api/{self.team_id}/rest"
156155

156+
def _get_auth_url(self) -> str:
157+
return f"{self._get_base_url()}/auth/token"
158+
157159
def _get_access_token(self) -> str:
158-
if not self.client_id or not self.client_secret:
160+
if not self.api_token and (not self.client_id or not self.client_secret):
159161
raise ValueError(
160162
"AccessToken has expired. Provide a new one or enable automatic refreshing by providing an apiToken or clientId and clientSecret."
161163
)
162164
try:
163-
auth_str = base64.b64encode(
165+
api_token = (
166+
self.api_token
167+
or f"sk-{base64.b64encode(
164168
f"{self.client_id}:{self.client_secret}".encode()
165-
).decode()
169+
).decode()}"
170+
)
166171

167172
response = requests.post(
168173
self.auth_url,
169174
headers={
170-
"Authorization": f"Basic {auth_str}",
171-
"Content-Type": "application/x-www-form-urlencoded",
175+
"x-api-token": api_token,
172176
},
173-
data="grant_type=client_credentials",
174177
timeout=self.timeout,
175178
)
176179

0 commit comments

Comments
 (0)