UNPKG

hybrisspm

Version:

Hybris Marketting API connector Library

148 lines (135 loc) 4.86 kB
/** * @author Delly Fofie <delly.fofie@sap.com> * */ var httpClient = require('request'); export class HybrisData { private baseUrl: string; private auth: any = { user: null, pass: null, sendImmediately: true }; private total: number = 10; private system: string; private order: string = "CampaignID"; private contentFormat: string = "json"; private inline: string = "allpages"; private uuid: string; private findOne: boolean = false; private sortOrder: string = 'desc'; constructor(username: string, password: string, baseUrl: string = "https://ccf-350.wdf.sap.corp", system: string = "/sap/opu/odata/SAP/API_MKT_CAMPAIGN_SRV/Campaigns") { this.auth.user = username; this.auth.pass = password; this.baseUrl = baseUrl; this.system = system; } setCredentials(username: string, password: string, baseUrl: string = "https://ccf-350.wdf.sap.corp", system: string = "/sap/opu/odata/SAP/API_MKT_CAMPAIGN_SRV/Campaigns") { this.auth.user = username; this.auth.pass = password; this.baseUrl = baseUrl; this.system = system; } get() { let complement: string = ''; let url: string; 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((resolve: any, reject: any) => { httpClient({ method: 'GET', url: url, auth: this.auth, json: true, strictSSL: false }, function (err: any, resp: any, body: any) { if (err) { reject(err); } else { resolve(body); } }); }); } find(uuid: string) { this.findOne = true; this.uuid = uuid; return this; } top(total: number) { this.total = total; return this; } orderBy(order: string) { this.order = order; return this; } inlineCount(inline: string) { this.inline = inline; return this; } format(contentFormat: string) { this.contentFormat = contentFormat; return this; } sort(sortOrder: string) { this.sortOrder = sortOrder; return this; } create(element: any) { // Create first check in which system we are if (this.system.indexOf("Campaigns") != -1) { let token: string; let url = this.baseUrl + this.system; let t = this; return httpClient({ method: 'HEAD', url: url, auth: this.auth, json: true, strictSSL: false, headers: { "x-csrf-token": "fetch" } }, function (err: any, resp: any, body: any) { if (err) { return new Promise((resolve: any, reject: any) => { reject(err); }); } else { var csrfToken = resp.headers['x-csrf-token'].trim(); url = t.baseUrl + t.system + "?$format=" + t.contentFormat; return new Promise((resolve: any, reject: any) => { httpClient({ method: 'POST', url: url, auth: t.auth, json: true, strictSSL: false, headers: { "x-csrf-token": csrfToken, "Content-Type": "application/json", "Accept": "application/json", }, body: element }, function (err: any, resp: any, body: any) { if (err) { reject(err); } else { resolve(body); } }); }); } }); } } }