forked from sandcage/sandcage-api-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsandcage.js
186 lines (165 loc) · 5.29 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
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
var SandCage;
SandCage = (function() {
var API_VERSION, ENDPOINT_BASE, loadScript;
API_VERSION = "0.2";
ENDPOINT_BASE = "https://api.sandcage.com/" + API_VERSION + "/";
function SandCage(apikey) {
var self;
this.apikey = apikey;
if (!this.apikey) {
SandCage.onerror({
status: 'error',
name: 'MissingKey',
message: 'Provide your SandCage API Key.'
});
return false;
}
self = this;
if (typeof JSON === "undefined" || JSON === null) {
loadScript('json2.min.js');
}
return this;
}
SandCage.call = function(service_endpoint, params, callback_endpoint, onresult) {
var key, payload, req;
if (callback_endpoint == null) {
callback_endpoint = '';
}
payload = {
key: self.apikey
};
if (callback_endpoint !== '') {
payload.callback_url = callback_endpoint;
}
for (key in params) {
payload[key] = params[key];
}
payload = JSON.stringify(payload);
req = new XMLHttpRequest();
req.open('POST', "" + ENDPOINT_BASE + service_endpoint);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.onreadystatechange = function() {
var e, res;
if (req.readyState !== 4) {
return;
}
try {
res = JSON.parse(req.responseText);
} catch (_error) {
e = _error;
res = {
status: 'error',
name: 'GenericError',
message: e
};
}
if (res == null) {
res = {
status: 'error',
name: 'GenericError',
message: 'An error occured.'
};
}
if (req.status !== 200) {
return SandCage.onerror(res);
} else {
if (onresult) {
return onresult(res);
}
}
};
return req.send(payload);
};
SandCage.onerror = function(err) {
return console.log('ERROR: ', {
name: err.name,
message: err.message,
error: err
});
};
loadScript = function(url) {
var ajax;
ajax = new XMLHttpRequest;
ajax.open('GET', url, false);
ajax.onreadystatechange = function() {
var script, scriptNode;
script = ajax.response || ajax.responseText;
if (ajax.readyState === 4) {
switch (ajax.status) {
case 200:
scriptNode = document.createElement('script');
scriptNode.setAttribute('type', 'text/javascript');
scriptNode.setAttribute('charset', 'UTF-8');
scriptNode.setAttribute('src', url);
document.head.appendChild(scriptNode);
console.log('library loaded: ', url);
break;
default:
console.log('ERROR: library not loaded: ', url);
}
}
};
ajax.send(null);
};
/*
The "get-info" service
@param {Object} params the hash of the parameters to pass to the request
@option params {String} name the immutable name of an existing template
*/
SandCage.getInfo = function(params, onsuccess) {
if ((params == null) || params === {}) {
return false;
}
if (onsuccess == null) {
return false;
}
return this.call('get-info', params, '', onsuccess);
};
/*
The "list-files" service
@param {Object} params the hash of the parameters to pass to the request
@option params {String} name the immutable name of an existing template
*/
SandCage.listFiles = function(params, onsuccess) {
if ((params == null) || params === {}) {
return false;
}
if (onsuccess == null) {
return false;
}
return this.call('list-files', params, '', onsuccess);
};
/*
The "schedule-tasks" service
@param {Object} params the hash of the parameters to pass to the request
@option params {String} name the immutable name of an existing template
@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} onsuccess an optional callback to execute when the API call is successfully made
*/
SandCage.scheduleFiles = function(params, callback_endpoint, onsuccess) {
if (callback_endpoint == null) {
callback_endpoint = '';
}
if ((params == null) || params === {}) {
return false;
}
return this.call('schedule-tasks', params, callback_endpoint, onsuccess);
};
/*
The "destroy-files" service
@param {Object} params the hash of the parameters to pass to the request
@option params {String} name the immutable name of an existing template
@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} onsuccess an optional callback to execute when the API call is successfully made
*/
SandCage.destroyFiles = function(params, callback_endpoint, onsuccess) {
if (callback_endpoint == null) {
callback_endpoint = '';
}
if ((params == null) || params === {}) {
return false;
}
return this.call('destroy-files', params, callback_endpoint, onsuccess);
};
return SandCage;
})();