-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatasetu.py
More file actions
223 lines (165 loc) · 5.14 KB
/
datasetu.py
File metadata and controls
223 lines (165 loc) · 5.14 KB
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
import sys
import json
import requests
catalog = "datasetu.org"
class datasetu:
#{
def __init__(self,certificate,key):
if certificate and key:
self.credentials = (certificate,key)
else:
self.credentials = None
@staticmethod
def search(options = {}, catalog = catalog_of_catalogs):
catalog = "https://" + catalog + "/catalog/v1/search"
response = requests.get(catalog)
return json.loads(response.text)
@staticmethod
def get_metadata(items, catalog = catalog_of_catalogs):
if type(items) == type("string"):
items = [items]
else:
items = items
catalog = "https://" + catalog + "/catalog/v1/items/"
result = {}
for i in items:
url = catalog + i
response = requests.get(url=url,verify=True)
if response.status_code == 200:
result[i] = json.loads(response.text)[0]
else:
sys.stderr.write (
"WARNING: catalog API failure | " +
url + "/" + " | " +
response.reason + " | " +
response.text
)
result[i] = None
return result
@staticmethod
def get_latest_data(items, token = None, server_token = None):
if type(items) == type("string"):
items = [items]
else:
items = items
result = {}
metadata = datasetu.get_metadata(items)
for i in items:
split = i.split("/")
rs = split[2]
provider = metadata[i]["providerId"].split(":")[-1]
url = "https://" + rs + "/resource-server/" + provider + "/v1/search"
body = {
"id" : i,
"options" : "latest"
}
if token:
body["token"] = token
body["server-token"] = server_token[rs]
body = json.dumps(body)
response = requests.post(url=url,verify=True,data=body)
if response.status_code == 200:
result[i] = json.loads(response.text)[0]
else:
sys.stderr.write (
"WARNING: resource-server API failure | " +
url + "/" + " | " +
response.reason + " | " +
response.text
)
result[i] = None
return result
class auth:
#{
def __init__(self, certificate, key, auth_server="auth.datasetu.org", version=1):
self.url = "https://" + auth_server + "/auth/v" + str(version)
self.credentials = (certificate, key)
def call(self, api, body=None):
ret = True # success
body = json.dumps(body)
response = requests.post (
url=self.url + "/" + api,
verify=True,
cert=self.credentials,
data=body,
headers={"content-type": "application/json"}
)
if response.status_code != 200:
sys.stderr.write(
"WARNING: auth API failure | " +
self.url + "/" + api + " | " +
response.reason + " | " +
response.text
)
ret = False # failed
if response.headers['content-type'] == 'application/json':
return {'success':ret, 'response':json.loads(response.text)}
else:
sys.stderr.write(
"WARNING: auth did not send 'application/json'"
)
return {'success':False, 'response':None}
def get_token(self, items, token_time=None, existing_token=None):
body = {'request': {}}
for i in items:
body["request"]["id"] = i
if token_time:
body['token-time'] = token_time
if existing_token:
body['existing-token'] = existing_token
return self.call("token", body)
def get_token_complex (self, request, token_time=None, existing_token=None):
body = {'request': request}
if token_time:
body['token-time'] = token_time
if existing_token:
body['existing-token'] = existing_token
return self.call("token", body)
def get_certificate_info(self):
return self.call("certificate-info")
def get_policy(self):
return self.call("acl")
def set_policy(self, policy):
body = {'policy': policy}
return self.call("acl/set", body)
def append_policy(self, policy):
body = {'policy': policy}
return self.call("acl/append", body)
def introspect_token(self, token, server_token=None, request = None):
body = {'token': token}
if server_token:
body['server-token'] = server_token
if request:
body['request'] = request
return self.call("token/introspect", body)
def revoke_tokens(self, tokens):
if type(tokens) is type([]):
body = {'tokens': tokens}
else:
body = {'tokens': [tokens]}
return self.call("token/revoke", body)
def revoke_token_hashes(self, token_hashes):
if type(token_hashes) is type([]):
body = {'token-hashes': token_hashes}
else:
body = {'token-hashes': [token_hashes]}
return self.call("token/revoke", body)
def revoke_all_tokens (self, serial, fingerprint):
body = {'serial':serial, 'fingerprint': fingerprint}
return self.call("token/revoke-all", body)
def audit_tokens(self, hours):
body = {'hours': hours}
return self.call("audit/tokens", body)
def add_consumer_to_group(self, consumer, group, valid_till):
body = {'consumer': consumer, 'group': group, 'valid-till': valid_till}
return self.call("group/add", body)
def delete_consumer_from_group(self, consumer, group):
body = {'consumer': consumer, 'group': group}
return self.call("group/delete", body)
def list_group(self, consumer, group=None):
body = {'consumer': consumer}
if group:
body['group'] = group
return self.call("group/list", body)
#}
#}