-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
179 lines (145 loc) · 4.18 KB
/
index.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"use strict";
var util = require("util")
, Q = require("q")
, request = require("request")
;
/**
* Builds a Url from the path components
* @param parts The parts of the URL, either an array, or a number of arguments
* @returns {string} The URL composed of the path parts
* @private
*/
function _buildUrl(parts) {
var pathParts = [];
function addArg(arg) {
var value = arg;
if (value) {
if (value.charAt(0) === "/") {
value = value.substr(1);
}
if (value.charAt(value.length - 1) === "/") {
value = value.substr(0, value.length - 1);
}
pathParts.push(value);
}
}
for (var i = 0; i < arguments.length; i++) {
if (util.isArray(arguments[i])) {
arguments[i].forEach(addArg);
} else {
addArg(arguments[i]);
}
}
return pathParts.join("/");
}
/**
* Creates a promise that will perform a request using the provided options.
* @param requestOptions The options detailing the request.
* @returns {Q.promise} That will resolve to the an object with statusCode and body content data if successful, or will
* reject upon error.
*
* @private
*/
function _request(requestOptions) {
var deferred = Q.defer();
if (requestOptions.debug) {
console.log(JSON.stringify(_generateDisplayParameters(requestOptions), null, 2));
}
request(requestOptions, function (error, response, body) {
var result = {};
if (error) {
deferred.reject(error);
} else {
result.statusCode = response.statusCode;
if (!requestOptions.json
&& response.headers
&& response.headers["content-type"]
&& response.headers["content-type"].indexOf("application/json") >= 0) {
result.data = JSON.parse(body);
} else {
result.data = body;
}
deferred.resolve(result);
}
});
return deferred.promise;
}
function _get(options) {
options.method = "GET";
return _request(options);
}
function _delete(options) {
options.method = "DELETE";
return _request(options);
}
function _put(options, content) {
return _putOrPost("PUT", options, content);
}
function _post(options, content) {
return _putOrPost("POST", options, content);
}
function _putOrPost(action, options, content) {
options.method = action;
if (content) {
if (content.body) {
options.body = content.body;
}
if (content.type) {
options.headers["Content-Type"] = content.type
}
}
return _request(options);
}
function _generateAuth(username, password) {
var result = null;
if (username && password) {
result = {
username: username,
password: password
};
}
return result;
}
function _requireStatusCode(expectedStatusCode, failureMessage) {
return function (result) {
var message
, error
;
if (result.statusCode === expectedStatusCode) {
return result.data;
} else {
message = "Unexpected statusCode from server, required %s but got %s; ";
if (result.data) {
message += JSON.stringify(result.data);
}
if (failureMessage) {
message = failureMessage + "\n" + message;
}
error = new Error(util.format(message, expectedStatusCode, result.statusCode));
error.statusCode = result.statusCode;
error.data = result.data;
throw error;
}
}
}
function _generateDisplayParameters(params) {
var displayParams = {};
Object.keys(params).forEach(function(key) {
var value = params[key];
if (/password/.test(key)) {
value = "***** REDACTED *****";
}
displayParams[key] = value;
});
return displayParams;
}
module.exports = {
buildUrl: _buildUrl,
get: _get,
delete: _delete,
post: _post,
put: _put,
request: _request,
requireStatusCode: _requireStatusCode,
generateAuth: _generateAuth
};