nmdc-runtime 1.5.0__py3-none-any.whl → 1.7.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of nmdc-runtime might be problematic. Click here for more details.

@@ -19,7 +19,6 @@ from frozendict import frozendict
19
19
  from linkml_runtime.dumpers import json_dumper
20
20
  from pydantic import BaseModel, AnyUrl
21
21
  from pymongo import MongoClient, ReplaceOne, InsertOne
22
- from terminusdb_client import WOQLClient
23
22
  from toolz import get_in
24
23
  from toolz import merge
25
24
 
@@ -372,16 +371,37 @@ def gold_api_client_resource(context: InitResourceContext):
372
371
 
373
372
  @dataclass
374
373
  class NmdcPortalApiClient:
374
+
375
375
  base_url: str
376
- # Using a cookie for authentication is not ideal and should be replaced
377
- # when this API has an another authentication method
378
- session_cookie: str
376
+ refresh_token: str
377
+ access_token: Optional[str] = None
378
+ access_token_expires_at: Optional[datetime] = None
379
+
380
+ def _request(self, method: str, endpoint: str, **kwargs):
381
+ r"""
382
+ Submits a request to the specified API endpoint;
383
+ after refreshing the access token, if necessary.
384
+ """
385
+ if self.access_token is None or datetime.now() > self.access_token_expires_at:
386
+ refresh_response = requests.post(
387
+ f"{self.base_url}/auth/refresh",
388
+ json={"refresh_token": self.refresh_token},
389
+ )
390
+ refresh_response.raise_for_status()
391
+ refresh_body = refresh_response.json()
392
+ self.access_token_expires_at = datetime.now() + timedelta(
393
+ seconds=refresh_body["expires_in"]
394
+ )
395
+ self.access_token = refresh_body["access_token"]
379
396
 
380
- def fetch_metadata_submission(self, id: str) -> Dict[str, Any]:
381
- response = requests.get(
382
- f"{self.base_url}/api/metadata_submission/{id}",
383
- cookies={"session": self.session_cookie},
397
+ headers = kwargs.get("headers", {})
398
+ headers["Authorization"] = f"Bearer {self.access_token}"
399
+ return requests.request(
400
+ method, f"{self.base_url}{endpoint}", **kwargs, headers=headers
384
401
  )
402
+
403
+ def fetch_metadata_submission(self, id: str) -> Dict[str, Any]:
404
+ response = self._request("GET", f"/api/metadata_submission/{id}")
385
405
  response.raise_for_status()
386
406
  return response.json()
387
407
 
@@ -389,13 +409,13 @@ class NmdcPortalApiClient:
389
409
  @resource(
390
410
  config_schema={
391
411
  "base_url": StringSource,
392
- "session_cookie": StringSource,
412
+ "refresh_token": StringSource,
393
413
  }
394
414
  )
395
415
  def nmdc_portal_api_client_resource(context: InitResourceContext):
396
416
  return NmdcPortalApiClient(
397
417
  base_url=context.resource_config["base_url"],
398
- session_cookie=context.resource_config["session_cookie"],
418
+ refresh_token=context.resource_config["refresh_token"],
399
419
  )
400
420
 
401
421
 
@@ -512,33 +532,3 @@ def get_mongo(run_config: frozendict):
512
532
  )
513
533
  )
514
534
  return mongo_resource(resource_context)
515
-
516
-
517
- class TerminusDB:
518
- def __init__(self, server_url, user, key, account, dbid):
519
- self.client = WOQLClient(server_url=server_url)
520
- self.client.connect(user=user, key=key, account=account)
521
- db_info = self.client.get_database(dbid=dbid, account=account)
522
- if db_info is None:
523
- self.client.create_database(dbid=dbid, accountid=account, label=dbid)
524
- self.client.create_graph(graph_type="inference", graph_id="main")
525
- self.client.connect(user=user, key=key, account=account, db=dbid)
526
-
527
-
528
- @resource(
529
- config_schema={
530
- "server_url": StringSource,
531
- "user": StringSource,
532
- "key": StringSource,
533
- "account": StringSource,
534
- "dbid": StringSource,
535
- }
536
- )
537
- def terminus_resource(context):
538
- return TerminusDB(
539
- server_url=context.resource_config["server_url"],
540
- user=context.resource_config["user"],
541
- key=context.resource_config["key"],
542
- account=context.resource_config["account"],
543
- dbid=context.resource_config["dbid"],
544
- )