Skip to content

Commit

Permalink
Fix find attachments and add downloadFile (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Gaestel authored May 28, 2020
1 parent 0df3270 commit b18d5eb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 41 deletions.
51 changes: 26 additions & 25 deletions lib/attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const FormData = require('form-data');

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

Expand All @@ -15,26 +15,26 @@ module.exports = function(zuoraClient) {
query: {
associatedObjectType: associatedObjectType,
associatedObjectKey: associatedObjectKey,
description: description
description: description,
},
body: form
body: form,
};
return got.post(url, query).then(res => {
return got.post(url, query).then((res) => {
return res.body;
});
});
}

function listAttachments(associatedObjectType, associatedObjectKey) {
return zuoraClient.authenticate().then(headers => {
return zuoraClient.authenticate().then((headers) => {
const url = zuoraClient.serverUrl + '/v1/attachments/' + associatedObjectType + '/' + associatedObjectKey;
const query = {
headers,
json: true
json: true,
};
return got
.get(url, query)
.then(res => {
.then((res) => {
if (!res.body.success) {
return res.body;
}
Expand All @@ -45,13 +45,13 @@ module.exports = function(zuoraClient) {
return res.body;
}

return listAdditionalAttachments(res.body.nextPage).then(additionalAttachments =>
return listAdditionalAttachments(res.body.nextPage).then((additionalAttachments) =>
_.assignIn({}, res.body, {
attachments: _.concat(attachments, additionalAttachments)
attachments: _.concat(attachments, additionalAttachments),
})
);
})
.catch(error => {
.catch((error) => {
if (error.statusCode === 401) {
console.log(error.response.body);
return this(queryString);
Expand All @@ -63,24 +63,24 @@ module.exports = function(zuoraClient) {
}

function listAdditionalAttachments(nextPage) {
return zuoraClient.authenticate().then(headers => {
return zuoraClient.authenticate().then((headers) => {
const query = {
headers,
json: true
json: true,
};
return got
.get(nextPage, query)
.then(res => {
.then((res) => {
const attachments = res.body.attachments;
if (res.body.nextPage) {
return listAdditionalAttachments(res.body.nextPage).then(additionalAttachments =>
return listAdditionalAttachments(res.body.nextPage).then((additionalAttachments) =>
_.concat(attachments, additionalAttachments)
);
} else {
return attachments;
}
})
.catch(error => {
.catch((error) => {
if (error.statusCode === 401) {
console.log(error.response.body);
return this(queryString);
Expand All @@ -95,21 +95,22 @@ module.exports = function(zuoraClient) {
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 => {
listAttachments(associatedObjectType, associatedObjectKey).then((results) => {
results.attachments = _.filter(results.attachments, {
description,
});
return results;
}),
delete: (attachmentId) =>
zuoraClient.authenticate().then((headers) => {
const url = zuoraClient.serverUrl + '/v1/attachments/' + attachmentId;
const query = {
headers
headers,
};
return got.delete(url, query).then(res => {
return got.delete(url, query).then((res) => {
return res.body;
});
}),
list: (associatedObjectType, associatedObjectKey) => listAttachments(associatedObjectType, associatedObjectKey)
list: (associatedObjectType, associatedObjectKey) => listAttachments(associatedObjectType, associatedObjectKey),
};
};
37 changes: 21 additions & 16 deletions zuora.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ Zuora.prototype.authenticate = function() {
const auth_params = {
form: true,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
'Content-Type': 'application/x-www-form-urlencoded',
},
body: {
client_id: this.client_id,
client_secret: this.client_secret,
grant_type: 'client_credentials'
}
grant_type: 'client_credentials',
},
};

return got.post(url, auth_params).then(res => {
return got.post(url, auth_params).then((res) => {
const responseBody = JSON.parse(res.body);

this.access_token = responseBody.access_token;
Expand All @@ -90,11 +90,11 @@ Zuora.prototype.authenticate = function() {
headers: {
'user-agent': 'zuorajs',
apiAccessKeyId: this.apiAccessKeyId,
apiSecretAccessKey: this.apiSecretAccessKey
apiSecretAccessKey: this.apiSecretAccessKey,
},
json: true
json: true,
};
return got.post(url, query).then(res => {
return got.post(url, query).then((res) => {
this.authCookie = res.headers['set-cookie'][0];
return { cookie: this.authCookie };
});
Expand All @@ -111,31 +111,36 @@ Zuora.prototype.authenticate = function() {
};

Zuora.prototype.getObject = function(url) {
return this.authenticate().then(headers => {
return this.authenticate().then((headers) => {
const fullUrl = this.serverUrl + url;
const query = {
headers,
json: true
json: true,
};
return got.get(fullUrl, query).then(res => res.body);
return got.get(fullUrl, query).then((res) => res.body);
});
};

Zuora.prototype.queryFirst = function(queryString) {
return this.action.query(queryString).then(queryResult => (queryResult.size > 0 ? queryResult.records[0] : null));
return this.action.query(queryString).then((queryResult) => (queryResult.size > 0 ? queryResult.records[0] : null));
};

Zuora.prototype.queryFull = function(queryString) {
const fullQueryMore = queryLocator =>
const fullQueryMore = (queryLocator) =>
this.action
.queryMore(queryLocator)
.then(result =>
result.done ? result.records : fullQueryMore(result.queryLocator).then(more => _.concat(result.records, more))
.then((result) =>
result.done ? result.records : fullQueryMore(result.queryLocator).then((more) => _.concat(result.records, more))
);

return this.action
.query(queryString)
.then(result =>
result.done ? result.records : fullQueryMore(result.queryLocator).then(more => _.concat(result.records, more))
.then((result) =>
result.done ? result.records : fullQueryMore(result.queryLocator).then((more) => _.concat(result.records, more))
);
};

Zuora.prototype.downloadFile = async function(url) {
const headers = await this.authenticate();
return got.stream(this.serverUrl + url, { headers });
};

0 comments on commit b18d5eb

Please sign in to comment.