Skip to content

Commit

Permalink
parse CSV function debugging the delimiter error
Browse files Browse the repository at this point in the history
  • Loading branch information
BL committed Apr 27, 2018
1 parent 2ca56d3 commit 2e0cb73
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions lib/AmazonMwsResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,41 +151,48 @@ AmazonMwsResource.prototype = {
}
}

function processResponseType(res, response, callback) {
function parseCSVFile(res, responseString, callback) {
var data = [];
csv.fromString(responseString, {headers: true, delimiter: '\t', ignoreEmpty: true})
.on('data', function (value) {
data.push(value);
})
.on('end', function () {
debug('response after parsing tab delimited file %o ', data);
var items = {};
items.data = data;
items.Headers = {
'x-mws-quota-max': res.headers['x-mws-quota-max'] || 'unknown',
'x-mws-quota-remaining': res.headers['x-mws-quota-remaining'] || 'unknown',
'x-mws-quota-resetson': res.headers['x-mws-quota-resetson'] || 'unknown',
'x-mws-timestamp': res.headers['x-mws-timestamp']
};
return callback(null, items);
})
.on('error', function (error) {
return callback(error);
});
}

function processResponseType(res, responseString, callback) {
//debug('response %o ', response);
//debug('res.headers %o ', res.headers);
if (RESPONSE_CONTENT_TYPE.indexOf(res.headers['content-type'].toLowerCase()) > -1) {
debug('It is XML Response');
response = xml2json.toJson(response);
var items = xml2json.toJson(responseString);
//debug('response after parsing JSON %o ', response);
response = JSON.parse(response);
response.Headers = {
items = JSON.parse(items);
items.Headers = {
'x-mws-quota-max': res.headers['x-mws-quota-max'] || 'unknown',
'x-mws-quota-remaining': res.headers['x-mws-quota-remaining'] || 'unknown',
'x-mws-quota-resetson': res.headers['x-mws-quota-resetson'] || 'unknown',
'x-mws-timestamp': res.headers['x-mws-timestamp']
};
//debug('after adding header response', response);
return callback(null, response);
return callback(null, items);
} else {
debug('It is NON-XML Response');
var data = [];
csv.fromString(response, {headers: true, delimiter: '\t'})
.on('data', function (value) {
data.push(value);
})
.on('end', function () {
debug('response after parsing tab delimited file %o ', data);
response = {};
response.data = data;
response.Headers = {
'x-mws-quota-max': res.headers['x-mws-quota-max'] || 'unknown',
'x-mws-quota-remaining': res.headers['x-mws-quota-remaining'] || 'unknown',
'x-mws-quota-resetson': res.headers['x-mws-quota-resetson'] || 'unknown',
'x-mws-timestamp': res.headers['x-mws-timestamp']
};
return callback(null, response);
});
parseCSVFile(res, responseString, callback);
}
}

Expand Down

0 comments on commit 2e0cb73

Please sign in to comment.