diff --git a/CHANGELOG.rst b/CHANGELOG.rst index cc20fc041..16d295207 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 diff --git a/dcicutils/portal_utils.py b/dcicutils/portal_utils.py index 82a34f574..33cf7eb87 100644 --- a/dcicutils/portal_utils.py +++ b/dcicutils/portal_utils.py @@ -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 @@ -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 @@ -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): @@ -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 @@ -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": diff --git a/dcicutils/structured_data.py b/dcicutils/structured_data.py index 71400929a..ef309e921 100644 --- a/dcicutils/structured_data.py +++ b/dcicutils/structured_data.py @@ -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 = [] @@ -565,7 +566,9 @@ 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 @@ -573,8 +576,9 @@ def get_schema(self, schema_name: str) -> Optional[dict]: 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: diff --git a/pyproject.toml b/pyproject.toml index cbc4d28dc..0ba2fb089 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 "] license = "MIT"