Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.

Commit ca0a2ab

Browse files
dhruvit-rsharathkumaranbu
authored andcommitted
For #11 and #12 (#13)
Allow streams for downloading and updating tc core library version
1 parent 8695e05 commit ca0a2ab

File tree

5 files changed

+51
-12
lines changed

5 files changed

+51
-12
lines changed

docs/SubmissionsApi.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,19 @@ await submissionApiM2MClient.downloadSubmission(submissionId)
669669
await submissionApiUserCredentialsClient.downloadSubmission(submissionId)
670670

671671
await submissionApiJwtMethodArgClient.downloadSubmission(submissionId, config.JWT)
672+
673+
// Stream Model
674+
const outputStream = fs.createWriteStream(filePath)
675+
const req = await submissionApiM2MClient.downloadSubmission(submissionId, null, true)
676+
req.pipe(outputStream)
677+
678+
const outputStream = fs.createWriteStream(filePath)
679+
const req = await submissionApiUserCredentialsClient.downloadSubmission(submissionId, null, true)
680+
req.pipe(outputStream)
681+
682+
const outputStream = fs.createWriteStream(filePath)
683+
const req = await submissionApiJwtMethodArgClient.downloadSubmission(submissionId, config.JWT, true)
684+
req.pipe(outputStream)
672685
```
673686

674687
### Demo
@@ -716,6 +729,7 @@ Name | Type | Description
716729
------------- | ------------- | -------------
717730
**submissionId** | String | the submission id
718731
**jwt** | String | the optional json web token
732+
**streamed** | Boolean | the optional flag indicating whether or not to return a stream instead of a promise (default is promise)
719733

720734
### Return type
721735

@@ -960,6 +974,19 @@ await submissionApiM2MClient.downloadArtifact(submissionId, artifactId)
960974
await submissionApiUserCredentialsClient.downloadArtifact(submissionId, artifactId)
961975

962976
await submissionApiJwtMethodArgClient.downloadArtifact(submissionId, artifactId, config.JWT)
977+
978+
// Stream Model
979+
const outputStream = fs.createWriteStream(filePath)
980+
const req = await submissionApiM2MClient.downloadArtifact(submissionId, artifactId, null, true)
981+
req.pipe(outputStream)
982+
983+
const outputStream = fs.createWriteStream(filePath)
984+
const req = await submissionApiUserCredentialsClient.downloadArtifact(submissionId, artifactId, null, true)
985+
req.pipe(outputStream)
986+
987+
const outputStream = fs.createWriteStream(filePath)
988+
const req = await submissionApiJwtMethodArgClient.downloadArtifact(submissionId, artifactId, config.JWT, true)
989+
req.pipe(outputStream)
963990
```
964991

965992
### Demo
@@ -1008,6 +1035,7 @@ Name | Type | Description
10081035
**submissionId** | String | the submission id
10091036
**artifactId** | String | the artifact id
10101037
**jwt** | String | the optional json web token
1038+
**streamed** | Boolean | the optional flag indicating whether or not to return a stream instead of a promise (default is a promise)
10111039

