Skip to content

Commit 1b50b8b

Browse files
author
liuhuiqi.7
committed
feat(ark_api_key): fix warning
Change-Id: Ie848bc6407c5bf0631ce819be471935554ef35ec feat(ark_api_key): fix warning Change-Id: I91c45383f60532a28a47d22e91c3937f93b87b85 feat(ark_api_key): fix warning Change-Id: I4f62f67b2f30ac4fd2b8ff95e30e0e70ec93bfb1
1 parent fcca188 commit 1b50b8b

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

volcenginesdkarkruntime/_client.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,9 @@ def _get_endpoint_certificate(self, endpoint_id: str) -> key_agreement_client:
109109
if self._certificate_manager is None:
110110
cert_path = os.environ.get("E2E_CERTIFICATE_PATH")
111111
if (self.ak is None or self.sk is None) and cert_path is None and self.api_key is None:
112-
raise ArkAPIError("must set (ak and sk) or (E2E_CERTIFICATE_PATH) \
113-
or (api_key) before get endpoint token.")
114-
self._certificate_manager = E2ECertificateManager(self.ak, self.sk, self.region,
115-
self._base_url, self.api_key)
112+
raise ArkAPIError("must set (api_key) or (ak and sk) \
113+
or (E2E_CERTIFICATE_PATH) before get endpoint token.")
114+
self._certificate_manager = E2ECertificateManager(self.ak, self.sk, self.region, self._base_url, self.api_key)
116115
return self._certificate_manager.get(endpoint_id)
117116

118117
def _get_bot_sts_token(self, bot_id: str):
@@ -204,8 +203,8 @@ def _get_endpoint_certificate(self, endpoint_id: str) -> key_agreement_client:
204203
if self._certificate_manager is None:
205204
cert_path = os.environ.get("E2E_CERTIFICATE_PATH")
206205
if (self.ak is None or self.sk is None) and cert_path is None and self.api_key is None:
207-
raise ArkAPIError("must set (ak and sk) or (E2E_CERTIFICATE_PATH) \
208-
or (api_key) before get endpoint token.")
206+
raise ArkAPIError("must set (api_key) or (ak and sk) \
207+
or (E2E_CERTIFICATE_PATH) before get endpoint token.")
209208
self._certificate_manager = E2ECertificateManager(self.ak, self.sk, self.region, self._base_url, self.api_key)
210209
return self._certificate_manager.get(endpoint_id)
211210

@@ -310,10 +309,12 @@ class CertificateResponse():
310309

311310
def __init__(self, ak: str, sk: str, region: str, base_url: str | URL = BASE_URL, api_key: str | None = None):
312311
self._certificate_manager: Dict[str, key_agreement_client] = {}
312+
313+
# local cache prepare
313314
self._init_local_cert_cache()
314315

316+
# api instance prepare
315317
import volcenginesdkcore
316-
317318
self._api_instance_enabled = True
318319
if ak is None or sk is None:
319320
self._api_instance_enabled = False
@@ -322,18 +323,20 @@ def __init__(self, ak: str, sk: str, region: str, base_url: str | URL = BASE_URL
322323
configuration.sk = sk
323324
configuration.region = region
324325
configuration.schema = "https"
325-
326326
volcenginesdkcore.Configuration.set_default(configuration)
327327
self.api_instance = volcenginesdkark.ARKApi()
328328

329+
# global cert path prepare
329330
self.cert_path = os.environ.get("E2E_CERTIFICATE_PATH")
330331

332+
# ark client prepare
331333
self.client = Ark(
332334
base_url=base_url,
333335
api_key=api_key,
334336
ak=ak, sk=sk,
335337
)
336338
self._e2e_uri = "/e2e/get/certificate"
339+
self._x_session_token = {'X-Session-Token': self._e2e_uri}
337340

338341
def _load_cert_by_cert_path(self) -> str:
339342
with open(self.cert_path, 'r') as f:
@@ -353,12 +356,13 @@ def _load_cert_by_ak_sk(self, ep: str) -> str:
353356
return resp.pca_instance_certificate
354357

355358
def _sync_load_cert_by_auth(self, ep: str) -> str:
356-
try:
357-
resp = self.client.post(self._e2e_uri, body={"model": ep}, cast_to=self.CertificateResponse)
359+
try: # try to make request with session header (used for header statistic)
360+
resp = self.client.post(self._e2e_uri, options={"headers": self._x_session_token},
361+
body={"model": ep}, cast_to=self.CertificateResponse)
358362
except Exception as e:
359363
raise ArkAPIError("Getting Certificate failed: %s\n" % e)
360364
return resp['Certificate']
361-
365+
362366
def _save_cert_to_file(self, ep: str, cert_pem: str):
363367
cert_file_path = os.path.join(self._cert_storage_path, f"{ep}.pem")
364368
with open(cert_file_path, 'w') as f:
@@ -379,7 +383,7 @@ def _load_cert_locally(self, ep: str) -> str | None:
379383

380384
def _init_local_cert_cache(self):
381385
self._cert_storage_path = "/tmp/ark/certificates"
382-
self._cert_expiration_seconds = 14 * 24 * 60 * 60 # 14 days
386+
self._cert_expiration_seconds = 14 * 24 * 60 * 60 # 14 days
383387

384388
if not os.path.exists(self._cert_storage_path):
385389
os.makedirs(self._cert_storage_path)

0 commit comments

Comments
 (0)