-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatenraum.py
376 lines (277 loc) · 10.6 KB
/
datenraum.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import time
import os
import sys
from enum import Enum
from dataclasses import dataclass
import requests
from requests.auth import HTTPBasicAuth
def create_datenraum_session():
env = get_current_environment()
client_id = os.environ.get("CLIENT_ID")
client_secret = os.environ.get("CLIENT_SECRET")
assert client_id is not None
assert client_secret is not None
session = Session(env, Credentials(client_id, client_secret))
client = Client(session)
return client.create_source(
slug="serlo", name="Serlo Education e.V.", organization="Serlo Education e.V."
)
class Source:
"""
Represents a source in the Datenraum for creating, updating, or deleting nodes or edges
"""
def __init__(self, session, source_id):
self.session = session
self.source_id = source_id
def add_node(self, node, node_type="LearningOpportunity", log_error=True):
data = self.convert_node_to_request_body(node, node_type)
response = self.session.post_json(
"/api/core/nodes", json=data, params={"metadataValidation": "Amb"}
)
is_success = response.status_code == 201
if not is_success and log_error:
log_response(response, "Could not add " + node["id"])
return is_success
def update_node(
self, node, node_id, node_type="LearningOpportunity", log_error=True
):
data = self.convert_node_to_request_body(node, node_type)
response = self.session.put_json(
f"/api/core/nodes/{node_id}",
json=data,
params={"metadataValidation": "Amb"},
)
is_success = response.status_code == 204
if not is_success and log_error:
log_response(response, "Could not update " + node["id"] + " for " + node_id)
return is_success
def delete_node(self, node_id, log_error=True):
response = self.session.delete(f"/api/core/nodes/{node_id}")
is_success = response.status_code == 204
if not is_success and log_error:
log_response(response, "Could not delete " + node_id)
return is_success
def get_nodes(self, offset, limit=100):
result = self.session.get_json(
"/api/core/nodes",
params={"sourceId": self.source_id, "limit": limit, "offset": offset},
)
assert result is not None
return result["_embedded"]["nodes"]
def get_node_from_external_id(self, external_id):
return self.session.get_json(
f"/api/core/nodes/external/{self.source_id}",
params={"externalId": external_id},
)
def get_nodes_by_taxonomy(self, taxonomy_id, limit=100):
result = self.session.get_json(
"/api/core/nodes",
params={
"sourceId": self.source_id,
"limit": limit,
"referencedByTail": taxonomy_id,
},
)
assert result is not None
return result["_embedded"]["nodes"]
def get_node_by_id(self, node_id):
result = self.session.get_json(f"/api/core/nodes/{node_id}")
assert result is not None
return result
def convert_node_to_request_body(self, node, node_type="LearningOpportunity"):
language = node.get("inLanguage", [])[:1]
node["@context"] = [
"https://w3id.org/kim/amb/context.jsonld",
"https://schema.org",
{"@language": language[0] if language else "de"},
]
assert "id" in node and isinstance(node["id"], str)
assert "name" in node and isinstance(node["name"], str)
data = {
"title": node["name"],
"sourceId": self.source_id,
"externalId": node["id"],
"metadata": {"Amb": node},
"nodeClass": node_type,
}
if "description" in node:
data["description"] = node["description"]
return data
def add_edge_type(self, name, description, slug):
response = self.session.post_json(
"/api/core/edge-types",
json={
"name": name,
"description": description,
"slug": slug,
},
)
assert response.status_code == 201
def get_edge_types(self):
return self.session.get_json("/api/core/edge-types")
def add_edge(self, edge_type_id, tail_node_id, head_node_id):
response = self.session.put_json(
f"/api/core/edges/{edge_type_id}/{tail_node_id}/{head_node_id}",
json={
"metadata": {
"isAiGenerated": False,
}
},
)
assert response.status_code in (201, 204)
def delete_edge_type(self, edge_type_id):
self.session.delete(f"/api/core/edge-types/{edge_type_id}")
def get_edges(self):
response = self.session.get_json("/api/core/edges")
return response
def delete_edge(self, edge_type_id, tail_node_id, head_node_id):
response = self.session.delete(
f"/api/core/edges/{edge_type_id}/{tail_node_id}/{head_node_id}"
)
assert response.status_code in (201, 204)
class Client:
"""
A client for accessing the Datenraum.
"""
def __init__(self, session):
self.session = session
def create_source(self, slug, name, organization):
source = self.get_source(slug)
if source is None or "id" not in source:
self.register_source(slug, name, organization)
source = self.get_source(slug)
assert source is not None
if (
"name" not in source
or "organization" not in source
or name != source["name"]
or organization != source["organization"]
):
self.update_source(source["id"], name, organization)
return Source(self.session, source["id"])
def register_source(self, slug, name, organization):
response = self.session.post_json(
"/api/core/sources",
{"organization": organization, "name": name, "slug": slug},
)
assert response.status_code == 201
def update_source(self, source_id, name, organization):
response = self.session.patch_json(
f"/api/core/sources/{source_id}",
{"organization": organization, "name": name},
)
assert response.status_code == 204
def get_source(self, slug):
return self.session.get_json(f"/api/core/sources/slug/{slug}")
class Session:
"""
Class for handling API calls to the Datenraums. Manages authentication token creation and renewal.
"""
def __init__(self, env, credentials):
self.token = None
self.env = env
self.credentials = credentials
self.session = requests.Session()
def post_json(self, endpoint, json, params=None):
return self.send(
requests.Request("POST", self.base_url + endpoint, json=json, params=params)
)
def put_json(self, endpoint, json, params=None):
return self.send(
requests.Request("PUT", self.base_url + endpoint, json=json, params=params)
)
def patch_json(self, endpoint, json):
return self.send(
requests.Request(
"PATCH",
self.base_url + endpoint,
json=json,
headers={"Content-Type": "application/json-patch+json"},
)
)
def get_json(self, endpoint, params=None):
response = self.send(
requests.Request(
"GET",
self.base_url + endpoint,
params=params,
headers={"Accept": "application/json"},
)
)
if response.status_code == 404:
return None
try:
return response.json()
except requests.exceptions.RequestException as error:
sys.stderr.write(str(response.status_code) + "\n")
sys.stderr.write(response.text + "\n")
raise error
def delete(self, endpoint):
return self.send(requests.Request("DELETE", self.base_url + endpoint))
def send(self, req, no_retries=0):
if self.is_token_expired():
self.update_token()
assert self.token is not None
access_token = self.token["access_token"]
req.headers.update({"Authorization": f"Bearer {access_token}"})
response = self.session.send(req.prepare())
if response.status_code == 401 and no_retries == 0:
# Authorization has failed -> retry the call with an updated token once
self.update_token()
return self.send(req, no_retries=no_retries + 1)
return response
def update_token(self):
response = self.session.post(
self.authentication_url,
auth=HTTPBasicAuth(self.credentials.identifier, self.credentials.secret),
headers={"Content-Type": "application/x-www-form-urlencoded"},
data={"grant_type": "client_credentials"},
)
if response.status_code != 200:
raise IOError(response.text)
self.token = response.json()
self.token["expires_at"] = current_time() + self.token["expires_in"] - 20
def is_token_expired(self):
return self.token is None or self.token["expires_at"] <= current_time()
@property
def base_url(self):
return (
"https://dam.demo.meinbildungsraum.de/datenraum"
if self.env == Environment.DEMO
else "https://dam-dev.nbpdev.de/datenraum"
)
@property
def authentication_url(self):
return (
"https://aai.demo.meinbildungsraum.de/realms/nbp-aai/protocol/openid-connect/token"
if self.env == Environment.DEMO
else "https://aai-dev.nbpdev.de/realms/nbp-aai/protocol/openid-connect/token"
)
@dataclass
class Credentials:
"""
Data class for storing client credentials.
"""
identifier: str
secret: str
class Environment(Enum):
"""
Enum representing different environments.
"""
DEV = 1
DEMO = 2
def get_current_environment():
env = os.environ.get("DATENRAUM_ENVIRONMENT")
env = env.strip().lower() if env is not None else None
if env == "demo":
return Environment.DEMO
if env == "dev":
return Environment.DEV
raise ValueError("Illegal state: DATENRAUM_ENVIRONMENT must be defined")
def current_time():
return int(time.time())
def log_response(response, message):
print(f"ERROR: {message}")
print("Status code: ", response.status_code)
print("Response text: ", response.text)
print()