-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.js
102 lines (88 loc) · 3.36 KB
/
api.js
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
/**
* ProvStore API jQuery plugin
*
* Example usage:
*
* var api = new $.provStoreApi({
* username: '<username>',
* key: '<api_key>'
* });
*
* api.submitDocument("document_name", { "activity": { "a1": {} } }, function(new_document_id){console.log(new_document_id)},
* function(error){console.error(error)});
*
* api.addBundle(<target_document_id>, "bundle_identifier", { "activity": { "a1": {} } },
* function(){console.log('added')}, function(error){console.error(error)});
*
* api.getDocument(<target_document_id>, function(response){console.log(response)}, function(error){console.error(error)});
* api.deleteDocument(<target_document_id>, function(){console.log('deleted')}, function(error){console.error(error)});
*
*/
(function ($, undefined) {
$.provStoreApi = function (options) {
this.settings = $.extend({
location: "https://provenance.ecs.soton.ac.uk/store/api/v0/"
}, options);
};
$.provStoreApi.prototype._authorizationHeader = function () {
return "ApiKey " + this.settings.username + ":" + this.settings.key;
};
$.provStoreApi.prototype.request = function (path, data, method, callback, err) {
var headers = {'Accept': "application/json"};
if (this.settings.username && this.settings.key) {
headers.Authorization = this._authorizationHeader();
}
$.ajax(this.settings.location + path, {
contentType: "application/json",
headers: headers,
data: data ? method == 'GET' ? data : JSON.stringify(data) : null,
type: method || 'GET',
error: function (jqXHR, textStatus, errorThrown) {
err(errorThrown);
},
success: callback || function () {
}
});
};
$.provStoreApi.prototype.submitDocument = function (identifier, prov_document, isPublic, callback, err) {
if (isPublic === undefined) {
isPublic = false;
}
var data = {
'content': prov_document,
'public': isPublic,
'rec_id': identifier || null
};
this.request('documents/', data, 'POST', function (response) {
callback(response.id);
}, err);
};
$.provStoreApi.prototype.getDocuments = function (offset, limit, callback, err) {
var data = {};
if (offset) {
data.offset = offset;
}
if (limit) {
data.limit = limit;
}
this.request('documents/', data, 'GET', callback, err);
};
$.provStoreApi.prototype.getDocument = function (id, callback, err) {
this.request('documents/' + id + '/', null, 'GET', callback, err);
};
$.provStoreApi.prototype.getDocumentBody = function (id, format, callback, err) {
this.request('documents/' + id + '.' + format, null, 'GET', callback, err);
};
$.provStoreApi.prototype.addBundle = function (id, identifier, prov_bundle, callback, err) {
var data = {
'content': prov_bundle,
'rec_id': identifier
};
this.request('documents/' + id + '/bundles/', data, 'POST', function (response) {
callback(response);
}, err);
};
$.provStoreApi.prototype.deleteDocument = function (id, callback, err) {
this.request('documents/' + id + '/', null, 'DELETE', callback, err);
};
}(jQuery));