|
35 | 35 | } |
36 | 36 | */ |
37 | 37 |
|
| 38 | +var algorithmia = require("algorithmia"); |
38 | 39 | var AWS = require('aws-sdk'); |
39 | 40 | var apiKey, kmsEncryptedApiKey; |
40 | 41 |
|
@@ -115,240 +116,3 @@ var processEvent = function(event, context) { |
115 | 116 | } |
116 | 117 | }); |
117 | 118 | } |
118 | | - |
119 | | - |
120 | | -/* |
121 | | - * Algorithmia NodeJS SDK below |
122 | | - */ |
123 | | -var Algorithm, AlgorithmiaClient, AlgoResponse, Data, algorithmia, defaultApiAddress, http, https, packageJson, url; |
124 | | -https = require('https'); |
125 | | -http = require('http'); |
126 | | -url = require('url'); |
127 | | - |
128 | | -defaultApiAddress = 'https://api.algorithmia.com'; |
129 | | - |
130 | | -AlgorithmiaClient = (function() { |
131 | | - function AlgorithmiaClient(key, address) { |
132 | | - this.apiAddress = address || process.env.ALGORITHMIA_API || defaultApiAddress; |
133 | | - key = key || process.env.ALGORITHMIA_API_KEY; |
134 | | - if (key) { |
135 | | - if (key.indexOf('Simple ') === 0) { |
136 | | - this.apiKey = key; |
137 | | - } else { |
138 | | - this.apiKey = 'Simple ' + key; |
139 | | - } |
140 | | - } else { |
141 | | - this.apiKey = ''; |
142 | | - } |
143 | | - } |
144 | | - |
145 | | - AlgorithmiaClient.prototype.algo = function(path) { |
146 | | - return new Algorithm(this, path); |
147 | | - }; |
148 | | - |
149 | | - AlgorithmiaClient.prototype.file = function(path) { |
150 | | - return new Data(this, path); |
151 | | - }; |
152 | | - |
153 | | - AlgorithmiaClient.prototype.req = function(path, method, data, cheaders, callback) { |
154 | | - var dheader, httpRequest, key, options, protocol, val; |
155 | | - dheader = { |
156 | | - 'Content-Type': 'application/JSON', |
157 | | - 'Accept': 'application/JSON', |
158 | | - 'User-Agent': 'algorithmia-lambda/1.0.1 (NodeJS ' + process.version + ')' |
159 | | - }; |
160 | | - if (this.apiKey) { |
161 | | - dheader['Authorization'] = this.apiKey; |
162 | | - } |
163 | | - for (key in cheaders) { |
164 | | - val = cheaders[key]; |
165 | | - dheader[key] = val; |
166 | | - } |
167 | | - options = url.parse(this.apiAddress + path); |
168 | | - options.method = method; |
169 | | - options.headers = dheader; |
170 | | - protocol = options.protocol === 'https:' ? https : http; |
171 | | - httpRequest = protocol.request(options, function(res) { |
172 | | - var chunks; |
173 | | - res.setEncoding('utf8'); |
174 | | - chunks = []; |
175 | | - res.on('data', function(chunk) { |
176 | | - return chunks.push(chunk); |
177 | | - }); |
178 | | - res.on('end', function() { |
179 | | - var body, buff; |
180 | | - buff = chunks.join(''); |
181 | | - if (dheader['Accept'] === 'application/JSON') { |
182 | | - body = JSON.parse(buff); |
183 | | - } else { |
184 | | - body = buff; |
185 | | - } |
186 | | - if (callback) { |
187 | | - if (res.statusCode < 200 || res.statusCode >= 300) { |
188 | | - if (!body) { |
189 | | - body = {}; |
190 | | - } |
191 | | - if (!body.error) { |
192 | | - body.error = { |
193 | | - message: 'HTTP Response: ' + res.statusCode |
194 | | - }; |
195 | | - } |
196 | | - } |
197 | | - callback(body, res.statusCode); |
198 | | - } |
199 | | - }); |
200 | | - return res; |
201 | | - }); |
202 | | - httpRequest.write(data); |
203 | | - httpRequest.end(); |
204 | | - }; |
205 | | - |
206 | | - return AlgorithmiaClient; |
207 | | - |
208 | | -})(); |
209 | | - |
210 | | -algorithmia = function(key, address) { |
211 | | - return new AlgorithmiaClient(key, address); |
212 | | -}; |
213 | | - |
214 | | -algorithmia.client = function(key, address) { |
215 | | - return new AlgorithmiaClient(key, address); |
216 | | -}; |
217 | | - |
218 | | -algorithmia.algo = function(path) { |
219 | | - this.defaultClient = this.defaultClient || new AlgorithmiaClient(); |
220 | | - return this.defaultClient.algo(path); |
221 | | -}; |
222 | | - |
223 | | -algorithmia.file = function(path) { |
224 | | - this.defaultClient = this.defaultClient || new AlgorithmiaClient(); |
225 | | - return this.defaultClient.file(path); |
226 | | -}; |
227 | | - |
228 | | - |
229 | | -Algorithm = (function() { |
230 | | - function Algorithm(client, path) { |
231 | | - this.client = client; |
232 | | - this.algo_path = path; |
233 | | - this.promise = { |
234 | | - then: (function(_this) { |
235 | | - return function(callback) { |
236 | | - return _this.callback = callback; |
237 | | - }; |
238 | | - })(this) |
239 | | - }; |
240 | | - } |
241 | | - |
242 | | - Algorithm.prototype.pipe = function(input) { |
243 | | - var contentType, data; |
244 | | - data = input; |
245 | | - if (Buffer.isBuffer(input)) { |
246 | | - contentType = 'application/octet-stream'; |
247 | | - } else if (typeof input === 'string') { |
248 | | - contentType = 'text/plain'; |
249 | | - } else { |
250 | | - contentType = 'application/json'; |
251 | | - data = JSON.stringify(input); |
252 | | - } |
253 | | - this.req = this.client.req('/v1/algo/' + this.algo_path, 'POST', data, { |
254 | | - 'Content-Type': contentType |
255 | | - }, (function(_this) { |
256 | | - return function(response, status) { |
257 | | - return _this.callback(new AlgoResponse(response, status)); |
258 | | - }; |
259 | | - })(this)); |
260 | | - return this.promise; |
261 | | - }; |
262 | | - |
263 | | - Algorithm.prototype.pipeJson = function(input) { |
264 | | - if (typeof input !== 'string') { |
265 | | - throw "Cannot convert " + (typeof input) + " to string"; |
266 | | - } |
267 | | - this.req = this.client.req('/v1/algo/' + this.algo_path, 'POST', input, { |
268 | | - 'Content-Type': 'application/json' |
269 | | - }, (function(_this) { |
270 | | - return function(response, status) { |
271 | | - return _this.callback(new AlgoResponse(response, status)); |
272 | | - }; |
273 | | - })(this)); |
274 | | - return this.promise; |
275 | | - }; |
276 | | - |
277 | | - return Algorithm; |
278 | | - |
279 | | -})(); |
280 | | - |
281 | | -AlgoResponse = (function() { |
282 | | - function AlgoResponse(response, status) { |
283 | | - this.status = status; |
284 | | - this.result = response.result; |
285 | | - this.error = response.error; |
286 | | - this.metadata = response.metadata; |
287 | | - } |
288 | | - |
289 | | - AlgoResponse.prototype.get = function() { |
290 | | - if (this.error) { |
291 | | - throw "" + this.error.message; |
292 | | - } |
293 | | - switch (this.metadata.content_type) { |
294 | | - case "void": |
295 | | - return null; |
296 | | - case "text": |
297 | | - case "json": |
298 | | - return this.result; |
299 | | - case "binary": |
300 | | - return new Buffer(this.result, 'base64'); |
301 | | - default: |
302 | | - throw "Unknown result content_type: " + this.metadata.content_type + "."; |
303 | | - } |
304 | | - }; |
305 | | - |
306 | | - return AlgoResponse; |
307 | | - |
308 | | -})(); |
309 | | - |
310 | | - |
311 | | -Data = (function() { |
312 | | - function Data(client, path) { |
313 | | - this.client = client; |
314 | | - if (path.indexOf('data://') !== 0) { |
315 | | - throw 'Supplied path is invalid.'; |
316 | | - } |
317 | | - this.data_path = path.replace(/data\:\/\//, ''); |
318 | | - } |
319 | | - |
320 | | - Data.prototype.putString = function(content, callback) { |
321 | | - var headers; |
322 | | - headers = { |
323 | | - 'Content-Type': 'text/plain' |
324 | | - }; |
325 | | - return this.client.req('/v1/data/' + this.data_path, 'PUT', content, headers, callback); |
326 | | - }; |
327 | | - |
328 | | - Data.prototype.putJson = function(content, callback) { |
329 | | - var headers; |
330 | | - headers = { |
331 | | - 'Content-Type': 'application/JSON' |
332 | | - }; |
333 | | - return this.client.req('/v1/data/' + this.data_path, 'PUT', content, headers, callback); |
334 | | - }; |
335 | | - |
336 | | - Data.prototype.getString = function(callback) { |
337 | | - var headers; |
338 | | - headers = { |
339 | | - 'Accept': 'text/plain' |
340 | | - }; |
341 | | - return this.client.req('/v1/data/' + this.data_path, 'GET', '', headers, callback); |
342 | | - }; |
343 | | - |
344 | | - Data.prototype.getJson = function(callback) { |
345 | | - var headers; |
346 | | - headers = { |
347 | | - 'Accept': 'text/plain' |
348 | | - }; |
349 | | - return this.client.req('/v1/data/' + this.data_path, 'GET', '', headers, callback); |
350 | | - }; |
351 | | - |
352 | | - return Data; |
353 | | - |
354 | | -})(); |
0 commit comments