Skip to content

Commit

Permalink
Merge pull request #18 from ahaurw01/getservice-errback-fix
Browse files Browse the repository at this point in the history
getService errback fix
  • Loading branch information
ajsouza committed Aug 20, 2015
2 parents 5e4281e + 91a8f7a commit e250d50
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 13 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ You can instance any of DFP's API Services; https://developers.google.com/double


```JavaScript
dfpUser.getService('LineItemService', function (lineItemService) {
dfpUser.getService('LineItemService', function (err, lineItemService) {
if (err) {
return console.error(err);
}

var statement = new DfpClass.Statement('WHERE id = 103207340');

Expand Down
28 changes: 24 additions & 4 deletions examples/createCampaign.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ var dfpConfig = require('./dfpCredentials');
var dfpUser = new Dfp.User(dfpConfig.networkCore, dfpConfig.applicationName);
dfpUser.setSettings(dfpConfig);

dfpUser.getService('OrderService', function (orderService) {
dfpUser.getService('OrderService', function (err, orderService) {
if (err) {
console.error(err);
return false;
}

var args = { orders: [{
name: 'Full Campaign #67',
notes: 'It cannot be this simple!',
Expand All @@ -21,7 +26,12 @@ dfpUser.getService('OrderService', function (orderService) {
}
console.log(myOrder);

dfpUser.getService('LineItemService', function (lineItemService) {
dfpUser.getService('LineItemService', function (err, lineItemService) {
if (err) {
console.error(err);
return false;
}

var li = { lineItems: [{
orderId: parseFloat(myOrder.rval[0].id),
name: 'Lineitem descriptive name #10',
Expand Down Expand Up @@ -51,7 +61,12 @@ dfpUser.getService('OrderService', function (orderService) {
}
console.log(myLineItem);

dfpUser.getService('CreativeService', function (creativeService) {
dfpUser.getService('CreativeService', function (err, creativeService) {
if (err) {
console.error(err);
return false;
}

var cr = { creatives: [{
attributes: { 'xsi:type': 'ImageCreative' }, // Read Creative.Type - https://developers.google.com/doubleclick-publishers/docs/reference/v201403/CreativeService.BaseImageCreative
advertiserId: '10209990', // Must correspond to an advertiserId in your DFP instance
Expand All @@ -75,7 +90,12 @@ dfpUser.getService('OrderService', function (orderService) {
}
console.log(myCreative);

dfpUser.getService('LineItemCreativeAssociationService', function (licaService) {
dfpUser.getService('LineItemCreativeAssociationService', function (err, licaService) {
if (err) {
console.error(err);
return false;
}

var lica = { lineItemCreativeAssociations: [{
lineItemId: myLineItem.rval[0].id,
creativeId: myCreative.rval[0].id
Expand Down
8 changes: 6 additions & 2 deletions examples/createOrders.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ var dfpConfig = require('/dfpCredentials');
var dfpUser = new Dfp.User(dfpConfig.networkCore, dfpConfig.applicationName);
dfpUser.setSettings(dfpConfig);

dfpUser.getService('OrderService', function (orderService) {
var args = {
dfpUser.getService('OrderService', function (err, orderService) {
if (err) {
return console.error(err);
}

var args = {
orders: [
{
name: 'Multiple NodeJS Order Creation #1',
Expand Down
5 changes: 4 additions & 1 deletion examples/listLineitems.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ var dfpConfig = require('./dfpCredentials');
var dfpUser = new Dfp.User(dfpConfig.networkCore, dfpConfig.applicationName);
dfpUser.setSettings(dfpConfig);

dfpUser.getService('LineItemService', function (lineItemService) {
dfpUser.getService('LineItemService', function (err, lineItemService) {
if (err) {
return console.error(err);
}

var query = new Dfp.Statement('LIMIT 10');

Expand Down
5 changes: 4 additions & 1 deletion examples/runReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ var dfpConfig = require('./dfpCredentials');
var dfpUser = new Dfp.User(dfpConfig.networkCore, dfpConfig.applicationName);
dfpUser.setSettings(dfpConfig);

dfpUser.getService('ReportService', function (reportService) {
dfpUser.getService('ReportService', function (err, reportService) {
if (err) {
return console.error(err);
}

var results = null;
var args = {
Expand Down
23 changes: 19 additions & 4 deletions lib/DfpUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,30 @@ DfpUser.prototype.getService = function (service, callback, version) {
}
};

// If the callback accepts two arguments, make note that we should pass errors
// to the callback.
//
var callbackSupportsError = callback.length === 2;

this.getTokens(function (err, tokens) {

if (err) {
console.log('getTokens Error ' + err);
throw new Error('getTokens Error');
var error = new Error('getTokens Error');
if (callbackSupportsError) {
return callback(error);
}
throw error;
}

soap.createClient(soap_wsdl, options, function (err, client) {
if (err) {
console.log('Create Client Error ' + err);
throw new Error('Unable to get token');
var error = new Error('Unable to get token');
if (callbackSupportsError) {
return callback(error);
}
throw error;
}

client.addSoapHeader(dfpUser.getSOAPHeader(), '', '', '');
Expand All @@ -88,8 +101,10 @@ DfpUser.prototype.getService = function (service, callback, version) {
}
}

return callback(serviceInstance);

if (callbackSupportsError) {
return callback(null, serviceInstance);
}
callback(serviceInstance);
});
});

Expand Down

0 comments on commit e250d50

Please sign in to comment.