-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathnode.js
73 lines (66 loc) · 2.36 KB
/
node.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
// Copyright 2014 Pedro P. Candel <[email protected]>. All rights reserved.
// This is an example of the same gist API requests made straight from NodeJS
// without using wf, so differences can be quickly stablished with
// the way to proceed to write workflow tasks
if (process.argv.length < 4) {
console.error('Github username and password/token required as arguments');
process.exit(1);
}
var $login = process.argv[2],
$password = process.argv[3];
var restify = require('restify'),
util = require('util');
var client = restify.createJsonClient({
url: 'https://api.github.com'
});
client.basicAuth($login, $password);
client.post('/gists', {
'description': 'a gist created using node-workflow',
'public': false,
'files': {
'example.json': {
'content': JSON.stringify({
foo: 'bar',
bar: 'baz'
})
}
}
}, function (err, req, res, obj) {
if (err) {
console.error(err.name + ': ' + err.body.message);
if (err.statusCode === 401) {
process.exit(1);
}
} else {
// res.statusCode === 201
// res.location === obj.url
// obj.url === client.url + req.path + '/' + obj.id
var gist = obj;
console.info('Gist object with id ' + gist.id + ' created');
var path = '/gists/' + gist.id + '/star';
client.get(path, function (err, req, res, obj) {
console.log(util.inspect(err, false, 8));
console.log(util.inspect(obj, false, 8));
if (err) {
if (err.statusCode === 404) {
client.put('/gists/' + gist.id + '/star', {},
function (err, req, res, obj) {
if (err) {
console.error(err.name + ': ' + err.body.message);
process.exit(1);
} else {
console.info('Gist is starred: ' + gist.html_url);
process.exit(0);
}
});
} else {
console.error(err.name + ': ' + err.body.message);
process.exit(1);
}
} else {
console.info('Gist is starred: ' + gist.html_url);
process.exit(0);
}
});
}
});