Skip to content

Commit

Permalink
PLTFRS-7737 - Handle oauth v2 + cleanup + format with prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
sgaestel committed Oct 27, 2017
1 parent 2fcff11 commit 28aa2da
Show file tree
Hide file tree
Showing 15 changed files with 274 additions and 438 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ client.action.query("select Id, Name from Account where AccountNumber='XXXX'").t
});

```

By default, authentication is performed using cookie method: https://www.zuora.com/developer/api-reference/#section/Authentication/Other-Supported-Authentication-Schemes

You can use oauth v2 authentication instead: https://www.zuora.com/developer/api-reference/#section/Authentication/OAuth-v2.0

Usage
```javascript

var Zuora = require("../zuora.js");

var client = new Zuora({
url: "ZUORA_INSTANCE_URL",
oauthType: "oauth_v2"
client_id: "YOUR_CLIENT_ID",
client_secret: "YOUR_CLIENT_SECRET"
});

```

## License

MIT
Expand Down
38 changes: 10 additions & 28 deletions lib/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,36 @@ var _ = require('lodash');

module.exports = function(zuoraClient) {
function getAccount(accountId) {
return zuoraClient.authenticate().then(authCookie => {
return zuoraClient.authenticate().then(headers => {
var url = zuoraClient.serverUrl + '/object/account/' + accountId;
var query = {
headers: {
'Content-type': 'application/json',
cookie: authCookie
},
headers,
json: true
};
return got.get(url, query).then(res => {
return res.body;
});
return got.get(url, query).then(res => res.body);
});
}

function deleteAccount(accountId) {
return zuoraClient.authenticate().then(authCookie => {
return zuoraClient.authenticate().then(headers => {
var url = zuoraClient.serverUrl + '/object/account/' + accountId;
var query = {
headers: {
'Content-type': 'application/json',
cookie: authCookie
},
headers,
json: true
};
return got.delete(url, query).then(res => {
return res.body;
});
return got.delete(url, query).then(res => res.body);
});
}

function updateAccount(accountId, updatedContent) {
return zuoraClient.authenticate().then(authCookie => {
return zuoraClient.authenticate().then(headers => {
var url = zuoraClient.serverUrl + '/object/account/' + accountId;
var query = {
headers: {
'Content-type': 'application/json',
cookie: authCookie
},
headers,
body: updatedContent,
json: true
};
return got.put(url, query).then(res => {
return res.body;
});
return got.put(url, query).then(res => res.body);
});
}

Expand All @@ -66,10 +51,7 @@ module.exports = function(zuoraClient) {

return {
getFromAccountNumber: accountNumber => getAccountFromAccountNumber(accountNumber),
activate: accountId =>
updateAccount(accountId, {
Status: 'Active'
}),
activate: accountId => updateAccount(accountId, { Status: 'Active' }),
get: accountId => getAccount(accountId),
update: (accountId, updatedContent) => updateAccount(accountId, updatedContent),
delete: accountId => deleteAccount(accountId)
Expand Down
65 changes: 8 additions & 57 deletions lib/action.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
var BPromise = require('bluebird');
var got = require('got');
var _ = require('lodash');

module.exports = zuoraClient => {
return {
query: queryString => {
return zuoraClient.authenticate().then(authCookie => {
return zuoraClient.authenticate().then(headers => {
var url = zuoraClient.serverUrl + '/action/query';
var data = {
queryString: queryString
};
var query = {
body: data,
headers: {
'Content-type': 'application/json',
cookie: authCookie
},
headers,
json: true
};
return got
.post(url, query)
.then(res => {
return res.body;
})
.then(res => res.body)
.catch(error => {
if (error.statusCode === 401) {
console.log(error.response.body);
Expand All @@ -34,63 +28,20 @@ module.exports = zuoraClient => {
});
},
queryMore: queryLocator => {
return zuoraClient.authenticate().then(authCookie => {
return zuoraClient.authenticate().then(headers => {
var url = zuoraClient.serverUrl + '/action/queryMore';
var query = {
body: {
queryLocator: queryLocator
},
headers: {
'Content-type': 'application/json',
cookie: authCookie
},
body: { queryLocator },
headers,
json: true
};
return got
.post(url, query)
.then(res => {
return res.body;
})
.then(res => res.body)
.catch(error => {
if (error.statusCode === 401) {
console.log(error.response.body);
return this(queryString);
} else {
throw error;
}
});
});
},
fullQuery: queryString => {
return zuoraClient.authenticate().then(authCookie => {
var url = zuoraClient.serverUrl + '/action/query';
var data = {
queryString: queryString
};
var query = {
body: data,
headers: {
'Content-type': 'application/json',
cookie: authCookie
},
json: true
};
return got
.post(url, query)
.then(res => {
var records = res.body.records;
if (res.body.done) {
return records;
} else {
return queryMore(res.body.queryLocator).then(additionalRecords => {
return _.concat(records, additionalRecords);
});
}
})
.catch(error => {
if (error.statusCode === 401) {
console.log(error.response.body);
return this(queryString);
return this(queryLocator);
} else {
throw error;
}
Expand Down
66 changes: 25 additions & 41 deletions lib/attachments.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
const BPromise = require('bluebird');
const got = require('got');
const _ = require('lodash');
const fs = require('fs');
const FormData = require('form-data');

module.exports = function(zuoraClient) {
function addAttachment(associatedObjectType, associatedObjectKey, description, file) {
return zuoraClient.authenticate().then(authCookie => {
return zuoraClient.authenticate().then(headers => {
const form = new FormData();
form.append('file', fs.createReadStream(file));

var url = zuoraClient.serverUrl + '/attachments/';
var query = {
headers: {
cookie: authCookie
},
headers,
query: {
associatedObjectType: associatedObjectType,
associatedObjectKey: associatedObjectKey,
Expand All @@ -29,23 +26,20 @@ module.exports = function(zuoraClient) {
}

function listAttachments(associatedObjectType, associatedObjectKey) {
return zuoraClient.authenticate().then(authCookie => {
return zuoraClient.authenticate().then(headers => {
var url = zuoraClient.serverUrl + '/attachments/' + associatedObjectType + '/' + associatedObjectKey;
var query = {
headers: {
'Content-type': 'application/json',
cookie: authCookie
},
headers,
json: true
};
return got
.get(url, query)
.then(res => {
var attachments = res.body.attachments;
if (res.body.nextPage) {
return listAdditionalAttachments(res.body.nextPage).then(additionalAttachments => {
return _.concat(attachments, additionalAttachments);
});
return listAdditionalAttachments(res.body.nextPage).then(additionalAttachments =>
_.concat(attachments, additionalAttachments)
);
} else {
return attachments;
}
Expand All @@ -62,22 +56,19 @@ module.exports = function(zuoraClient) {
}

function listAdditionalAttachments(nextPage) {
return zuoraClient.authenticate().then(authCookie => {
return zuoraClient.authenticate().then(headers => {
var query = {
headers: {
'Content-type': 'application/json',
cookie: authCookie
},
headers,
json: true
};
return got
.get(nextPage, query)
.then(res => {
var attachments = res.body.attachments;
if (res.body.nextPage) {
return listAdditionalAttachments(res.body.nextPage).then(additionalAttachments => {
return _.concat(attachments, additionalAttachments);
});
return listAdditionalAttachments(res.body.nextPage).then(additionalAttachments =>
_.concat(attachments, additionalAttachments)
);
} else {
return attachments;
}
Expand All @@ -94,32 +85,25 @@ module.exports = function(zuoraClient) {
}

return {
add: (associatedObjectType, associatedObjectKey, description, file) => {
return addAttachment(associatedObjectType, associatedObjectKey, description, file);
},
find: (associatedObjectType, associatedObjectKey, description) => {
return listAttachments(associatedObjectType, associatedObjectKey).then(results => {
return _.filter(results, {
description: description
});
});
},
delete: attachmentId => {
return zuoraClient.authenticate().then(authCookie => {
add: (associatedObjectType, associatedObjectKey, description, file) =>
addAttachment(associatedObjectType, associatedObjectKey, description, file),
find: (associatedObjectType, associatedObjectKey, description) =>
listAttachments(associatedObjectType, associatedObjectKey).then(results =>
_.filter(results, {
description
})
),
delete: attachmentId =>
zuoraClient.authenticate().then(headers => {
var url = zuoraClient.serverUrl + '/attachments/' + attachmentId;
var query = {
headers: {
cookie: authCookie
}
headers
};
return got.delete(url, query).then(res => {
console.log(res.body);
return res.body;
});
});
},
list: (associatedObjectType, associatedObjectKey) => {
return listAttachments(associatedObjectType, associatedObjectKey);
}
}),
list: (associatedObjectType, associatedObjectKey) => listAttachments(associatedObjectType, associatedObjectKey)
};
};
39 changes: 13 additions & 26 deletions lib/contacts.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
var BPromise = require("bluebird");
var got = require("got");
var _ = require("lodash");
var got = require('got');

module.exports = function(zuoraClient) {


function getContact(id) {
return zuoraClient.authenticate().then(authCookie => {
var url = zuoraClient.serverUrl + "/object/contact/" + id;
var query = {
headers: {
"Content-type": "application/json",
"cookie": authCookie
},
json: true
};
return got.get(url, query)
.then(res => {
return res.body;
});
return {
get: contactId =>
zuoraClient.authenticate().then(headers => {
var url = zuoraClient.serverUrl + '/object/contact/' + contactId;
var query = {
headers,
json: true
};
return got.get(url, query).then(res => {
return res.body;
});
}

return {
get: contactId => {
return getContact(contactId);
}
};
})
};
};
Loading

0 comments on commit 28aa2da

Please sign in to comment.