-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi_interface.html
194 lines (175 loc) · 9.05 KB
/
api_interface.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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HiSPARC - jSparc - API interface</title>
<link rel="shortcut icon" href="images/favicon.ico">
<link rel="stylesheet" href="styles/common.css">
<link rel="stylesheet" href="styles/api_interface.css">
<script src="scripts/jquery-3.7.1.min.js"></script>
<script src="jquery.jsparc.js"></script>
<script>
"use strict";
var jsparc,
man = {"api_man": "",
"api_clusters": "clusters/",
"api_clusters_in_country": "countries/{country_number}/",
"api_configuration": "station/{station_number}/config/{year}/{month}/{day}/",
"api_countries": "countries/",
"api_event_trace": "station/{station_number}/trace/{ext_timestamp}/",
"api_has_data": "station/{station_number}/data/{year}/{month}/{day}/",
"api_has_weather": "station/{station_number}/weather/{year}/{month}/{day}/",
"api_number_of_events": "station/{station_number}/num_events/{year}/{month}/{day}/{hour}/",
"api_pulseheight_drift": "station/{station_number}/plate/{plate_number}/pulseheight/drift/{year}/{month}/{day}/{number_of_days}/",
"api_pulseheight_fit": "station/{station_number}/plate/{plate_number}/pulseheight/fit/{year}/{month}/{day}/",
"api_station_info": "station/{station_number}/{year}/{month}/{day}/",
"api_stations": "stations/",
"api_stations_in_subcluster": "subclusters/{subcluster_number}/",
"api_stations_with_data": "stations/data/{year}/{month}/{day}/",
"api_stations_with_weather": "stations/weather/{year}/{month}/{day}/",
"api_subclusters": "subclusters/",
"api_subclusters_in_cluster": "clusters/{cluster_number}/"};
function syntaxHighlight(json) {
/* Syntax highlighting for JSON
from: http://stackoverflow.com/a/7220510/1033535
*/
if (typeof json != 'string') {
json = JSON.stringify(json, undefined, 4);}
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g,
function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';}
else {
cls = 'string';}}
else if (/true|false/.test(match)) {
cls = 'boolean';}
else if (/null/.test(match)) {
cls = 'null';}
return '<span class="' + cls + '">' + match + '</span>';});
}
function year_options() {
var select = $('<select>'),
years = jsparc.range(2004, new Date().getFullYear() + 1);
select.append($('<option>').attr('value', '').text('year'));
for (var i = 0; i < years.length; i++) {
select.append($('<option>').attr('value', years[i]).text(years[i]));}
return select;
}
function month_options() {
var select = $('<select>'),
months = jsparc.range(1, 13);
select.append($('<option>').attr('value', '').text('month'));
for (var i = 0; i < months.length; i++) {
select.append($('<option>').attr('value', months[i]).text(months[i]));}
return select;
}
function day_options() {
var select = $('<select>'),
days = jsparc.range(1, 32);
select.append($('<option>').attr('value', '').text('day'));
for (var i = 0; i < days.length; i++) {
select.append($('<option>').attr('value', days[i]).text(days[i]));}
return select;
}
function hour_options() {
var select = $('<select>'),
hours = jsparc.range(0, 24);
select.append($('<option>').attr('value', '').text('hour'));
for (var i = 0; i < hours.length; i++) {
select.append($('<option>').attr('value', hours[i]).text(hours[i]));}
return select;
}
function replace_options_with_inputs(url) {
var options = /{(\w+)}/g,
input = "<input type='text' name='$1' size='19' placeholder='$1'>";
return url.replace(options, input);
}
function getParamNames(func) {
/* Get param names
from: http://stackoverflow.com/a/9924463/1033535
*/
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg,
ARGUMENT_NAMES = /([^\s,]+)/g,
fnStr = func.toString().replace(STRIP_COMMENTS, ''),
result = fnStr.slice(fnStr.indexOf('(') + 1, fnStr.indexOf(')')).match(ARGUMENT_NAMES);
if (result === null)
{result = [];}
return result;
}
$(document).ready(function () {
// Animate HiSPARC logo during ajax activity
$(document).bind('ajaxStart', function() {
$('#pageHeader').addClass('animated');})
.bind('ajaxStop', function() {
$('#pageHeader').removeClass('animated');});
// Load jSparc library
jsparc = $.jsparc();
var api_list = [];
// Get all api functions
for (var fun in jsparc) {
if (fun.slice(0, 4) == 'api_') {
api_list.push(fun);}}
var select = $('<select>');
for (var i = 0; i < api_list.length; i++) {
select.append($('<option>').text(api_list[i].slice(4))
.attr('value', api_list[i]));}
$('#api_urls').html(select);
$('#api_urls').on('change', 'select', function() {
var url = jsparc.API_URL + "/" + man[$(this).val()];
$('#url_options').html(replace_options_with_inputs(url));
$('#url_options [name=year]').replaceWith(year_options());
$('#url_options [name=month]').replaceWith(month_options());
$('#url_options [name=day]').replaceWith(day_options());
$('#url_options [name=hour]').replaceWith(hour_options());
});
$('#execute').on('click', function() {
var options = [],
inputs = $('#url_options input, #url_options select').each(function() {options.push($(this).val());});
var url = jsparc[$('#api_urls select').val()].apply(this, options);
$('#result_url').text(url);
jsparc.get_json(url)
.done(function (data) {
$('#result_json').html(syntaxHighlight(data));})
.fail(function () {
$('#result_json').html('Failed to retrieve url, possibly missing arguments.');});
$('#url, #result').show();
});
$(window).keyup(function (e) {
if (e.keyCode === 13) {
$('#execute').click();}
});
});
</script>
</head>
<body>
<div id="container">
<div id="header"><div id="pageHeader"></div></div>
<div id="doc_link">
<a href="https://docs.hisparc.nl/publicdb/api_tutorial.html" target="jsparc_doc">➔ Documentation</a>
<a href="index.html">➔ jSparc</a>
</div>
<div id="api">
<div id="api_url">
<h3>Choose API option</h3>
<span id="api_urls"></span>
</div>
<div id="options">
<h3>URL</h3>
<pre id="url_options">https://data.hisparc.nl/api/</pre>
</div>
<input type='button' value="Execute!" id="execute">
</div>
<div id="url">
<h3>Executed url</h3>
<pre id="result_url"></pre>
</div>
<div id="result">
<h3>Retrieved JSON</h3>
<pre id="result_json"></pre>
</div>
</div>
</body>
</html>