This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.html
263 lines (224 loc) · 11.3 KB
/
console.html
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
---
layout: default
title: API Console
---
<script type="text/javascript">
var BASE = 'https://api.redlink.io/';
</script>
<div class="row">
<div class="span12">
<div class="container">
<h1>Redlink API Console</h1>
<p>
Simple console to play with the <a href="api">Redlink API</a>.
</p>
</div>
<div class="card">
<div class="card-body">
<p style="display: inline; vertical-align: middle;">
<strong>API Key: </strong>
<input type="password" id="api_key" style="border-radius: 5px; background-color: #eeeeee; width: 400px; margin: 0 0 0.3em 0.5em;" />
<a href="faq#get-key" title="where can I get an api key?">
<img src="https://cdn.redlink.io/dev/img/help.png" alt="help" width="32" height="32" />
</a>
<input type="checkbox" id="hide_key" checked /> Hide key
<span id="api_key_message" class="alert">API Key is required!</span>
</p>
<p id="console_uri_builder">
<select id="api_method"><option value="GET" style="background-color:#0f6ab4;color:#ffffff;font-weight:bold;">GET</option><option value="POST" style="background-color:#10a54a;color:#ffffff;font-weight:bold;" selected>POST</option><option value="PUT" style="background-color:#c5862b;color:#ffffff;font-weight:bold;">PUT</option><option value="DELETE" style="background-color:#a41e22;color:#ffffff;font-weight:bold;">DELETE</option></select>
https://api.redlink.io/<select id="api_version"><option value="1.0">1.0</option></select>/<select id="api_service"><option value="analysis">analysis</option><option value="data">data</option></select>/<select id="api_instance"></select><select id="api_endpoint"></select>
<a class="btn btn-block" id="api_submit" href="#" style="width: 200px; display: inline-block; margin: auto auto 0.5em 3em;">send</a>
</p>
<div id="devtools" style="display: none;">
<p>This is the request URI: <a id="console_uri" href="#"></a></p>
<pre id="console_curl"><code></code></pre>
</div>
<div class="row">
<div class="span4">
<label for="api_request">request body</label>
<textarea id="api_request"></textarea>
</div>
<div class="span7">
<label>raw response</label>
<pre id="api_response" class="prettyprint"></pre>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.redlink.io/dev/js/prettify.js"></script>
<script type="text/javascript">
prettyPrint();
var key = undefined;
var analyses = [];
var datasets = [];
var endpoints = { "analysis" : ["/enhance"],
"data": ["/resource", "/sparql/select", "/ldpath"],
"data-with-dataset": ["", "/resource", "/sparql/select", "/sparql/update", "/ldpath"]
};
function getRequestURI() {
var api_version = $("select#api_version").find(":selected").val();
var api_service = $("select#api_service").find(":selected").val();
var api_instance = $("select#api_instance").find(":selected").val() || "";
if (api_instance.length > 0) api_instance = "/" + api_instance;
var api_endpoint = $("select#api_endpoint").find(":selected").val() || "";
return BASE + api_version + "/" + api_service + api_instance + api_endpoint;
}
function updateDevTools() {
var uri = getRequestURI();
if(!$("input#hide_key").is(':checked')) {
uri += "?key=" + $("input#api_key").val();
} else {
uri += "?key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
}
uri += "&out=json";
$("#console_uri").text(uri).attr("href", uri);
var api_method = $("select#api_method").find(":selected").val();
$("pre#console_curl code").text("curl -iX " + api_method + " '" + uri +
"' -H 'Content-Type: text/plain' --data '" + $("textarea#api_request").val().trim().replace(/\n/g, " ").replace(/\s+/g, " ") + "'");
}
function populateSelect(target, values) {
target.empty();
values.forEach(function(value) {
target.append($("<option/>", {
value: value,
text: value
}));
});
}
function checkStatus(key, version) {
var TO,
display = $("#api_key_message").hide().empty()
.removeClass("alert-info alert-success alert-danger alert-error");
if (TO) clearTimeout(TO);
$.ajax({
dataType: "json",
url: BASE+version+"?key="+key,
success: function(data) {
if (data.accessible) {
var msg = "Key VALID, " + data.requests + " of " + data.limit + " requests used.";
display.text(msg).addClass("alert-success")
.fadeIn(function() { TO=setTimeout(function() {display.fadeOut();}, 7500); });
updateDevTools();
$("#devtools").fadeIn(function() { TO=setTimeout(function() {display.fadeOut();}, 7500); });
analyses = data.analyses;
datasets = data.datasets;
datasets.unshift("");
var service = $("select#api_service").find(":selected").val();
if (service == "analysis") {
populateSelect($("select#api_instance"), analyses);
} else if (service == "data") {
populateSelect($("select#api_instance"), datasets);
} else {
log.error("unknown service " + service);
}
populateSelect($("select#api_endpoint"), endpoints[service]);
} else {
display.text("invalid key!").addClass("alert-error")
.fadeIn(function() { TO=setTimeout(function() {display.fadeOut();}, 10000); });
}
},
error: function(data) {
display.text("invalid key!").addClass("alert-error")
.fadeIn(function() { TO=setTimeout(function() {display.fadeOut();}, 10000); });
}
});
}
(function(jQuery){
jQuery( document ).ready( function() {
$("#api_base_url").text(BASE);
$("#console_uri").click(function(e) {e.preventDefault();e.stopPropagation(); return false;});
$("input#hide_key").click(function() {
if($(this).is(':checked')) {
$("input#api_key").attr("type", "password");
} else {
$("input#api_key").attr("type", "text");
}
updateDevTools();
});
$("textarea#api_request").keyup(updateDevTools);
$("input#api_key").keyup(function() {
var new_key = $(this).val();
if (new_key.length == 40 && new_key != key) {
key = new_key;
console.log("new key: " + key);
var version = $("select#api_version").find(":selected").val();
checkStatus(key, version);
}
});
$("select#api_service").change(function() {
var service = $(this).find(":selected").val();
populateSelect($("select#api_instance"), (service == "analysis" ? analyses : datasets));
populateSelect($("select#api_endpoint"), endpoints[service]);
updateDevTools();
});
$("select#api_instance").change(function() {
var service = $("select#api_service").find(":selected").val();
var instance = $(this).find(":selected").val();
if (service == "data" && instance != "") {
populateSelect($("select#api_endpoint"), endpoints["data-with-dataset"]);
} else {
populateSelect($("select#api_endpoint"), endpoints[service]);
}
updateDevTools();
});
$("select#api_version").change(updateDevTools);
$("select#api_endpoint").change(updateDevTools);
$("a#api_submit").mousedown(function () {
var api_key = $("input#api_key").val();
if (api_key.length == 40) {
var api_method = $("select#api_method").find(":selected").val();
var api_uri = getRequestURI();
var api_call = api_uri + "?key=" + api_key + "&out=json";
console.info("submit to " + api_call);
var text = $("textarea#api_request").val().trim();
//console.log("text: " + text);
var parent = $("pre#api_response").parent();
parent.empty();
parent.html('<label>response</label><pre id="api_response" class="prettyprint"></pre>');
$.ajax({
type: api_method,
url: api_call,
data: text,
contentType: "text/plain",
headers: { Accept : "application/json; charset=utf-8" },
processData: false
}).success(function(data) {
//console.log("raw response: " + data.responseText);
try {
var json = JSON.stringify(data, undefined, 2);
$("pre#api_response").text(json);
} catch (e) {
log.error(e);
$("pre#api_response").text(data.responseText);
}
prettyPrint();
}).error(function(){
alert("could not load data");
}).complete(function() {
// Every test on the API is as good as a reload ;-)
_paq.push(["trackPageView"]);
});
} else {
alert("invalid key");
}
return false;
});
location.queryString = {};
location.search.substr(1).split("&").forEach(function (pair) {
if (pair === "") return;
var parts = pair.split("=");
//TODO: pass also the concrete endpoint
if (parts[0] == "key" && parts[1] && parts[1].length == 40) {
key = parts[1];
console.log("key from query parameter: " + key);
$("input#api_key").val(key);
var version = $("select#api_version").find(":selected").val();
checkStatus(key, version);
return false;
}
});
});
}(jQuery))
</script>