Skip to content

Commit fe950af

Browse files
authored
Merge pull request #6 from CyberSource/cybersource-rest-client-fileDownload-issueFix
Changes file download
2 parents ac3c4ed + 3d33753 commit fe950af

File tree

3 files changed

+35
-34
lines changed

3 files changed

+35
-34
lines changed

src/ApiClient.js

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@
9393
this.agent = new superagent.agent();
9494
}
9595

96+
/**
97+
* The filepath where reports are downloaded
98+
*/
99+
this.downloadFilePath = '';
96100
};
97101

98102
/**
@@ -356,6 +360,8 @@
356360
exports.prototype.setConfiguration = function (configObject) {
357361

358362
this.merchantConfig = new AuthenticationSDK.MerchantConfig(configObject);
363+
this.constants = AuthenticationSDK.Constants;
364+
this.logger = AuthenticationSDK.Logger.getLogger(this.merchantConfig);
359365
}
360366

361367
/**
@@ -367,41 +373,44 @@
367373
*/
368374
exports.prototype.callAuthenticationHeader = function (httpMethod, requestTarget, requestBody, headerParams) {
369375

370-
var Constants = AuthenticationSDK.Constants;
371-
372376
this.merchantConfig.setRequestTarget(requestTarget);
373377
this.merchantConfig.setRequestType(httpMethod)
374378
this.merchantConfig.setRequestJsonData(requestBody);
375-
var logger = AuthenticationSDK.Logger.getLogger(this.merchantConfig);
376-
var token = AuthenticationSDK.Authorization.getToken(this.merchantConfig, logger);
379+
380+
this.logger.info("Authentication Type : " + this.merchantConfig.getAuthenticationType());
381+
this.logger.info(this.constants.REQUEST_TYPE + ' : ' + httpMethod.toUpperCase());
377382

378-
if (this.merchantConfig.getAuthenticationType() === Constants.JWT) {
383+
var token = AuthenticationSDK.Authorization.getToken(this.merchantConfig, this.logger);
384+
385+
if (this.merchantConfig.getAuthenticationType() === this.constants.JWT) {
379386
token = "Bearer " + token;
380387
headerParams['Authorization'] = token;
388+
this.logger.info(this.constants.AUTHORIZATION + ' : ' + token);
381389
}
382-
else if (this.merchantConfig.getAuthenticationType() === Constants.HTTP) {
390+
else if (this.merchantConfig.getAuthenticationType() === this.constants.HTTP) {
383391
var date = new Date(Date.now()).toUTCString();
384392

385-
if (httpMethod === "POST" || httpMethod === "PATCH" || httpMethod === "PUT") {
386-
var digest = AuthenticationSDK.PayloadDigest.generateDigest(this.merchantConfig, logger);
387-
digest = Constants.SIGNATURE_ALGORITHAM + digest;
388-
logger.info(Constants.DIGEST + " : " + digest);
393+
if (httpMethod.toLowerCase() === this.constants.POST
394+
|| httpMethod.toLowerCase() === this.constants.PATCH
395+
|| httpMethod.toLowerCase() === this.constants.PUT) {
396+
var digest = AuthenticationSDK.PayloadDigest.generateDigest(this.merchantConfig, this.logger);
397+
digest = this.constants.SIGNATURE_ALGORITHAM + digest;
398+
this.logger.info(this.constants.DIGEST + " : " + digest);
389399
headerParams['digest'] = digest;
390-
logger.info('digest : ' + headerParams['digest']);
391400
}
392401

393402
headerParams['v-c-merchant-id'] = this.merchantConfig.getMerchantID();
394403
headerParams['date'] = date;
395404
headerParams['host'] = this.merchantConfig.getRequestHost();
396405
headerParams['signature'] = token;
397-
headerParams['User-Agent'] = "Mozilla/5.0";
398-
399-
logger.info("v-c-merchant-id : " + this.merchantConfig.getMerchantID());
400-
logger.info("date : " + date);
401-
logger.info("host : " + this.merchantConfig.getRequestHost());
402-
logger.info("signature : " + token);
403-
logger.info("User-Agent : " + headerParams['User-Agent']);
404-
logger.info("End > ==============================")
406+
headerParams['User-Agent'] = this.constants.USER_AGENT_VALUE;
407+
408+
this.logger.info("v-c-merchant-id : " + this.merchantConfig.getMerchantID());
409+
this.logger.info("date : " + date);
410+
this.logger.info("host : " + this.merchantConfig.getRequestHost());
411+
this.logger.info("signature : " + token);
412+
this.logger.info("User-Agent : " + headerParams['User-Agent']);
413+
this.logger.info(this.constants.END_TRANSACTION);
405414
}
406415

407416
return headerParams;
@@ -444,7 +453,7 @@
444453
this.applyAuthToRequest(request, authNames);
445454

446455
// set query parameters
447-
if (httpMethod.toUpperCase() === 'GET' && this.cache === false) {
456+
if (httpMethod.toLowerCase() === this.constants.GET && this.cache === false) {
448457
queryParams['_'] = new Date().getTime();
449458
}
450459
request.query(this.normalizeParams(queryParams));
@@ -454,7 +463,9 @@
454463
*/
455464
var requestTarget = this.buildRequestTarget(path, pathParams, queryParams);
456465

457-
if (httpMethod.toUpperCase() === 'POST' || httpMethod === 'PATCH' || httpMethod === 'PUT') {
466+
if (httpMethod.toLowerCase() === this.constants.POST
467+
|| httpMethod.toLowerCase() === this.constants.PATCH
468+
|| httpMethod.toLowerCase() === this.constants.PUT) {
458469
bodyParam = JSON.stringify(bodyParam, null, 0);
459470
}
460471
headerParams = this.callAuthenticationHeader(httpMethod, requestTarget, bodyParam, headerParams);
@@ -475,8 +486,6 @@
475486
request.type('application/json');
476487
}
477488

478-
479-
480489
if (contentType === 'application/x-www-form-urlencoded') {
481490
request.send(querystring.stringify(this.normalizeParams(formParams)));
482491
} else if (contentType == 'multipart/form-data') {
@@ -501,14 +510,7 @@
501510
/* Code for downloading file from stream */
502511
if (accept === 'application/xml') {
503512
var fs = require('fs');
504-
var path = require('path');
505-
var fileName;
506-
if (queryParams['reportName'])
507-
fileName = queryParams['reportName'] + '.xml';
508-
else
509-
fileName = "FileIdentifier.csv";
510-
var filePath = path.join(this.merchantConfig.getKeysDirectory(), fileName);
511-
var stream = fs.createWriteStream(path.resolve(filePath));
513+
var stream = fs.createWriteStream(this.downloadFilePath);
512514
request.send().pipe(stream);
513515
request._endCalled = false;
514516
}
@@ -551,7 +553,6 @@
551553
};
552554

553555
/**
554-
* @ghari
555556
* Build request target required for the signature generation
556557
* @param {String} path
557558
* @param {Object} pathParams

src/api/ReportDownloadsApi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* @param {module:ApiClient} apiClient Optional API client implementation to use,
4444
* default to {@link module:ApiClient#instance} if unspecified.
4545
*/
46-
var exports = function(configObject, apiClient = undefined) {
46+
var exports = function(configObject, apiClient) {
4747
this.apiClient = apiClient || ApiClient.instance;
4848

4949
this.apiClient.setConfiguration(configObject);

src/api/SecureFileShareApi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* @param {module:ApiClient} apiClient Optional API client implementation to use,
4444
* default to {@link module:ApiClient#instance} if unspecified.
4545
*/
46-
var exports = function(configObject, apiClient = undefined) {
46+
var exports = function(configObject, apiClient) {
4747
this.apiClient = apiClient || ApiClient.instance;
4848

4949
this.apiClient.setConfiguration(configObject);

0 commit comments

Comments
 (0)