11import requests
22from typing import Optional
3+ import yaml
34
45# TODO: Update og_url to be regular nmdc api url when berkeley is implemented
56
6- class ApiInfoRetriever :
7+ class NmdcApiInfoRetriever :
78 def __init__ (self , collection_name : str ):
89 self .collection_name = collection_name
910
1011 def get_id_by_slot_from_collection (self , slot_name : str , slot_field_value : str ):
1112 """
12- Retrieve the identifier from a specified collection based on a slot name and field value.
13+ Retrieve the NMDC identifier from a specified collection based on a slot name and field value.
1314
1415 Parameters
1516 ----------
@@ -51,3 +52,73 @@ def get_id_by_slot_from_collection(self, slot_name: str, slot_field_value: str):
5152
5253 return identifier
5354
55+ class BioOntologyInfoRetriever :
56+ """
57+ Client for retrieving ENVO term information from BioPortal API.
58+
59+ A class to handle authentication and retrieval of Environmental Ontology (ENVO)
60+ terms using the BioPortal REST API service.
61+
62+ Parameters
63+ ----------
64+ config_path : str
65+ Path to YAML configuration file containing BioPortal API credentials
66+
67+ Notes
68+ -----
69+ The configuration file should contain an 'api_key' field with a valid
70+ BioPortal API key.
71+
72+ Examples
73+ --------
74+ >>> retriever = BioOntologyInfoRetriever('config.yaml')
75+ >>> envo_terms = retriever.get_envo_terms('ENVO:00002042')
76+ >>> print(envo_terms)
77+ {'ENVO:00002042': 'surface water'}
78+ """
79+ def __init__ (self , config_path : str ):
80+ self .config = config_path
81+
82+ def get_envo_terms (self , envo_id : dict ):
83+ """
84+ Look up an ENVO term label using BioPortal API.
85+
86+ Parameters
87+ ----------
88+ envo_id : str
89+ The ENVO identifier to look up (e.g., 'ENVO:00002042')
90+
91+ Returns
92+ -------
93+ dict
94+ Dictionary with envo_id as key and term label as value
95+ Example: {'ENVO:00002042': 'surface water'}
96+
97+ Raises
98+ ------
99+ requests.exceptions.RequestException
100+ If the API request fails
101+ KeyError
102+ If the response doesn't contain expected data format
103+ yaml.YAMLError
104+ If the config file cannot be parsed
105+ FileNotFoundError
106+ If the config file is not found
107+
108+ Notes
109+ -----
110+ Makes an authenticated request to BioPortal API to retrieve the
111+ preferred label (prefLabel) for the given ENVO term.
112+ """
113+
114+ config = yaml .safe_load (open (self .config ))
115+ api_key = config ['api_key' ]
116+
117+ url = f"http://data.bioontology.org/ontologies/ENVO/classes/{ envo_id } "
118+ headers = {"Authorization" : f"apikey token={ api_key } " }
119+
120+ response = requests .get (url , headers = headers )
121+ response .raise_for_status ()
122+
123+ data = response .json ()
124+ return {envo_id : data ['prefLabel' ]}
0 commit comments