|
import requests |
|
from config.settings import settings |
|
from services.logger import app_logger |
|
|
|
|
|
|
|
UMLS_BASE_URL = "https://uts-ws.nlm.nih.gov/rest" |
|
BIOPORTAL_BASE_URL = "http://data.bioontology.org" |
|
|
|
def search_umls_term(term: str) -> dict: |
|
if not settings.UMLS_API_KEY: |
|
app_logger.warning("UMLS_API_KEY not set. UMLS search will be mocked.") |
|
return {"results": [{"term": term, "cui": "C0000000", "definition": "Mocked UMLS definition."}]} |
|
|
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
|
|
|
|
|
|
|
return {"results": [{"name": f"Mocked UMLS result for {term}"}]} |
|
except requests.RequestException as e: |
|
app_logger.error(f"UMLS API request failed: {e}") |
|
return {"error": str(e)} |
|
except Exception as e: |
|
app_logger.error(f"Error processing UMLS search for '{term}': {e}") |
|
return {"error": f"Unexpected error during UMLS search: {str(e)}"} |
|
|
|
|
|
def search_bioportal_term(term: str, ontology: str = "SNOMEDCT") -> dict: |
|
if not settings.BIOPORTAL_API_KEY: |
|
app_logger.warning("BIOPORTAL_API_KEY not set. BioPortal search will be mocked.") |
|
return {"collection": [{"prefLabel": term, "cui": ["C0000000"], "definition": ["Mocked BioPortal definition."]}]} |
|
|
|
headers = {"Authorization": f"apikey token={settings.BIOPORTAL_API_KEY}"} |
|
params = {"q": term, "ontologies": ontology, "include": "prefLabel,synonym,definition,cui"} |
|
|
|
try: |
|
response = requests.get(f"{BIOPORTAL_BASE_URL}/search", params=params, headers=headers) |
|
response.raise_for_status() |
|
return response.json() |
|
except requests.RequestException as e: |
|
app_logger.error(f"BioPortal API request failed: {e}") |
|
return {"error": str(e)} |
|
except Exception as e: |
|
app_logger.error(f"Error processing BioPortal search for '{term}': {e}") |
|
return {"error": f"Unexpected error during BioPortal search: {str(e)}"} |