-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_api_requests.py
139 lines (108 loc) · 5.3 KB
/
code_api_requests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Filename: code_api_requests.py
# Author: Kayla O'Sullivan-Steben
# Date Created: January 21, 2025
# Description: Contains functions to make API calls to ICD, SNOMED, and UMLS, to get code display names in different languages.
from Authentication import *
from config import *
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
# NOTE: for production, you should probably not disable these warnings
# Global variables to store authentication tokens and headers for API requests
# This is done so that they aren't authenticated on import for the cases in which someone only wants to use one of them
# ICD
icd_token = None
icd_headers = None
# UMLS
umls_auth_client = None
umls_tgt = None
# SNOWMED Header
sct_headers = {'Authorization': 'Basic',
'Accept': 'application/json',
'Accept-Language': 'en',
'API-Version': 'v2',
'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Mobile Safari/537.36 Edg/97.0.1072.62'}
# ICD Authentication
def authenticate_icd():
'''
authenticate_icd Authenticates with the ICD API and sets global variables for tokens and headers, if not already done.
'''
global icd_token, icd_headers
if icd_token == None:
# Token
r = requests.post(token_endpoint, data=payload, verify=False).json()
icd_token = r['access_token']
# Headers for API requests
icd_headers = {
'Authorization': 'Bearer ' + icd_token,
'Accept': 'application/json',
'Accept-Language': 'en',
'API-Version': 'v2'
}
# UMLS Authentication
def authenticate_umls():
'''
authenticate_umls Authenticates with the UMLS API and sets global variables for authentication client and ticket, if not done.
'''
global umls_auth_client, umls_tgt
if umls_auth_client == None:
AuthClient = Authentication(umls_api_key)
tgt = AuthClient.gettgt()
def get_icd_display(code, lang):
'''
get_icd_display Retrieves the display name for an ICD code in the specified language.
:PARAM code (str): The ICD code to look up.
:PARAM lang (str): The language code for the desired language.
:Returns: The display name of the ICD code in the specified language, or an empty string if not found.
'''
# Note: for ICD language codes supported: https://icd.who.int/icdapi/docs2/SupportedClassifications/
authenticate_icd()
uri = 'https://id.who.int/icd/'+icd_version+'/' + code # access ICD API --> can update version here
icd_headers['Accept-Language'] = lang # Set header field to desired language
r = requests.get(uri, headers=icd_headers, verify=False) # make request
if 'Requested resource could not be found' in r.text:
print("WARNING: No response received for code",code,"in",lang)
return '' # Return empty string if no reponse received found
response = r.json()
return response['title']['@value']
def get_snomed_display(code, lang):
'''
get_snomed_display Retrieves the display name for a SNOMED code in the specified language.
:PARAM code (str): The SNOMED code to look up.
"PARAM lang (str): The language code for the desired language.
:Returns: The display name of the SNOMED code in the specified language, or an empty string if not found.
'''
uri = 'https://browser.ihtsdotools.org/snowstorm/snomed-ct/browser/'+snomed_edition+'/'+snomed_version+'/concepts/' + code # access snomed API
sct_headers['Accept-Language'] = lang # Set header field to desired language
r = requests.get(uri, headers=sct_headers, verify=False) # make request
if 'Concept not found' in r.text:
print("WARNING: No response received for code",code,"in",lang)
return '' # Return empty string if no reponse received found
response = r.json()
return response['pt']['term'].replace("'","''") if lang in response['pt']['lang'] else ''
def get_umls_display(code, lang):
'''
get_umls_display Retrieves the display name for a UMLS code in the specified language.
:PARAM code (str): The UMLS code to look up.
:PARAM lang (str): The language code for the desired language.
:Returns: The display name of the UMLS code in the specified language, or an empty string if not found.
'''
authenticate_umls()
base_uri = "https://uts-ws.nlm.nih.gov/rest"
endpoint = "/content/" + umls_version + "/CUI/" + code + "/atoms"
# query = {'ticket':AuthClient.getst(tgt)}
query = {'apiKey':umls_api_key}
# If not english, must search for the code's atoms that have the given language
# Can add more statements if other languages included
if "fr" in lang.lower():
endpoint = endpoint + "?language=FRE"
elif "en" in lang.lower():
endpoint = endpoint+"?language=ENG"
else:
endpoint = endpoint+"?language="+lang
r = requests.get(base_uri+endpoint, params=query,verify=False)
if ('No results' in r.text):
print("WARNING: No response received for code",code,"in",lang)
return '' # Return empty string if no reponse received found
response = r.json()
return response['result'][0]['name']