Skip to content

Commit

Permalink
Add response formtting and retrieveOne
Browse files Browse the repository at this point in the history
  • Loading branch information
zarathustra323 committed Oct 15, 2018
1 parent 3d0a57f commit 002f8ad
Showing 1 changed file with 70 additions and 4 deletions.
74 changes: 70 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ const soap = require('soap');
const MarketingCloudAuth = require('marketing-cloud-auth');
const applyAuthHeader = require('./utils/apply-auth-header');
const ResponseError = require('./objects/response-error');
const defaultProps = require('./default-props');

const { isArray } = Array;

class MarketingCloudSOAP {
/**
Expand All @@ -26,20 +29,62 @@ class MarketingCloudSOAP {
this.soapOptions = soapOptions;
}

retrieveByCustomerKey(type, key, props) {
const filter = {
attributes: { 'xsi:type': 'SimpleFilterPart' },
Property: 'CustomerKey',
SimpleOperator: 'equals',
Value: key,
};
return this.retrieveOne(type, filter, props);
}

async retrieveOne(type, filter, props, options) {
const response = await this.retrieve(type, props, {
Filter: filter,
...options,
});
const result = MarketingCloudSOAP.formatResponse(response, true);
return result;
}

/**
*
* @param {string} type
* @param {string[]} [props]
*/
async retrieve(type, props = ['CustomerKey']) {
async retrieve(type, props, options) {
const client = await this.client();
const [result, rawResponse, rawRequest] = await client.RetrieveAsync({
const [result, rawResponse, soapHeader, rawRequest] = await client.RetrieveAsync({
RetrieveRequest: {
ObjectType: type,
Properties: props,
Properties: MarketingCloudSOAP.propsFor(type, props),
...options,
},
});
return MarketingCloudSOAP.handleResponse({ result, rawResponse, rawRequest });
return MarketingCloudSOAP.handleResponse({
result,
rawResponse,
rawRequest,
soapHeader,
});
}

async create(type, payload, options) {
const client = await this.client();
const [result, rawResponse, soapHeader, rawRequest] = await client.CreateAsync({
Objects: [{
attributes: { 'xsi:type': type },
...payload,
}],
Options: options || undefined,
});
return MarketingCloudSOAP.handleResponse({
result,
rawResponse,
rawRequest,
soapHeader,
});
}

/**
Expand Down Expand Up @@ -73,6 +118,27 @@ class MarketingCloudSOAP {
}
}

/**
*
* @param {string} type
* @param {?array} props
*/
static propsFor(type, props) {
if (isArray(props) && props.length) return props;
return defaultProps[type] || ['ObjectId'];
}

/**
*
* @param {object} res
* @param {boolean} [asOne=false]
*/
static formatResponse(res, asOne = false) {
const results = (res && isArray(res.Results)) ? res.Results.slice() : [];
if (asOne) return results.shift() || null;
return results;
}

/**
* @private
* @param {object} params
Expand Down

0 comments on commit 002f8ad

Please sign in to comment.