-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathalgorithmia.js
More file actions
197 lines (179 loc) · 5.78 KB
/
Copy pathalgorithmia.js
File metadata and controls
197 lines (179 loc) · 5.78 KB
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
(function() {
var Algorithm, Client, Promise, algoPattern, getDefaultApiAddress;
window.Algorithmia = window.Algorithmia || {};
if (Algorithmia.query) {
console.error("Warning: algorithmia.js loaded twice");
}
if (Algorithmia.client) {
console.error("Warning: algorithmia.js loaded twice");
}
if (Algorithmia.apiAddress) {
console.log("Using alternate API server: " + Algorithmia.apiAddress);
}
getDefaultApiAddress = function() {
if (Algorithmia.apiAddress !== void 0) {
return Algorithmia.apiAddress;
} else {
return "https://api.algorithmia.com/v1/web/algo";
}
};
algoPattern = /^(?:algo:\/\/|\/|)(\w+\/.+)$/;
Algorithmia.query = function(algo_uri, api_key, input, cb) {
return Algorithmia.client(api_key).algo(algo_uri).pipe(input, function(result) {
if (result.error) {
return cb(result.error.message || result.error);
} else {
return cb(void 0, result.result);
}
});
};
Algorithmia.client = function(api_key, api_address) {
api_key = api_key || Algorithmia.apiKey;
api_address = api_address || getDefaultApiAddress();
return new Client(api_key, api_address);
};
Client = function(api_key, api_address) {
this.api_key = api_key;
this.api_address = api_address;
this.algo = function(algo_uri) {
if (algo_uri && typeof algo_uri === "string") {
return new Algorithm(this, algo_uri);
} else {
console.error("Invalid algorithm url: " + algo_uri);
return null;
}
};
};
Algorithmia.algo = function(algo_uri) {
return Algorithmia.client().algo(algo_uri);
};
Algorithm = function(client, algo_uri) {
if (!(typeof algo_uri === "string" && algo_uri.match(algoPattern))) {
throw "Invalid Algorithm URI (expected /owner/algo)";
}
this.client = client;
this.algo_uri = algo_uri.match(algoPattern)[1];
this.pipe = function(input, cb) {
var promise;
Algorithmia.startTask();
promise = new Promise((function(_this) {
return function(resolve) {
var endpoint_url, xhr;
xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
var error, responseJson;
if (xhr.readyState === 4) {
Algorithmia.finishTask();
if (xhr.status === 0) {
if (xhr.responseText) {
resolve({
error: "API connection error: " + xhr.responseText
});
} else {
resolve({
error: "API connection error"
});
}
} else if (xhr.status === 502) {
resolve({
error: "API error, bad gateway"
});
} else if (xhr.status === 503) {
resolve({
error: "API error, service unavailable"
});
} else if (xhr.status === 504) {
resolve({
error: "API error, server timeout"
});
} else {
try {
responseJson = JSON.parse(xhr.responseText);
resolve(responseJson);
} catch (_error) {
error = _error;
resolve({
error: "API error (status " + xhr.status + "): " + error
});
}
}
}
};
endpoint_url = client.api_address + "/" + _this.algo_uri;
xhr.open("POST", endpoint_url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Accept", "application/json, text/javascript");
if (client.api_key) {
xhr.setRequestHeader("Authorization", "Simple " + client.api_key);
}
return xhr.send(JSON.stringify(input));
};
})(this));
if (cb) {
promise.then(cb);
}
return promise;
};
};
Promise = function(runner) {
var resolve;
this.isComplete = false;
this.listeners = [];
this.result = void 0;
resolve = (function(_this) {
return function(result) {
var cb, _i, _len, _ref;
_this.result = result;
_this.isComplete = true;
_ref = _this.listeners;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
cb = _ref[_i];
try {
cb(result);
} catch (_error) {}
}
};
})(this);
this.then = (function(_this) {
return function(cb) {
if (_this.isComplete) {
try {
return cb(_this.result);
} catch (_error) {}
} else {
return _this.listeners.push(cb);
}
};
})(this);
runner(resolve);
};
Algorithmia.tasksInProgress = 0;
Algorithmia.startTask = function() {
var spinner, _i, _len, _ref;
if (Algorithmia.tasksInProgress === 0) {
_ref = document.getElementsByClassName("algo-spinner");
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
spinner = _ref[_i];
spinner.style.visibility = "visible";
}
}
Algorithmia.tasksInProgress++;
};
Algorithmia.finishTask = function() {
var spinner, _i, _len, _ref;
Algorithmia.tasksInProgress--;
if (Algorithmia.tasksInProgress === 0) {
_ref = document.getElementsByClassName("algo-spinner");
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
spinner = _ref[_i];
spinner.style.visibility = "hidden";
}
} else if (Algorithmia.tasksInProgress < 0) {
console.error("Algorithmia task error (unknown task finished)");
}
};
if (Algorithmia.onload && typeof Algorithmia.onload === 'function') {
Algorithmia.onload();
}
}).call(this);
//# sourceMappingURL=algorithmia-0.2.0.js.map