-
Notifications
You must be signed in to change notification settings - Fork 14
/
request.js
123 lines (110 loc) · 3.74 KB
/
request.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
/*jslint node: true */
var _ = require('lodash');
var FormData = require('form-data');
var https = require('https');
var logger = require('loge');
var url = require('url');
var requestOptions = function(opts) {
// method is the Flickr API method name
if (opts.method == 'upload') {
// http://up.flickr.com/services/upload/
return {
method: 'POST',
protocol: 'https',
hostname: 'up.flickr.com',
pathname: '/services/upload/',
query: _.omit(opts, 'method'),
};
}
else if (opts.method == 'replace') {
return {
method: 'POST',
protocol: 'https',
hostname: 'up.flickr.com',
pathname: '/services/replace/',
query: _.omit(opts, 'method'),
};
}
else {
return {
method: 'GET',
protocol: 'https',
hostname: 'api.flickr.com',
pathname: '/services/rest/',
query: opts,
};
}
};
var send = function(opts, oauth_client, oauth_token, oauth_token_secret, callback) {
/**
`opts` is a object of parameters, generally with strings values,
or perhaps a readable stream. There are two required keys:
`opts.method` is the simple name of the Flickr API call
`opts.api_key` is the oauth consumer key
If `opts.photo` is set and `opts.method` is "upload" or "replace",
`opts.photo` is assumed to be a readable stream, will be detached for
signing purposes, and then reattached to the POST request.
oauth_client: oauth.OAuth, client object (primed with consumer (app) key and secret, and relevant urls)
oauth_token: String, user's access token
oauth_token_secret: String, user's secret token
callback: function(Error | null, Object)
*/
var photo = opts.photo;
delete opts.photo;
// method and api_key come from opts, and they go into the querystring
// representation of the url, which gets signed via OAuth
var request_options = requestOptions(opts);
var urlStr = url.format(request_options);
// logger.error('urlStr::' + urlStr, oauth_token, oauth_token_secret, request_options.method);
var signed_urlStr = oauth_client.signUrl(urlStr, oauth_token, oauth_token_secret, request_options.method);
var signed_request_options = url.parse(signed_urlStr, true);
// logger.error('signed_request_options', signed_request_options);
if (request_options.method == 'POST') {
var form = new FormData();
_.each(signed_request_options.query, function(value, key) {
form.append(key, value);
});
if (photo) {
form.append('photo', photo);
}
form.getLength(function(err, length) {
if (err) return callback(err);
var post_request_options = {
hostname: request_options.hostname,
method: request_options.method,
path: request_options.pathname,
headers: {
'content-length': length,
'content-type': 'multipart/form-data; boundary=' + form.getBoundary(),
},
};
var post_req = https.request(post_request_options);
// logger.debug('POST request_opts: %j', post_request_options, form);
form.pipe(post_req);
callback(null, post_req);
});
}
else {
// logger.debug('GET request_opts: %j', request_options);
var get_req = https.request(signed_request_options);
get_req.end();
callback(null, get_req);
}
};
module.exports = function(opts, oauth_client, oauth_token, oauth_token_secret, callback) {
send(opts, oauth_client, oauth_token, oauth_token_secret, function(err, req) {
if (err) return callback(err);
req.on('response', function(res) {
callback(null, res);
})
.on('error', function(err) {
req.destroy();
callback(err);
})
.on('timeout', function() {
req.destroy();
callback(new Error('Timeout error'));
})
.setTimeout(30000);
});
};