Skip to content

Commit

Permalink
Added portal_utils.Portal.key_id
Browse files Browse the repository at this point in the history
  • Loading branch information
dmichaels-harvard committed Dec 13, 2023
1 parent a7f8677 commit 34e7466
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Change Log
* Minor fix to misc_utils.to_integer to handle float strings.
* Minor fix to structured_data to accumulate unique resolved_refs across schemas.
* Changes to structured_data to respect uniqueItems for arrays.
* Handle no schemas better in structured_data.
* Added portal_utils.Portal.ping().
* Minor fix in portal_utils.Portal._uri().


8.5.0
Expand Down
18 changes: 17 additions & 1 deletion dcicutils/portal_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def __init__(self,
self._server = portal._server
self._key = portal._key
self._key_pair = portal._key_pair
self._key_id = portal._key_id
self._key_file = portal._key_file
return
self._vapp = None
Expand All @@ -70,6 +71,7 @@ def __init__(self,
self._server = server
self._key = None
self._key_pair = None
self._key_id = None
self._key_file = None
if isinstance(portal, (VirtualApp, TestApp)):
self._vapp = portal
Expand All @@ -95,6 +97,10 @@ def __init__(self,
self._key = key_manager.get_keydict_for_server(self._server)
self._key_pair = key_manager.keydict_to_keypair(self._key) if self._key else None
self._key_file = key_manager.keys_file
if self._key and (key_id := self._key.get("key")):
self._key_id = key_id
elif self._key_pair and (key_id := self._key_pair[1]):
self._key_id = key_id

@property
def env(self):
Expand All @@ -116,6 +122,10 @@ def key(self):
def key_pair(self):
return self._key_pair

@property
def key_id(self):
return self._key_id

@property
def key_file(self):
return self._key_file
Expand Down Expand Up @@ -206,13 +216,19 @@ def breadth_first(super_type_map: dict, super_type_name: str) -> dict:
super_type_map_flattened[super_type_name] = breadth_first(super_type_map, super_type_name)
return super_type_map_flattened

def ping(self) -> bool:
try:
return self.get("/health").status_code == 200
except Exception:
return False

def _uri(self, uri: str) -> str:
if not isinstance(uri, str) or not uri:
return "/"
if uri.lower().startswith("http://") or uri.lower().startswith("https://"):
return uri
uri = re.sub(r"/+", "/", uri)
return (self._server + ("/" if uri.startswith("/") else "") + uri) if self._server else uri
return (self._server + ("/" if not uri.startswith("/") else "") + uri) if self._server else uri

def _kwargs(self, **kwargs) -> dict:
result_kwargs = {"headers":
Expand Down
12 changes: 8 additions & 4 deletions dcicutils/structured_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ def __init__(self, schema_json: dict, portal: Optional[Portal] = None) -> None:

@staticmethod
def load_by_name(name: str, portal: Portal) -> Optional[dict]:
return Schema(portal.get_schema(Schema.type_name(name)), portal) if portal else None
schema_json = portal.get_schema(Schema.type_name(name)) if portal else None
return Schema(schema_json, portal) if schema_json else None

def validate(self, data: dict) -> List[str]:
errors = []
Expand Down Expand Up @@ -565,16 +566,19 @@ def get_metadata(self, object_name: str) -> Optional[dict]:

@lru_cache(maxsize=256)
def get_schema(self, schema_name: str) -> Optional[dict]:
if (schemas := self.get_schemas()) and (schema := schemas.get(schema_name := Schema.type_name(schema_name))):
if not (schemas := self.get_schemas()):
return None
if schema := schemas.get(schema_name := Schema.type_name(schema_name)):
return schema
if schema_name == schema_name.upper() and (schema := schemas.get(schema_name.lower().title())):
return schema
if schema_name == schema_name.lower() and (schema := schemas.get(schema_name.title())):
return schema

@lru_cache(maxsize=1)
def get_schemas(self) -> dict:
schemas = super().get_schemas()
def get_schemas(self) -> Optional[dict]:
if not (schemas := super().get_schemas()) or (schemas.get("status") == "error"):
return None
if self._schemas:
schemas = copy.deepcopy(schemas)
for user_specified_schema in self._schemas:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dcicutils"
version = "8.5.0.1b2" # TODO: To become 8.6.0
version = "8.5.0.1b3" # TODO: To become 8.6.0
description = "Utility package for interacting with the 4DN Data Portal and other 4DN resources"
authors = ["4DN-DCIC Team <support@4dnucleome.org>"]
license = "MIT"
Expand Down

0 comments on commit 34e7466

Please sign in to comment.