10121040
### Return type
10131041

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ module.exports = (allConfig) => {
226226
return require('./src/SubmissionsApi').deleteSubmission(config, submissionId, jwt)
227227
},
228228
// Download submission
229-
downloadSubmission: (submissionId, jwt) => {
230-
return require('./src/SubmissionsApi').downloadSubmission(config, submissionId, jwt)
229+
downloadSubmission: (submissionId, jwt, streamed = false) => {
230+
return require('./src/SubmissionsApi').downloadSubmission(config, submissionId, jwt, streamed)
231231
},
232232
// Create artifact for submission
233233
createArtifact: (submissionId, reqFormData, jwt) => {
@@ -238,8 +238,8 @@ module.exports = (allConfig) => {
238238
return require('./src/SubmissionsApi').listArtifacts(config, submissionId, jwt)
239239
},
240240
// Download artifact
241-
downloadArtifact: (submissionId, artifactId, jwt) => {
242-
return require('./src/SubmissionsApi').downloadArtifact(config, submissionId, artifactId, jwt)
241+
downloadArtifact: (submissionId, artifactId, jwt, streamed = false) => {
242+
return require('./src/SubmissionsApi').downloadArtifact(config, submissionId, artifactId, jwt, streamed)
243243
},
244244
// Delete artifact
245245
deleteArtifact: (submissionId, artifactId, jwt) => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"@hapi/joi": "^15.0.3",
1414
"lodash": "^4.17.15",
1515
"superagent": "^3.8.3",
16-
"tc-core-library-js": "appirio-tech/tc-core-library-js.git"
16+
"tc-core-library-js": "github:appirio-tech/tc-core-library-js#v2.6.4"
1717
},
1818
"devDependencies": {
1919
"chai": "^4.2.0",

src/SubmissionsApi.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,11 @@ const deleteSubmission = (config, submissionId, jwt = null) => {
116116
* Function to download submission by id.
117117
* @param {Object} config Configuration object
118118
* @param {String} submissionId the submission id
119+
* @param {Boolean} streamed Whether a stream is to be returned (Default: false)
119120
* @returns {Promise} the submission file content
120121
*/
121-
const downloadSubmission = (config, submissionId, jwt = null) => {
122-
return helper.reqToV5APIDownload(config, jwt, `${config.SUBMISSION_API_URL}/submissions/${submissionId}/download`)
122+
const downloadSubmission = (config, submissionId, jwt = null, streamed = false) => {
123+
return helper.reqToV5APIDownload(config, jwt, `${config.SUBMISSION_API_URL}/submissions/${submissionId}/download`, streamed)
123124
}
124125

125126
/**
@@ -149,10 +150,11 @@ const listArtifacts = (config, submissionId, jwt = null) => {
149150
* @param {Object} config Configuration object
150151
* @param {String} submissionId the submission id
151152
* @param {String} artifactId the artifact id
153+
* @param {Boolean} streamed Whether a stream is to be returned (Default: false)
152154
* @returns {Promise} the artifact file content
153155
*/
154-
const downloadArtifact = (config, submissionId, artifactId, jwt = null) => {
155-
return helper.reqToV5APIDownload(config, jwt, `${config.SUBMISSION_API_URL}/submissions/${submissionId}/artifacts/${artifactId}/download`)
156+
const downloadArtifact = (config, submissionId, artifactId, jwt = null, streamed = false) => {
157+
return helper.reqToV5APIDownload(config, jwt, `${config.SUBMISSION_API_URL}/submissions/${submissionId}/artifacts/${artifactId}/download`, streamed)
156158
}
157159

158160
/**

src/common/helper.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,23 @@ const reqToV5APIWithFile = async (config, jwt, path, formData, fileFieldName) =>
164164
* Function to download file using V5 API
165165
* @param {Object} config Configuration object
166166
* @param {String} jwt The JWT
167-
* @param (String) path Complete path of the API URL
167+
* @param {String} path Complete path of the API URL
168+
* @param {Boolean} streamed Whether a stream is to be returned (Default: false)
168169
* @returns {Promise}
169170
*/
170-
const reqToV5APIDownload = async (config, jwt, path) => {
171+
const reqToV5APIDownload = async (config, jwt, path, streamed = false) => {
171172
const token = await getToken(config, jwt)
172-
return request
173+
let req = request
173174
.get(path)
174175
.set('Authorization', `Bearer ${token}`)
176+
if (streamed) {
177+
req = req.buffer(false)
178+
req.then = undefined
179+
req.catch = undefined
180+
req.finally = undefined
181+
return req
182+
}
183+
return req
175184
.buffer(true)
176185
.parse(function (res, callback) {
177186
res.data = ''

0 commit comments

Comments
 (0)