-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsandcage.js
131 lines (121 loc) · 4.24 KB
/
sandcage.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
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
var SandCage;
SandCage = (function() {
var SANDCAGE_API = {};
var API_VERSION = "0.2";
var ENDPOINT_BASE = "https://api.sandcage.com/" + API_VERSION + "/";
function SandCage(apikey) {
this.apikey = apikey;
if (!this.apikey) {
return {status: 'error', name: 'MissingKey', message: 'Provide your SandCage API Key.'};
}
SANDCAGE_API = this;
if (typeof JSON === "undefined" || JSON === null) {
this.loadScript('json2.min.js');
}
return this;
}
SandCage.call = function(service_endpoint, params, callback_endpoint, onresult) {
var key, payload;
if ((params == null) || params === {}) {
return false;
}
payload = {
key: SANDCAGE_API.apikey
};
for (key in params) {
payload[key] = params[key];
}
return this.ajaxCall(service_endpoint, payload, callback_endpoint, onresult);
};
SandCage.ajaxCall = function(service_endpoint, payload, callback_endpoint, onresult) {
/*
global: XMLHttpRequest
*/
var req;
if (!callback_endpoint) {
payload.callback_url = callback_endpoint;
}
payload = JSON.stringify(payload);
req = new XMLHttpRequest();
req.open('POST', "" + ENDPOINT_BASE + service_endpoint, false);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.onreadystatechange = function() {
var res;
if (req.readyState !== 4) {
return false;
}
res = JSON.parse(req.responseText);
if (res == null) {
res = {status: 'error', name: 'GenericError', message: 'An error occurred.'};
}
if (onresult) {
return onresult(res);
} else {
return false;
}
};
return req.send(payload);
};
SandCage.loadScript = function(url) {
/*
global: XMLHttpRequest
*/
var req;
req = new XMLHttpRequest();
req.open('GET', url, false);
req.onreadystatechange = function() {
var scriptNode;
if (req.readyState === 4) {
if (req.status === 200) {
scriptNode = document.createElement('script');
scriptNode.setAttribute('type', 'text/javascript');
scriptNode.setAttribute('charset', 'UTF-8');
scriptNode.setAttribute('src', url);
document.head.appendChild(scriptNode);
}
}
};
req.send(null);
};
/*
The "get-info" service
@param {Object} params the hash of the parameters to pass to the request
@param {Function} onresult an optional callback to execute when the API call is made
*/
SandCage.getInfo = function(params, onresult) {
if (onresult == null) {
return false;
}
return this.call('get-info', params, '', onresult);
};
/*
The "list-files" service
@param {Object} params the hash of the parameters to pass to the request
@param {Function} onresult an optional callback to execute when the API call is made
*/
SandCage.listFiles = function(params, onresult) {
if (onresult == null) {
return false;
}
return this.call('list-files', params, '', onresult);
};
/*
The "schedule-tasks" service
@param {Object} params the hash of the parameters to pass to the request
@param {String} callback_endpoint an optional callback endpoint, to which a request will be sent whenever there is an update for any of the tasks included in this request. See https://www.sandcage.com/docs/0.2/schedule_tasks#callbacks for an example
@param {Function} onresult an optional callback to execute when the API call is made
*/
SandCage.scheduleFiles = function(params, callback_endpoint, onresult) {
return this.call('schedule-tasks', params, callback_endpoint, onresult);
};
/*
The "destroy-files" service
@param {Object} params the hash of the parameters to pass to the request
@param {String} callback_endpoint an optional callback endpoint, to which a request will be sent whenever there is an update for any of the tasks included in this request. See https://www.sandcage.com/docs/0.2/destroy_files#callbacks for an example
@param {Function} onresult an optional callback to execute when the API call is made
*/
SandCage.destroyFiles = function(params, callback_endpoint, onresult) {
return this.call('destroy-files', params, callback_endpoint, onresult);
};
return SandCage;
})();