-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_api.py
51 lines (38 loc) · 1.11 KB
/
generate_api.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
"""
Generate all API methods from SMART OWL ontology
"""
import os
import re
import json
import common
import common.rdf_tools.rdf_ontology as rdf_ontology
pFormat = "{.*?}"
class SmartResponse:
def __init__(self, resp, body):
self.response = resp
self.body = body
try:
self.graph = common.rdf_tools.util.parse_rdf(body)
except:
self.graph = None
try:
self.json = json.loads(body)
except:
self.json = None
def params(call):
return [x[1:-1] for x in re.findall(pFormat, str(call.path))]
def make_generic_call(call):
def c(self, *args, **kwargs):
url = str(call.path)
f = getattr(self, str(call.http_method).lower())
resp, body = f(url, *args, **kwargs)
return SmartResponse(resp, body)
return c
def augment(client_class):
for c in rdf_ontology.api_calls:
call = make_generic_call(c)
setattr(client_class, c.client_method_name, call)
call.__doc__ = """%s %s
%s
Returns RDF Graph containing: %s
""" % (c.http_method, c.path, c.description, c.target)