forked from tonyto/node-posterous
/
posterous.js
145 lines (113 loc) · 3.31 KB
/
posterous.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
var http = require('http');
var qs = require('querystring');
var api = require('./api');
var HOST = 'posterous.com';
var PATH = '/api/2/';
var Posterous = function(user, pass, api_token) {
this._auth = 'Basic ' + new Buffer(user + ':' + pass).toString('base64');
this._token = api_token;
};
Posterous.prototype.request = function(method, api, args, callback) {
args = args || {};
//Add the api token
if (!("api_token" in args)) {
args.api_token = this._token;
}
//Set up request options
var path = PATH + api;
var body = null;
var headers = {
Authorization: this._auth
};
if (method != 'GET') {
args['_method'] = method;
method = 'POST';
body = qs.stringify(args);
headers['Content-Length'] = body.length;
} else {
path = path + '?' + qs.stringify(args);
}
var reqOptions = {
host: HOST,
port: 80,
method: method,
path: path,
headers: headers
};
console.log('uri path: ' + reqOptions.path);
//Make request to Posterous API
var req = http.request(reqOptions, function apiRequest(res) {
//Gather response
var response = '';
res.on('data', function apiResponse(d) {
response += d.toString();
});
res.on('end', function endResponse(){
//Work-around, not all API responses are valid json >_>
//Example: Deleting a site gives a response of 'undefined:1'
try {
var r = JSON.parse(response);
} catch (err) {
var message = 'Error parsing JSON: ' + err + ' in\n' + response;
return callback(message, null);
}
callback(null, r);
});
});
if (body) {
req.write(body);
}
req.on('error', function reqError(e) {
return callback('Problem with request: ' + e);
});
req.end();
};
Posterous.prototype.makeRequest = function(method, args) {
console.log('in makeRequest trying');
if (args.length < 3) {
return console.error('[Posterous] You must pass arguments to the method.');
}
var apiName = args[0];
var callback = args[args.length - 1];
var options = args[args.length - 2];
//Check last argument is a callback function
if (typeof callback != 'function') {
return console.error('[Posterous] You must pass a callback function.');
}
//Check API method name
if (!(apiName in api)) {
return callback('This API method doesn\'t exist.');
}
var info = api[apiName];
var path = info.path;
//Check API method
if (info.methods.indexOf(method) == -1) {
return callback('This API method is not allowed.');
}
//Check number of arguments
if ('args' in info) {
if (args.length - 3 != info.args) {
return callback('You passed the wrong number of arguments to the method.');
}
//Create API path
for (var i = 1; i < info.args + 1; i++) {
path = path.replace('{' + (i - 1) + '}', args[i]);
}
}
//console.log(method + ' ' + path);
this.request(method, path, options, callback);
};
Posterous.prototype.get = function() {
console.log('about to get');
this.makeRequest('GET', arguments);
};
Posterous.prototype.post = function() {
this.makeRequest('POST', arguments);
};
Posterous.prototype.put = function() {
this.makeRequest('PUT', arguments);
};
Posterous.prototype.del = Posterous.prototype.delete = function() {
this.makeRequest('DELETE', arguments);
};
module.exports = Posterous;