-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrest_utils.py
101 lines (82 loc) · 2.91 KB
/
rest_utils.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
import requests
import json
def get_json_response(url, post=False, timeout=10):
"""
:param url:
:param post:
:param timeout: default is 10 seconds
:return:
"""
try:
response = (
requests.post(url, timeout=timeout)
if post
else requests.get(url, timeout=timeout)
)
response.raise_for_status()
return json.loads(response.text)
except requests.exceptions.HTTPError as errh:
print("Http Error:", errh)
except requests.exceptions.ConnectionError as errc:
print("Error Connecting:", errc)
except requests.exceptions.Timeout as errt:
print("Timeout Error:", errt)
except requests.exceptions.RequestException as err:
print("Other error:", err)
# on error return None
return None
def get_json_response_with_headers(url, headers, body, post=False, timeout=10):
try:
req_type = "POST" if post else "GET"
response = requests.request(
req_type, url, json=body, headers=headers, timeout=timeout
)
response.raise_for_status()
return json.loads(response.text)
except requests.exceptions.HTTPError as errh:
print("Http Error:", errh)
except requests.exceptions.ConnectionError as errc:
print("Error Connecting:", errc)
except requests.exceptions.Timeout as errt:
print("Timeout Error:", errt)
except requests.exceptions.RequestException as err:
print("Other error:", err)
# on error return None
return None
def json_col(
result_df, json_column, prefix, field, apply_function=None, new_col_name=None
):
if new_col_name is None:
new_col_name = field
full_column_name = f"{prefix}_{new_col_name}"
if apply_function is None:
result_df[full_column_name] = [
jo[field] if jo and field in jo else None for jo in json_column
]
else:
result_df[full_column_name] = [
None if jo is None or field not in jo else apply_function(jo[field])
for jo in json_column
]
return result_df
def extract_external_descriptors(json_array):
# [{'source': 'CHEBI', 'source_id': 'CHEBI:48565', 'annotations': ['organic heteropentacyclic compound',
# 'methyl ester', 'yohimban alkaloid']}]
return join(
[
"{} ({}):{}".format(json["source"], json["source_id"], json["annotations"])
for json in json_array
]
)
def extract_name(json):
return json["name"] if json is not None else None
def join(json_array, sep=";"):
return sep.join(json_array)
def join_by_field(json, field, sep=";"):
if json is None:
return None
return sep.join(json[field])
def join_array_by_field(json_array, field, sep=";"):
return sep.join([json[field] for json in json_array if json and field in json])
def extract_names_array(json_array):
return join_array_by_field(json_array, "name")