Skip to content

Commit da53aec

Browse files
author
Bhushankumar L
authored
Merge pull request #54 from bhushankumarl/development
Development
2 parents f927a5f + 1694d89 commit da53aec

File tree

11 files changed

+193
-30
lines changed

11 files changed

+193
-30
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ node_js:
55
- "6"
66
- "7"
77
- "8"
8+
- "9"
89
sudo: false

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
0.0.20
2+
- Handle for non content type responses
3+
- Bug fix for GetMyPriceForASIN and similar other products API
4+
- Add example, test case for GetMyPriceForASIN
5+
- Add test case for failure/error responses
6+
- Add support for RequestReport API
7+
- Add example for GetMyFeesEstimate in Products
8+
- Add force check for the XML string
9+
110
0.0.19
211
- Correct lint and increase test case timeout
312
- Added Status Code in Error Response.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# amazon-mws (Amazon Marketplace Web Service)
2+
[![Version](https://img.shields.io/npm/v/amazon-mws.svg)](https://www.npmjs.org/package/amazon-mws)
23
[![Build Status](https://travis-ci.org/bhushankumarl/amazon-mws.svg?branch=master)](https://travis-ci.org/bhushankumarl/amazon-mws)
34

45
This API supported Amazon Marketplace Web Service(MWS)'s standard REST-style API that accepts/returns JSON requests and Here is the [API reference] (http://docs.developer.amazonservices.com/en_IN/dev_guide/DG_IfNew.html)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
var accessKey = process.env.AWS_ACCESS_KEY_ID || 'YOUR_KEY';
4+
var accessSecret = process.env.AWS_SECRET_ACCESS_KEY || 'YOUR_SECRET';
5+
6+
var amazonMws = require('../../../lib/amazon-mws')(accessKey, accessSecret);
7+
8+
var productRequest = function () {
9+
amazonMws.products.searchFor({
10+
'Version': '2011-10-01',
11+
'Action': 'GetMyFeesEstimate',
12+
'SellerId': 'SELLER_ID',
13+
'MWSAuthToken': 'MWS_AUTH_TOKEN',
14+
'FeesEstimateRequestList.FeesEstimateRequest.1.MarketplaceId': 'MARKET_PLACE_ID',
15+
'FeesEstimateRequestList.FeesEstimateRequest.1.IdType': 'ASIN',
16+
'FeesEstimateRequestList.FeesEstimateRequest.1.IdValue': 'ASIN',
17+
'FeesEstimateRequestList.FeesEstimateRequest.1.IsAmazonFulfilled': 'true',
18+
'FeesEstimateRequestList.FeesEstimateRequest.1.Identifier': 'Hello',
19+
'FeesEstimateRequestList.FeesEstimateRequest.1.PriceToEstimateFees.ListingPrice.CurrencyCode': 'USD',
20+
'FeesEstimateRequestList.FeesEstimateRequest.1.PriceToEstimateFees.ListingPrice.Amount': '30.00',
21+
'FeesEstimateRequestList.FeesEstimateRequest.1.PriceToEstimateFees.Shipping.CurrencyCode': 'USD',
22+
'FeesEstimateRequestList.FeesEstimateRequest.1.PriceToEstimateFees.Shipping.Amount': '3.99',
23+
'FeesEstimateRequestList.FeesEstimateRequest.1.PriceToEstimateFees.Points.PointsNumber': '0'
24+
}, function (error, response) {
25+
if (error) {
26+
console.log('error products', error);
27+
return;
28+
}
29+
console.log('response ', response);
30+
});
31+
};
32+
33+
productRequest();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
var accessKey = process.env.AWS_ACCESS_KEY_ID || 'YOUR_KEY';
4+
var accessSecret = process.env.AWS_SECRET_ACCESS_KEY || 'YOUR_SECRET';
5+
6+
var amazonMws = require('../../../lib/amazon-mws')(accessKey, accessSecret);
7+
8+
var productRequest = function () {
9+
amazonMws.products.searchFor({
10+
'Version': '2011-10-01',
11+
'Action': 'GetMyPriceForASIN',
12+
'SellerId': 'SELLER_ID',
13+
'MWSAuthToken': 'MWS_AUTH_TOKEN',
14+
'MarketplaceId': 'MARKET_PLACE_ID',
15+
'ASINList.ASIN.1': 'ASINList_ASIN_1'
16+
}, function (error, response) {
17+
if (error) {
18+
console.log('error products', error);
19+
return;
20+
}
21+
console.log('response ', response);
22+
});
23+
};
24+
25+
productRequest();

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ declare class Products extends BaseAmazonMWS {
4848

4949
declare class Reports extends BaseAmazonMWS {
5050

51+
submit(params: any): Promise<any>;
52+
5153
}
5254

5355
declare class Sellers extends BaseAmazonMWS {

lib/AmazonMwsResource.js

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var Error = require('./Error');
1818

1919
var hasOwn = {}.hasOwnProperty;
2020

21-
var RESPONSE_CONTENT_TYPE = ['text/xml', 'text/xml;charset=utf-8', 'application/xml'];
21+
var RESPONSE_CONTENT_TYPE_XML = ['text/xml', 'text/xml;charset=utf-8', 'application/xml'];
2222
// Provide extension mechanism for AmazonMws Resource Sub-Classes
2323
AmazonMwsResource.extend = utils.protoExtend;
2424

@@ -168,20 +168,31 @@ AmazonMwsResource.prototype = {
168168

169169
function processResponseType(res, responseString, callback) {
170170
//debug('res %o ', res);
171-
//debug('res.headers %o ', res.headers);
172-
if (RESPONSE_CONTENT_TYPE.indexOf(res.headers['content-type'].toLowerCase()) > -1) {
171+
var xmlParser = new xml2js.Parser({
172+
mergeAttrs: true,
173+
explicitArray: false,
174+
emptyTag: {}
175+
});
176+
if (!res.headers['content-type']) {
177+
debug('Content type has not set, so considered it as XML response');
178+
xmlParser.parseString(responseString, function (err, response) {
179+
//debug('response after parsing JSON %o ', response);
180+
return callback(null, response);
181+
});
182+
} else if (RESPONSE_CONTENT_TYPE_XML.indexOf(res.headers['content-type'].toLowerCase()) > -1) {
173183
debug('It is XML Response');
174-
var parser = new xml2js.Parser({
175-
explicitArray: false,
176-
ignoreAttrs: true
184+
xmlParser.parseString(responseString, function (err, response) {
185+
// debug('response after parsing JSON %o ', response);
186+
return callback(null, response);
177187
});
178-
179-
parser.parseString(responseString, function (err, response) {
180-
//debug('response after parsing JSON %o ', response);
188+
} else if (_.includes(responseString, '?xml')) {
189+
debug('It is XML Response be find out from responseString');
190+
xmlParser.parseString(responseString, function (err, response) {
191+
// debug('response after parsing JSON %o ', response);
181192
return callback(null, response);
182193
});
183194
} else {
184-
debug('It is NON-XML Response');
195+
debug('It is NON-XML Response, so considered it as CSV file');
185196
var TAB_DELIMITER = '\t';
186197
var COMMA_DELIMITER = ',';
187198
parseCSVFile(res, responseString, TAB_DELIMITER, function (error, response) {
@@ -200,29 +211,34 @@ AmazonMwsResource.prototype = {
200211
var dbgResponseBuffer = [];
201212
var headers = res.headers;
202213
var statusCode = res.statusCode;
214+
var contentType = '';
215+
if (headers['content-type']) {
216+
contentType = headers['content-type'].toLowerCase();
217+
}
203218
try {
204219
statusCode = parseInt(statusCode, 10);
205220
} catch (Exception) {
206221
debug('Failed to parse statusCode as statusCode not provided in the response. ', statusCode);
207222
}
208223
var charset = '';
209-
var content_type = '';
210224
var responseString = '';
211-
if (headers['content-type'].indexOf('charset') > -1 && headers['content-type'].split(';')[0] && headers['content-type'].split(';')[1]) {
212-
content_type = headers['content-type'].split(';')[0].toLowerCase();
213-
if (headers['content-type'].split(';')[1].match(/^((\b[^\s=]+)=(([^=]|\\=)+))*$/)[3]) {
214-
charset = headers['content-type'].split(';')[1].match(/^((\b[^\s=]+)=(([^=]|\\=)+))*$/)[3];
225+
226+
/**
227+
* Separate the charset & content type
228+
*/
229+
if (contentType.indexOf('charset') > -1 && contentType.split(';')[0] && contentType.split(';')[1]) {
230+
if (contentType.split(';')[1] && contentType.split(';')[1].match(/^((\b[^\s=]+)=(([^=]|\\=)+))*$/)[3]) {
231+
charset = contentType.split(';')[1].match(/^((\b[^\s=]+)=(([^=]|\\=)+))*$/)[3];
215232
}
216-
} else {
217-
content_type = headers['content-type'].toLowerCase();
233+
contentType = contentType.split(';')[0].toLowerCase();
218234
}
219235

220236
var ResponseHeaders = {
221237
'x-mws-quota-max': res.headers['x-mws-quota-max'] || 'unknown',
222238
'x-mws-quota-remaining': res.headers['x-mws-quota-remaining'] || 'unknown',
223239
'x-mws-quota-resetson': res.headers['x-mws-quota-resetson'] || 'unknown',
224240
'x-mws-timestamp': res.headers['x-mws-timestamp'],
225-
'content-type': content_type || 'unknown',
241+
'content-type': contentType || 'unknown',
226242
'content-charset': charset || 'unknown',
227243
'content-length': res.headers['content-length'] || 'unknown',
228244
'content-md5': res.headers['content-md5'] || 'unknown',
@@ -269,21 +285,21 @@ AmazonMwsResource.prototype = {
269285
}
270286

271287
debug('responseString ', responseString);
272-
debug('content_type ', content_type);
288+
debug('contentType ', contentType);
273289
debug('statusCode ', statusCode);
274290

275-
if (!content_type) {
276-
return callback.call(self, new Error.AmazonMwsAPIError({
277-
message: 'Content Type is not provided in response received from the AmazonMws API',
278-
StatusCode: statusCode || 'unknown'
279-
}), null);
280-
}
281-
282291
try {
292+
var errorResponse = {};
293+
if (statusCode > 499 && !responseString) {
294+
errorResponse.message = res.statusMessage || 'unknown';
295+
errorResponse.Headers = ResponseHeaders;
296+
errorResponse.StatusCode = statusCode || 'unknown';
297+
return callback.call(self, errorResponse, null);
298+
}
283299
processResponseType(res, responseString, function (error, response) {
284300
if (response.ErrorResponse) {
285301
debug('It is ErrorResponse');
286-
var errorResponse = response.ErrorResponse.Error;
302+
errorResponse = response.ErrorResponse.Error;
287303
errorResponse.Headers = ResponseHeaders;
288304
errorResponse.StatusCode = statusCode || 'unknown';
289305
errorResponse.RequestId = response.ErrorResponse.RequestID || response.ErrorResponse.RequestId || 'unknown';
@@ -301,7 +317,7 @@ AmazonMwsResource.prototype = {
301317
}
302318

303319
var ResponseMetadata = {};
304-
if (RESPONSE_CONTENT_TYPE.indexOf(content_type) > -1) {
320+
if (RESPONSE_CONTENT_TYPE_XML.indexOf(contentType) > -1) {
305321
/**
306322
* It should execute for only XML response
307323
*/

lib/resources/Reports.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ module.exports = AmazonMwsResource.extend({
88
path: 'Reports',
99
search: amazonMwsMethod({
1010
method: 'GET'
11+
}),
12+
submit: amazonMwsMethod({
13+
method: 'POST'
1114
})
12-
1315
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "amazon-mws",
3-
"version": "0.0.19",
3+
"version": "0.0.20",
44
"description": "Amazon MWS API wrapper",
55
"keywords": [
66
"Amazon MWS",

test/mocha.opts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
### mocha.opts
33
###
44

5+
--exclude ./test/specs/fulfillmentInboundShipment.spec.js
56
--bail
67
--silly
78
--full-trace

0 commit comments

Comments
 (0)