forked from feedhenry/fh-js-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_cloud.js
58 lines (53 loc) · 1.51 KB
/
api_cloud.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
var logger =require("./logger");
var cloud = require("./waitForCloud");
var fhparams = require("./fhparams");
var ajax = require("./ajax");
var handleError = require("./handleError");
var appProps = require("./appProps");
var _ = require('underscore');
function doCloudCall(opts, success, fail){
var cloud_host = cloud.getCloudHost();
var url = cloud_host.getCloudUrl(opts.path);
var params = opts.data || {};
params = fhparams.addFHParams(params);
var type = opts.method || "POST";
var data;
if (["POST", "PUT", "PATCH", "DELETE"].indexOf(type.toUpperCase()) !== -1) {
data = JSON.stringify(params);
} else {
data = params;
}
var headers = fhparams.getFHHeaders();
if (opts.headers) {
headers = _.extend(headers, opts.headers);
}
return ajax({
"url": url,
"type": type,
"dataType": opts.dataType || "json",
"data": data,
"contentType": opts.contentType || "application/json",
"timeout": opts.timeout || appProps.timeout,
"headers": headers,
"success": success,
"error": function(req, statusText, error){
return handleError(fail, req, statusText, error);
}
});
}
module.exports = function(opts, success, fail){
logger.debug("cloud is called");
if(!fail){
fail = function(msg, error){
logger.debug(msg + ":" + JSON.stringify(error));
};
}
cloud.ready(function(err, cloudHost){
logger.debug("Calling fhact now");
if(err){
return fail(err.message, err);
} else {
doCloudCall(opts, success, fail);
}
});
};