diff --git a/src/app.py b/src/app.py index 82d7119d..08145748 100644 --- a/src/app.py +++ b/src/app.py @@ -3342,7 +3342,7 @@ def get_prov_info(): distinct_organ_uuid_list.append(item['uuid']) organ_code = item['organ'].upper() - validate_organ_code(organ_code, organ_types_dict) + validate_organ_code(organ_code) distinct_organ_type_list.append(organ_types_dict[organ_code].lower()) internal_dict[HEADER_ORGAN_HUBMAP_ID] = distinct_organ_hubmap_id_list @@ -3662,7 +3662,7 @@ def get_prov_info_for_dataset(id): distinct_organ_uuid_list.append(item['uuid']) organ_code = item['organ'].upper() - validate_organ_code(organ_code, organ_types_dict ) + validate_organ_code(organ_code) distinct_organ_type_list.append(organ_types_dict[organ_code].lower()) internal_dict[HEADER_ORGAN_HUBMAP_ID] = distinct_organ_hubmap_id_list @@ -3858,7 +3858,7 @@ def sankey_data(): internal_dict[HEADER_DATASET_GROUP_NAME] = dataset[HEADER_DATASET_GROUP_NAME] organ_code = dataset[HEADER_ORGAN_TYPE].upper() - validate_organ_code(organ_code, organ_types_dict) + validate_organ_code(organ_code) internal_dict[HEADER_ORGAN_TYPE] = organ_types_dict[organ_code].lower() @@ -5554,9 +5554,8 @@ def access_level_prefix_dir(dir_name): ---------- organ_code : str """ -def validate_organ_code(organ_code, organ_types_dict=None): - if organ_types_dict is None: - organ_types_dict = schema_manager.get_organ_types() +def validate_organ_code(organ_code): + organ_types_dict = schema_manager.get_organ_types() if not organ_code.isalpha() or not len(organ_code) == 2: internal_server_error(f"Invalid organ code {organ_code}. Must be 2-letter alphabetic code") diff --git a/src/schema/schema_manager.py b/src/schema/schema_manager.py index 4012465f..ff93c0b2 100644 --- a/src/schema/schema_manager.py +++ b/src/schema/schema_manager.py @@ -39,6 +39,7 @@ _neo4j_driver = None _memcached_client = None _memcached_prefix = None +_organ_types = None #################################################################################################### @@ -2160,29 +2161,34 @@ def delete_memcached_cache(uuids_list): """ def get_organ_types(): global _ontology_api_url + global _organ_types - target_url = _ontology_api_url + SchemaConstants.ONTOLOGY_API_ORGAN_TYPES_ENDPOINT + if _organ_types is None: + target_url = _ontology_api_url + SchemaConstants.ONTOLOGY_API_ORGAN_TYPES_ENDPOINT - # Use Memcached to improve performance - response = make_request_get(target_url, internal_token_used = True) + # Use Memcached to improve performance + response = make_request_get(target_url, internal_token_used = True) - # Invoke .raise_for_status(), an HTTPError will be raised with certain status codes - response.raise_for_status() + # Invoke .raise_for_status(), an HTTPError will be raised with certain status codes + response.raise_for_status() - if response.status_code == 200: - return response.json() - else: - # Log the full stack trace, prepend a line with our message - logger.exception("Unable to make a request to query the organ types via ontology-api") + if response.status_code == 200: + _organ_types = response.json() + return _organ_types + else: + # Log the full stack trace, prepend a line with our message + logger.exception("Unable to make a request to query the organ types via ontology-api") - logger.debug("======get_organ_types() status code from ontology-api======") - logger.debug(response.status_code) + logger.debug("======get_organ_types() status code from ontology-api======") + logger.debug(response.status_code) - logger.debug("======get_organ_types() response text from ontology-api======") - logger.debug(response.text) + logger.debug("======get_organ_types() response text from ontology-api======") + logger.debug(response.text) - # Also bubble up the error message from ontology-api - raise requests.exceptions.RequestException(response.text) + # Also bubble up the error message from ontology-api + raise requests.exceptions.RequestException(response.text) + else: + return _organ_types """