-
Notifications
You must be signed in to change notification settings - Fork 2
/
ServiceCatalographer.py
173 lines (106 loc) · 3.84 KB
/
ServiceCatalographer.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import sys, urllib.parse
import requests
requestJSON = {'Accept': 'application/json'}
def getAll(requestLink, resultKey=None):
page = 0
totalPages = 1
results = []
while page < totalPages:
params = {
'page': page + 1, # URL page numbers are 1-based
'per_page': 50
}
request = requests.get(requestLink, params=params, headers=requestJSON)
result = request.json()
if len(result) != 1:
raise RuntimeError('expected single top-level result')
for key in result:
# assign the last (and only) key value to the variable 'key'
pass
assert key == resultKey, key
pageResults = result[key]
totalPages = pageResults['pages']
results.extend(pageResults['results'])
page += 1
return results
def convert(result, cache):
if isinstance(result, dict):
result = Resource(result, cache)
elif isinstance(result, list):
result = [convert(value, cache) for value in result]
elif cache.isResource(result):
result = CacheResource(result, cache)
return result
class Resource:
def __init__(self, result, cache):
self._values = result
self._cache = cache
if 'self' in self._values:
self._values['annotations'] = self._values['self'] + '/annotations'
def __getitem__(self, name):
result = self._values[name]
if name != 'self':
result = convert(result, self._cache)
return result
def __setitem__(self, name, value):
self._values[name] = value
def __getattr__(self, name):
try:
return self[name]
except KeyError:
raise AttributeError(name)
def __contains__(self, name):
return name in self._values
def __iter__(self):
return iter(self._values)
def __str__(self):
return repr(self)
def __repr__(self):
return '\n'.join(['%s: %r' % (name, self._values[name]) for name in self])
class CacheResource(str):
def __new__(cls, data, cache):
s = str.__new__(cls, data)
return s
def __init__(self, data, cache):
self.__cache = cache
def __call__(self):
return self.__cache.getResource(self)
def __getattr__(self, name):
raise AttributeError("%s().%s" % (self, name))
class NotValidResource(Exception):
pass
class ServiceCatalographer:
def __init__(self, url):
self.url = url
self.cache = {}
def getFullURL(self, urlStub):
return urllib.parse.urljoin(self.url, urlStub)
def isResource(self, resource):
if isinstance(resource, str):
return resource.startswith(self.url)
return False
def getResource(self, resource):
result = self.cache.get(resource)
if result is None:
if self.isResource(resource):
response = requests.get(resource, headers={'Accept': 'application/json'})
response.raise_for_status()
result = Resource(response.json(), self)
self.cache[resource] = result
else:
raise NotValidResource(resource)
return result
def getServices(self):
services = getAll(self.getFullURL('services'), 'services')
return [self.getService(service['resource']) for service in services]
def getService(self, url):
service = self.getResource(url).service
service['summary'] = service.self + '/summary'
return service
def getServiceId(self, serviceId):
return self.getService(self.getFullURL('services/%d' % serviceId))
if __name__ == '__main__':
bdc = ServiceCatalographer('https://www.biodiversitycatalogue.org/')
resource = bdc.getFullURL('services/1')
service = bdc.getServiceId(1)
print(service)