Skip to content

Commit 9e50ba7

Browse files
author
Nathan Lyle Black
authored
Merge pull request #15 from algorithmiaio/revert-ts-pr
Revert "Merge pull request #13 from algorithmiaio/DEV-86"
2 parents 7c8762f + 1b52875 commit 9e50ba7

62 files changed

Lines changed: 1801 additions & 11262 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
1-
node_modules/
1+
tmp/
2+
3+
# Logs
4+
logs
5+
*.log
6+
7+
# Runtime data
8+
pids
9+
*.pid
10+
*.seed
11+
12+
# Directory for instrumented libs generated by jscoverage/JSCover
13+
lib-cov
14+
15+
# Coverage directory used by tools like istanbul
16+
coverage
17+
18+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
19+
.grunt
20+
21+
# Compiled binary addons (http://nodejs.org/api/addons.html)
22+
build/Release
23+
24+
# Dependency directory
25+
# Commenting this out is preferred by some people, see
26+
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
27+
node_modules
28+
29+
# Users Environment Variables
30+
.lock-wscript

.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
contrib
2+
examples
3+
src
4+
gulpfile.js
5+
tmp

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Anthony Nowell
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

README.md

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
algorithmia.js
2+
==============
3+
4+
A nodejs library for calling algorithms on Algorithmia.com with partial support for the DataAPI
5+
6+
[![npm](https://img.shields.io/npm/v/algorithmia.svg?maxAge=2592000)]()
7+
8+
## Getting started
9+
10+
The official Algorithmia nodejs client is available on NPM.
11+
Install it for your project by adding `algorithmia` to your package.json:
12+
13+
```bash
14+
npm install --save algorithmia
15+
```
16+
17+
Then instantiate an Algorithmia client using your API key:
18+
19+
```javascript
20+
var algorithmia = require("algorithmia");
21+
22+
var client = algorithmia(process.env.ALGORITHMIA_API_KEY);
23+
```
24+
25+
Now you are ready to call algorithms.
26+
27+
## Calling algorithms
28+
29+
The following examples of calling algorithms are organized by type of input/output which vary between algorithms.
30+
31+
Note: a single algorithm may have different input and output types, or accept multiple types of input, so consult the algorithm's description for usage examples specific to that algorithm.
32+
33+
### Text input/output
34+
35+
Call an algorithm with text input by passing a string into the `pipe` method.
36+
The returned promise will be called with the response with the Algorithm completes (or when an error occurs).
37+
If the algorithm output is text, then the `get()` method on the response will return a string.
38+
39+
```javascript
40+
client.algo("algo://demo/Hello/0.1.1")
41+
.pipe("HAL 9000")
42+
.then(function(response) {
43+
console.log(response.get());
44+
});
45+
// -> Hello HAL 9000
46+
```
47+
48+
### JSON input/output
49+
50+
Call an algorithm with JSON input by passing in a native JavaScript type;
51+
most of the time this will be an `Object` or an `Array` (though `Boolean`, `Number`, and `Null` are possible).
52+
Similarly, if the algorithm response is JSON, the `get()` method will return the appropriate native JavaScript type.
53+
54+
```javascript
55+
client.algo("algo://WebPredict/ListAnagrams/0.1.0")
56+
.pipe(["transformer", "terraforms", "retransform"])
57+
.then(function(response) {
58+
console.log(response.get());
59+
// -> ["transformer","retransform"]
60+
});
61+
```
62+
63+
Alternatively, if you already have serialized JSON, you can call `pipeJson` with the raw JSON string.
64+
The following example makes the same API call as the previous example:
65+
66+
```javascript
67+
client.algo("algo://WebPredict/ListAnagrams/0.1.0")
68+
.pipeJson('["transformer", "terraforms", "retransform"]')
69+
```
70+
71+
### Binary input/output
72+
73+
Call an algorithm with binary input by passing a `Buffer` into the pipe method.
74+
Similarly, if the algorithm response is binary data, then the `get` method on the response will be a byte array.
75+
76+
```javascript
77+
var buffer = fs.readFileSync("/path/to/bender.jpg");
78+
client.algo("opencv/SmartThumbnail")
79+
.pipe(buffer)
80+
.then(function(response) {
81+
var buffer = response.get();
82+
// -> Buffer(...)
83+
});
84+
```
85+
86+
Note: while it is possible to use `response.result` for text or JSON responses, in the case of a binary resonse,
87+
the `result` field will be base64-encoded. The `get()` method is recommended
88+
because it will return the correct type in all cases.
89+
90+
### Error handling
91+
92+
If an error occurs when calling an algorithm, the response will contain an error field that you can check:
93+
94+
```javascript
95+
client.algo('util/whoopsWrongAlgo').pipe('Hello, world!')
96+
.then(function(response) {
97+
if(response.error) {
98+
console.log("Error: " + response.error.message);
99+
} {
100+
console.log(response.get());
101+
}
102+
});
103+
```
104+
105+
### Request options
106+
107+
The Algorithmia API exposes parameters to configure algorithm requests including support
108+
for changing the timeout of indicating that the API should include stdout in the response.
109+
Currently, the node.js client exposes these as query paremeters to the algorithm URI:
110+
111+
```javascript
112+
client.algo("algo://demo/Hello/0.1.1?timeout=10&stdout=true")
113+
.pipe("HAL 9000")
114+
```
115+
116+
Note: `stdout=true` is only supported if you have access to the algorithm source.
117+
118+
## Working with data
119+
120+
The Algorithmia client also provides a way to manage both Algorithmia hosted data and data from Dropbox or S3 accounts that you've connected to you Algorithmia account.
121+
122+
### Create directories
123+
124+
Create directories by instantiating a `Dir` object and calling `create()`:
125+
126+
```javascript
127+
var robots = client.dir("data://.my/robots");
128+
robots.create(function(response) {
129+
if(response.error) {
130+
return console.log("Failed to create dir: " + response.error.message);
131+
}
132+
console.log("Created directory: " + robots.data_path);
133+
});
134+
```
135+
136+
### Upload files to a directory
137+
138+
Upload files by calling the `putFile` method a `Dir` object or `put` on a `File` object:
139+
140+
```javascript
141+
var robots = client.dir("data://.my/robots");
142+
robots.putFile("/path/to/Optimus_Prime.png", function(response) {
143+
if(response.error) {
144+
return console.log("Failed to upload file: " + response.error.message);
145+
}
146+
console.log("File uploaded.");
147+
);
148+
```
149+
150+
You can also write to a `File` using the `put` method with either a `string` or `Buffer` as input:
151+
152+
```javascript
153+
var prime = client.file("data://.my/robots/Optimus_Prime.txt");
154+
prime.put("Leader of the Autobots", function(response) {
155+
if(response.error) {
156+
return console.log("Failed to upload file: " + response.error.message);
157+
}
158+
console.log("File uploaded.");
159+
);
160+
```
161+
162+
### Download content from files
163+
164+
Download files by calling `get` on a `File` object:
165+
166+
```javascript
167+
var robots = client.dir("data://.my/robots");
168+
169+
// Get a text file's contents as a string
170+
robots.file("T-800.txt").get(function(err, data) {
171+
console.log(data);
172+
});
173+
174+
/// Get a binary file's contents as a Buffer
175+
robots.file("T-800.jpg").get(function(err, data) {
176+
console.log("Received " + data.length + " bytes.");
177+
fs.writeFileSync("/tmp/T-800.jpg", data);
178+
});
179+
```
180+
181+
### Delete files and directories
182+
183+
Delete files by calling `delete` on their respective `File` or `Dir` object.
184+
When deleting directories, you may optionally specify a `force` argument
185+
that indicates whether or not a directory should be deleted if it contains files or other directories (default = `false`).
186+
187+
```javascript
188+
var c3po = client.file("data://.my/robots/C-3PO.txt");
189+
c3po.delete(function(response) {
190+
if(response.error) {
191+
return console.log("Failed to delete file: " + response.error.message);
192+
}
193+
console.log("Deleted file: " + c3po.data_path);
194+
});
195+
196+
// Force delete a directory
197+
client.dir("data://.my/robots")
198+
.delete(true, function(response) {
199+
/* ommitting callback implementation */
200+
});
201+
```
202+
203+
### List directory contents
204+
205+
Iterate over the contents of a directory using the iterated returned by calling `forEachDir` or `forEachFile` on a `Dir` object:
206+
207+
```javascript
208+
// List top level directories
209+
client.dir("data://.my").forEachDir(function(err, dir) {
210+
if(err) {
211+
return console.log("Error: " + JSON.stringify(err));
212+
}
213+
console.log(dir.data_path);
214+
}).then(function() {
215+
console.log("Finished listing directory");
216+
});
217+
218+
219+
// List files in the Public folder of your connected Dropbox account
220+
client.dir("dropbox://Public").forEachFile(function(err, file) {
221+
if(err) {
222+
return console.log("Error: " + JSON.stringify(err));
223+
}
224+
console.log(file.data_path);
225+
}).then(function() {
226+
console.log("Finished listing directory");
227+
});
228+
```
229+
230+
## Building the client
231+
232+
This project uses gulp to compile coffeescript.
233+
234+
```bash
235+
npm install
236+
npm install -g gulp-cli
237+
238+
gulp
239+
```
240+
241+
Note: Don't edit the .js in the `lib` directory; they will get overwritten on subsequent compiles.
242+
Instead, modify `.coffee` files in the `src` dir, and run `gulp`.
243+
244+
Testing (kinda) is currently achieved by running this bash loop and checking for error messages.
245+
246+
```
247+
for f in examples/*.js; do node $f; done
248+
```

babel.config.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

build/src/AlgorithmExecutable.d.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

build/src/AlgorithmExecutable.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

build/src/AlgorithmExecutable.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

build/src/Algorithmia.d.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

build/src/Algorithmia.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)