hybrisspm
Version:
Hybris Marketting API connector Library
146 lines (145 loc) • 5.29 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @author Delly Fofie <delly.fofie@sap.com>
*
*/
var httpClient = require('request');
var HybrisData = (function () {
function HybrisData(username, password, baseUrl, system) {
if (baseUrl === void 0) { baseUrl = "https://ccf-350.wdf.sap.corp"; }
if (system === void 0) { system = "/sap/opu/odata/SAP/API_MKT_CAMPAIGN_SRV/Campaigns"; }
this.auth = {
user: null,
pass: null,
sendImmediately: true
};
this.total = 10;
this.order = "CampaignID";
this.contentFormat = "json";
this.inline = "allpages";
this.findOne = false;
this.sortOrder = 'desc';
this.auth.user = username;
this.auth.pass = password;
this.baseUrl = baseUrl;
this.system = system;
}
HybrisData.prototype.setCredentials = function (username, password, baseUrl, system) {
if (baseUrl === void 0) { baseUrl = "https://ccf-350.wdf.sap.corp"; }
if (system === void 0) { system = "/sap/opu/odata/SAP/API_MKT_CAMPAIGN_SRV/Campaigns"; }
this.auth.user = username;
this.auth.pass = password;
this.baseUrl = baseUrl;
this.system = system;
};
HybrisData.prototype.get = function () {
var _this = this;
var complement = '';
var url;
if (this.findOne) {
url = this.baseUrl + this.system + "(guid'" + this.uuid + "')?$format=" + this.contentFormat;
}
else {
// means whe are looking for the top campaigns;
complement += "$top=" + this.total;
complement += "&$orderby=" + this.order + " " + this.sortOrder;
complement += "&$format=" + this.contentFormat;
complement += "&$inlinecount=" + this.inline;
url = this.baseUrl + this.system + "?" + complement;
}
return new Promise(function (resolve, reject) {
httpClient({
method: 'GET',
url: url,
auth: _this.auth,
json: true,
strictSSL: false
}, function (err, resp, body) {
if (err) {
reject(err);
}
else {
resolve(body);
}
});
});
};
HybrisData.prototype.find = function (uuid) {
this.findOne = true;
this.uuid = uuid;
return this;
};
HybrisData.prototype.top = function (total) {
this.total = total;
return this;
};
HybrisData.prototype.orderBy = function (order) {
this.order = order;
return this;
};
HybrisData.prototype.inlineCount = function (inline) {
this.inline = inline;
return this;
};
HybrisData.prototype.format = function (contentFormat) {
this.contentFormat = contentFormat;
return this;
};
HybrisData.prototype.sort = function (sortOrder) {
this.sortOrder = sortOrder;
return this;
};
HybrisData.prototype.create = function (element) {
// Create first check in which system we are
if (this.system.indexOf("Campaigns") != -1) {
var token = void 0;
var url_1 = this.baseUrl + this.system;
var t_1 = this;
return httpClient({
method: 'HEAD',
url: url_1,
auth: this.auth,
json: true,
strictSSL: false,
headers: {
"x-csrf-token": "fetch"
}
}, function (err, resp, body) {
if (err) {
return new Promise(function (resolve, reject) {
reject(err);
});
}
else {
var csrfToken = resp.headers['x-csrf-token'].trim();
url_1 = t_1.baseUrl + t_1.system + "?$format=" + t_1.contentFormat;
return new Promise(function (resolve, reject) {
httpClient({
method: 'POST',
url: url_1,
auth: t_1.auth,
json: true,
strictSSL: false,
headers: {
"x-csrf-token": csrfToken,
"Content-Type": "application/json",
"Accept": "application/json",
},
body: element
}, function (err, resp, body) {
if (err) {
reject(err);
}
else {
resolve(body);
}
});
});
}
});
}
};
return HybrisData;
}());
exports.HybrisData = HybrisData;