-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiexplorer.html
503 lines (459 loc) · 15.4 KB
/
apiexplorer.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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8' />
<title>NMA API Explorer</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
<link href="//fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<style>
html {
background-color: #F39C12;
background-image: -moz-linear-gradient(center top, #5e8ec6, #E4E4E4);
}
body {
background-color: transparent;
font-family: 'Open Sans', 'Helvetica Neue', Arial, sans-serif;
font-size: 10pt;
padding: 1px 0 30px;
}
h1, h2, h3, h4, h5, h6 {
font-family: 'Open Sans', 'Helvetica Neue', Arial, sans-serif;
}
hr {
border-top: 1px solid #CCCCCC;
}
#page {
background-color: #FFFFFF;
border-radius: 5px 5px 5px 5px;
box-shadow: 0 0 5px #000000;
clear: left;
padding: 10px 0 0;
margin-top: 20px;
min-height: 800px;
}
#page article {
margin: 0 43px;
padding: 30px 0 20px;
}
.objRow {
display: table-row;
}
.objTable {
display: table;
}
.name {
color: #117864;
font-weight: bold;
display: table-cell;
min-width: 60px;
padding-right: 8px;
}
.value {
display: table-cell;
}
.noLinkValue {
color: SlateGray;
font-weight: bold;
}
.nestedvalue {
display: table-cell;
margin-top: 20px;
}
</style>
</head>
<body>
<section id='page' class='container'>
<article>
<a href="http://www.nma.gov.au/collections/api">API home</a>
<div class='well'>
<h3>National Museum of Australia API Explorer</h3>
<div style="margin-top: 20px;">
<form name="searchForm">
<select name="endpoint" id="formEndpoint">
<option value="object">Object</option>
<option value="narrative">Narrative</option>
<option value="party">Party</option>
<option value="place">Place</option>
<option value="collection">Collection</option>
<option value="media">Media</option>
</select>
<input type="text" name="keywords" id="formKeywords" placeholder="Enter your keywords" value="*" />
<select name="field" id="formField">
<option value="text">Anywhere</option>
<option value="title">Title</option>
<option value="description">Description</option>
<option value="type">Type</option>
<option value="additionalType">Additional Type</option>
<option value="all">--- Objects: ---</option>
<option value="creator">Creator</option>
<option value="temporal">Date</option>
<!--
<option value="temporal_date">Date (structured)</option>
-->
<option value="spatial">Place</option>
<option value="contributor">Contributor</option>
<option value="collection">Collection</option>
<option value="medium">Material</option>
<option value="acknowledgement">Acknowledgement</option>
<option value="location">Exhibit</option>
<!--
<option value="contentDescription">Content description</option>
<option value="physicalDescription">Physical description</option>
<option value="significanceStatement">Significance</option>
<option value="educationalSignificance">Educational significance</option>
-->
<!--
<option value="length">Length</option>
<option value="height">Height</option>
<option value="width">Width</option>
<option value="depth">Depth</option>
<option value="diameter">Diameter</option>
<option value="weight">Weight</option>
-->
<option value="rights">Rights</option>
<option value="identifier">Identifier</option>
<option value="modified">Modified</option>
<!--
<option value="modified_date">Modified (structured)</option>
<option value="issued">Released online</option>
<option value="source">Inward loan</option>
<option value="seeAlso">Web link</option>
-->
<option value="media">Media</option>
<option value="all">--- Parties: ---</option>
<option value="name">Name</option>
<option value="gender">Gender</option>
<option value="all">--- Related objects: ---</option>
<option value="collection_id">Obj collected in (coll id)</option>
<option value="creator_id">Obj created by (party id)</option>
<option value="contributor_id">Obj contributed by (party id)</option>
<option value="spatial_id">Obj located in (place id)</option>
<option value="media_id">Obj represented by (media id)</option>
<option value="related_object_id">Obj related to (object id)</option>
<option value="hasPart_object_id">Obj parent of (object id)</option>
<option value="isPartOf_object_id">Obj child of (object id)</option>
<option value="isAggregatedBy_narrative_id">Obj child of (narrative id)</option>
<option value="all">--- Related narratives: ---</option>
<option value="hasPart_narrative_id">Narr parent of (narrative id)</option>
<option value="isPartOf_narrative_id">Narr child of (narrative id)</option>
<option value="aggregates_object_id">Narr parent of (object id)</option>
<option value="all">--- Related media: ---</option>
<option value="media_object_id">Media represents (object id)</option>
</select>
<select name="sort" id="formSort">
<option value="">Relevance</option>
<option value="title_sort asc">Title (asc)</option>
<option value="title_sort desc">Title (desc)</option>
<option value="modified_sort asc">Modified (asc)</option>
<option value="modified_sort desc">Modified (desc)</option>
</select>
<select name="format" id="formFormat">
<option value="simple">Simple DC</option>
<option value="json-ld">Linked Art</option>
</select>
<select name="size" id="formSize">
<option>5</option>
<option>10</option>
<option>50</option>
<option>100</option>
</select>
<input type="submit" id="formSearch" value="Search" /> <a
id="formReset" href="#">Reset</a>
</form>
<div id="searchUrl"></div>
<div id="resultsCount"></div>
</div>
</div>
<div id='top'></div>
</article>
</section>
<script type="text/javascript">
//var API_BASE = 'https://data.nma.gov.au';
var API_BASE = '';
$(function(){
// set up reset-form link
$('#formReset').attr("href", window.location.href.split("?")[0]);
// page called to display a single object
let pageId = _getParameterByName('id');
let apiKey = _getParameterByName('apiKey');
if (apiKey != null) {
$('#formApiKey').val(apiKey);
}
if (pageId) {
try {
let url = API_BASE + '/' + pageId + '?format=simple';
$('#searchUrl').html('<a href="' + url + '">' + url + '</a>');
// retrieve from API then process
let apiHeaders = new Headers();
fetch(url, {headers: apiHeaders})
.then(_checkStatus)
.then(function(response) {
return response.json()
}).then(function(json) {
displayResponse(json)
}).catch(function(ex) {
console.log('retrieve failed:', ex);
alert("There was an error: " + ex);
})
} catch (e) {
alert("There was an error: " + e.message);
}
}
// page called to run query
if (_getParameterByName('keywords')) {
$('#resultsCount').html('');
$('#searchUrl').html('');
// populate the form fields
let keywords = _getParameterByName('keywords');
let endpoint = _getParameterByName('endpoint');
let format = _getParameterByName('format');
let field = _getParameterByName('field');
let sort = _getParameterByName('sort');
let size = _getParameterByName('size');
$('#formKeywords').val(keywords);
$('#formEndpoint').val(endpoint);
$('#formFormat').val(format);
$('#formField').val(field);
$('#formSort').val(sort);
$('#formSize').val(size);
if (format == null || format == '') {
format = 'simple';
}
// run query
doQuery(keywords, field, endpoint, format, sort, size, apiKey);
}
});
// run query and display results
function doQuery(keywords, field, endpoint, format, sort, size, apiKey) {
let parts = [];
if (keywords == null) {
parts.push(field + "=*");
} else {
if (keywords == '') keywords = '*';
parts.push(field + "=" + keywords);
}
let query = parts.join("&");
if (endpoint == null || endpoint == '') endpoint='object';
// retrieve from API then process
try {
// currently API doesn't accept empty sort param
let sortParam = '';
if (sort != '') {
sortParam = '&sort=' + sort;
}
let url = API_BASE + '/' + endpoint + '?limit=' + size + '&' + query + sortParam;
if (format != null) {
url += "&format=" + format;
}
$('#searchUrl').html('<a href=\'' + url + '\'>' + url + '</a>');
let apiHeaders = new Headers();
// apiHeaders.append('Access-Control-Allow-Origin', '*');
// fetch(url, {headers: apiHeaders, mode: "no-cors"})
fetch(url)
.then(_checkStatus)
.then(function(response) {
return response.json();
}).then(function(json) {
displayResponse(json)
}).catch(function(ex) {
console.log('retrieve failed:', ex);
alert("There was an error: " + ex);
})
} catch (e) {
alert("There was an error: " + e.message);
}
}
// display an API response
function displayResponse(responseJson) {
$('#top').html('');
if (responseJson.errors) {
$('#resultsCount').html('<p style="color:red;">Error: ' + responseJson.errors[0].title + '</p>' );
} else if (responseJson.meta && responseJson.data != null) {
// simple format results
$('#resultsCount').html('<p>' + responseJson.meta.results + ' results</p>' );
} else if (responseJson.entities) {
// json-ld format results
$('#resultsCount').html('<p>' + responseJson.entities + ' results</p>' );
} else {
// resource request so no result count
$('#resultsCount').html('');
}
// simple format - search results or single object
if (responseJson.data) {
if ($.type(responseJson.data) === 'array') {
responseJson.data.forEach(function(r){
$('#top').append( '<div class="container table table-hover">' + displayObject(r, 0) + '</div>' );
$('#top').append( '<hr style="border-top: 5px solid green;" />' );
})
}
}
// json-ld format - search results or single object
if (responseJson.aggregates) {
if ($.type(responseJson.aggregates) === 'array') {
responseJson.aggregates.forEach(function(r){
$('#top').append( '<div class="container table table-hover">' + displayObject(r, 0) + '</div>' );
$('#top').append( '<hr style="border-top: 5px solid green;" />' );
})
}
}
}
// display a single object or property within an object
function displayObject(obj, depth) {
let objType = $.type(obj);
let objUrl = '?id=' + getEndpoint(obj.type) + '/' + obj.id;
let apiType = obj.type;
let html = "";
if (depth === 0) {
// header
let headerTitle = "untitled";
if (obj.title != null) {
headerTitle = obj.title;
} else if (obj.label != null) {
headerTitle = obj.label;
} else if (obj.name != null) {
headerTitle = obj.name;
} else if (obj.identified_by != null && obj.identified_by[0].value != null) {
headerTitle = obj.identified_by[0].value;
}
headerTitle = headerTitle.replace(/&/g, '&');
headerTitle = headerTitle.replace(/</g, '<');
headerTitle = headerTitle.replace(/>/g, '>');
$('#top').append( '<h4><a href="' + objUrl + '">' + headerTitle + '</a> - ' + obj.type + '</h4>' );
}
if (objType === 'string') {
// display leaf node
obj = obj.replace(/&/g, '&');
obj = obj.replace(/</g, '<');
obj = obj.replace(/>/g, '>');
if (obj.startsWith("http")) {
html += '<div class="value"><a href="' + obj + '">' + obj + '</a></div>';
} else {
html += '<div class="value">' + obj + '</div>';
}
} else {
for(let prop in obj) {
// ignore JSON-LD context
if (prop === '@context') continue;
html += '<div class="objRow">';
let name = prop[0].toUpperCase() + prop.substring(1);
html += '<div class="name">' + name + '</div>';
switch($.type(obj[prop])) {
case 'object':
// recurse to display nested object
html += '<div class="nestedvalue">' + displayObject(obj[prop], depth+1) + '</div>';
break;
case 'array':
// recurse to display each nested object
html += '<div class="nestedvalue">';
for (let a = 0; a < obj[prop].length; a++) {
html += displayObject(obj[prop][a], depth+1);
if ($.type(obj[prop][a]) === "string") {
html += '<span style="clear:both;" />';
} else {
html += '<hr style="margin:5px;" />';
}
}
html += '</div>';
break;
default:
// display this prop
let value = obj[prop];
// encode for HTML display
if ($.type(value) === 'string') {
value = value.replace(/&/g, '&');
value = value.replace(/</g, '<');
value = value.replace(/>/g, '>');
}
switch(prop) {
case 'type':
html += '<div class="value" style="font-style:italic;">' + value + '</div>';
break;
case 'title':
if (!obj.id) {
// no id so there is no endpoint
html += '<div class="value"><span class="noLinkValue">' + value + '</span></div>';
} else {
html += '<div class="value"><a href="' + objUrl + '">' + value + '</a></div>';
}
break;
case 'geo':
html += '<div class="value">' + value + '<br />';
html += '<iframe width="400" height="150" src="https://maps.google.com/maps?q=' + value + '&hl=en&z=10&output=embed" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>';
html += '</div>';
break;
default:
if ($.type(value) === 'string' && value.startsWith("http")) {
html += '<div class="value"><a href="' + value + '">' + value + '</a>';
// display thumbnail image
if (value.includes('prodderivT') || value.includes('200x200')) {
html += '<br /><img src="' + value + '" height="100" />';
}
html += '</div>';
} else {
html += '<div class="value">' + value + '</div>';
}
}
}
html += '</div>';
}
}
return html;
}
// get API endpoint name for the specified object type
function getEndpoint(objectType) {
switch(objectType) {
case 'Person':
case 'Organisation':
case 'person':
case 'organisation':
case 'party':
return('party');
case 'Place':
case 'place':
return('place');
case 'Narrative':
case 'narrative':
return('narrative');
case 'Collection':
case 'collection':
return('collection');
case 'StillImage':
case 'MovingImage':
case 'Sound':
case 'media':
return('media');
default:
return("object");
}
}
// throw error if response not HTTP 2xx OK or 404 not found
function _checkStatus(response) {
// search results
let resultCount = response.headers.get('Result-Count');
console.log(resultCount);
if (resultCount != '') {
$('#resultsCount').html('<p>' + resultCount + ' results</p>' );
}
if ( (response.status >= 200 && response.status < 300) || response.status == 404) {
return response
} else {
let error = new Error(response.status + " " + response.statusText)
error.response = response
throw error
}
}
// extract querystring parameter from page URL
function _getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
let regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
</script>
</body>
</html>