Commit 00a6931c by tdgiang

Add header-aware GET/PATCH helpers with status code passthrough to ApiRequest

parent cb12e735
...@@ -256,12 +256,63 @@ exports.postOtherUrlWithHeader = function (apiName, param, headers, callback) { ...@@ -256,12 +256,63 @@ exports.postOtherUrlWithHeader = function (apiName, param, headers, callback) {
} }
// request ato API Host // request ato API Host
request(options, function (err, httpResponse, body) { request(options, function (err, httpResponse, body) {
var statusCode = httpResponse ? httpResponse.statusCode : undefined;
if (err) { if (err) {
callback(err, body); callback(err, body, statusCode);
} else if (httpResponse.statusCode >= 200 && httpResponse.statusCode < 300) { } else if (statusCode >= 200 && statusCode < 300) {
callback(err, body); callback(err, body, statusCode);
} else { } else {
callback(body); callback(body, undefined, statusCode);
}
});
};
exports.getOtherUrlWithHeader = function (apiName, param, headers, callback) {
if (typeof (param) === 'function') {
callback = param;
param = {};
}
var url = apiName;
for (var k in param) {
url += k + '=' + param[k] + '&';
}
var options = {
method: 'GET',
uri: url,
headers: headers,
json: true
};
request(options, function (err, httpResponse, body) {
var statusCode = httpResponse ? httpResponse.statusCode : undefined;
if (err) {
callback(err, body, statusCode);
} else if (statusCode >= 200 && statusCode < 300) {
callback(err, body, statusCode);
} else {
callback(body, undefined, statusCode);
}
});
};
exports.patchOtherUrlWithHeader = function (apiName, param, headers, callback) {
if (typeof (param) === 'function') {
callback = param;
param = {};
}
var options = {
method: 'PATCH',
uri: apiName,
headers: headers,
json: param
};
request(options, function (err, httpResponse, body) {
var statusCode = httpResponse ? httpResponse.statusCode : undefined;
if (err) {
callback(err, body, statusCode);
} else if (statusCode >= 200 && statusCode < 300) {
callback(err, body, statusCode);
} else {
callback(body, undefined, statusCode);
} }
}); });
}; };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment