Newer
Older
import requests
import urllib
class RORAPIHandler:
API_URL = "https://api.dev.ror.org/v2/organizations"
# "https://api.ror.org/organizations" ... for v1
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
def query(self, query_string, all_status=True):
# URL-encode query_string to make it safe for use in a URL
query_string = urllib.parse.quote(query_string)
url = f'{self.API_URL}?query="{query_string}"'
if all_status:
url += "&all_status"
response = requests.get(url)
data = response.json()
items = list(map(self._process_organization, data["items"]))
return {**data, "items": items}
def from_id(self, ror_id):
# For old grid IDs, use a query instead
# and only return a result if there is exactly one
if ror_id.startswith("grid"):
results = self.query(ror_id)
if results["number_of_results"] != 1:
return {}
else:
return results["items"][0]
# Handle ROR IDs normally
url = f"{self.API_URL}/{ror_id}"
response = requests.get(url)
try:
data = response.json()
response = self._process_organization(data)
except:
response = {}
return response
@staticmethod
def _process_organization(organization):
"""
Processes an organization from the ROR API into a dict that can be used
to create a new Organization object.
"""
# Remove url part from ROR ID
ror_link = organization["id"]
ror_id = ror_link.split("/")[-1]
return {**organization, "id": ror_id, "ror_link": ror_link}