Skip to content

Commit

Permalink
Merge pull request #76 from bhushankumarl/development
Browse files Browse the repository at this point in the history
correct example for GetMyFeesEstimate API call
  • Loading branch information
Bhushankumar L authored Apr 17, 2019
2 parents a789626 + b64094f commit e531635
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 42 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
0.0.24
- Add Subscription in Typescript definition
- Correct Documentation
- Allow user to change response type into XML format
- Allow user to change content type for request

0.0.23
- Bug Fix for Feed Result charset

Expand Down
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ Originally by [Bhushankumar L](mailto:[email protected]).

#### Get My Fees Estimate
```
amazonMws.products.searchFor({
amazonMws.products.search({
'Version': '2011-10-01',
'Action': 'GetMyFeesEstimate',
'SellerId': 'SELLER_ID',
Expand Down Expand Up @@ -790,6 +790,44 @@ Originally by [Bhushankumar L](mailto:[email protected]).
});
```

#### Request Report Content Type
```
amazonMws.setContentType('application/json');
amazonMws.reports.submit({
'Version': '2009-01-01',
'Action': 'RequestReport',
'SellerId': 'SELLER_ID',
'MWSAuthToken': 'MWS_AUTH_TOKEN',
'ReportType': '_GET_MERCHANT_LISTINGS_ALL_DATA_'
}, function (error, response) {
if (error) {
console.log('error ', error);
return;
}
console.log('response', response);
});
```

#### Request Report XML Response
```
amazonMws.setResponseFormat('XML');
amazonMws.reports.submit({
'Version': '2009-01-01',
'Action': 'RequestReport',
'SellerId': 'SELLER_ID',
'MWSAuthToken': 'MWS_AUTH_TOKEN',
'ReportType': '_GET_MERCHANT_LISTINGS_ALL_DATA_'
}, function (error, response) {
if (error) {
console.log('error ', error);
return;
}
console.log('response', response);
});
```

### Sellers
#### List Marketplace Participations
```
Expand Down
2 changes: 1 addition & 1 deletion examples/javaScript/products/getMyFeesEstimate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var accessSecret = process.env.AWS_SECRET_ACCESS_KEY || 'YOUR_SECRET';
var amazonMws = require('../../../lib/amazon-mws')(accessKey, accessSecret);

var productRequest = function () {
amazonMws.products.searchFor({
amazonMws.products.search({
'Version': '2011-10-01',
'Action': 'GetMyFeesEstimate',
'SellerId': 'SELLER_ID',
Expand Down
30 changes: 30 additions & 0 deletions examples/javaScript/sample/requestReportContentType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

var accessKey = process.env.AWS_ACCESS_KEY_ID || 'YOUR_KEY';
var accessSecret = process.env.AWS_SECRET_ACCESS_KEY || 'YOUR_SECRET';

var amazonMws = require('../../../lib/amazon-mws')(accessKey, accessSecret);

/**
* This example has been written to override/set the contentType of the request.
*/

var reportRequest = function () {
amazonMws.setContentType('application/json');

amazonMws.reports.submit({
'Version': '2009-01-01',
'Action': 'RequestReport',
'SellerId': 'SELLER_ID',
'MWSAuthToken': 'MWS_AUTH_TOKEN',
'ReportType': '_GET_MERCHANT_LISTINGS_ALL_DATA_'
}, function (error, response) {
if (error) {
console.log('error ', error);
return;
}
console.log('response', response);
});
};

reportRequest();
30 changes: 30 additions & 0 deletions examples/javaScript/sample/requestReportXMLResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

var accessKey = process.env.AWS_ACCESS_KEY_ID || 'YOUR_KEY';
var accessSecret = process.env.AWS_SECRET_ACCESS_KEY || 'YOUR_SECRET';

var amazonMws = require('../../../lib/amazon-mws')(accessKey, accessSecret);

/**
* This example has been written to override/set the default RESPONSE type to XML.
*/

var reportRequest = function () {
amazonMws.setResponseFormat('XML');

amazonMws.reports.submit({
'Version': '2009-01-01',
'Action': 'RequestReport',
'SellerId': 'SELLER_ID',
'MWSAuthToken': 'MWS_AUTH_TOKEN',
'ReportType': '_GET_MERCHANT_LISTINGS_ALL_DATA_'
}, function (error, response) {
if (error) {
console.log('error ', error);
return;
}
console.log('response', response);
});
};

reportRequest();
32 changes: 15 additions & 17 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ declare class Sellers extends BaseAmazonMWS {

declare class Subscriptions extends BaseAmazonMWS {

create(params: any): Promise<any>;

searchFor(params: any): Promise<any>;

remove(params: any): Promise<any>;
Expand All @@ -72,33 +74,29 @@ declare class Subscriptions extends BaseAmazonMWS {

declare class AmazonMWS {

constructor()

constructor(key: string, token: string);

setApiKey(key: string, secret: string): void;

setHost(host?: string, port?: string, protocol?: string): void;

feeds: Feeds;

finances: Finances;

fulfillmentInboundShipment: FulfillmentInboundShipment;

fulfillmentInventory: FulfillmentInventory;

fulfillmentOutboundShipment: FulfillmentOutboundShipment;

merchantFulfillment: MerchantFulfillment;

orders: Orders;

products: Products;

reports: Reports;

sellers: Sellers;
subscriptions: Subscriptions;

constructor()

constructor(key: string, token: string);

setApiKey(key: string, secret: string): void;

setHost(host?: string, port?: string, protocol?: string): void;

setResponseFormat(responseFormat: string): void;

setContentType(setContentType: string): void;

}

Expand Down
19 changes: 17 additions & 2 deletions lib/AmazonMwsResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ AmazonMwsResource.prototype = {
debug('responseString ', responseString);
debug('contentType ', contentType);
debug('statusCode ', statusCode);
if (userOptions.responseType === 'XML') {
debug('Inside user response as XML option');
return callback.call(self, null, responseString);
}

try {
var errorResponse = {};
Expand Down Expand Up @@ -390,7 +394,10 @@ AmazonMwsResource.prototype = {

var userRaw = '';
var userCharset = '';
/**custom option passed by user, a better way to do this would be nice */
var responseType = '';
/**
* Custom option passed by user, a better way to do this would be nice
*/
if (data.__RAW__) {
userRaw = data.__RAW__;
delete data.__RAW__;
Expand All @@ -399,6 +406,9 @@ AmazonMwsResource.prototype = {
userCharset = data.__CHARSET__;
delete data.__CHARSET__;
}
if (self._AmazonMws.getApiField('format') !== 'JSON') {
responseType = self._AmazonMws.getApiField('format');
}
self.requestParams = data;

if (!self.requestParams.Version) {
Expand Down Expand Up @@ -472,6 +482,10 @@ AmazonMwsResource.prototype = {
self.body = self.requestParams;
}

if (self._AmazonMws.getApiField('contentType')) {
headers['Content-Type'] = self._AmazonMws.getApiField('contentType');
}

// Make a deep copy of the request params, assign to block scoped variable
var requestParamsCopy = JSON.parse(JSON.stringify(self.requestParams));
var requestParamsJSONCopy = JSON.parse(JSON.stringify(self.requestParamsJSON));
Expand Down Expand Up @@ -506,7 +520,8 @@ AmazonMwsResource.prototype = {
req.setTimeout(timeout, self._timeoutHandler(timeout, req, callback));
var userOptions = {
userCharset: userCharset,
userRaw: userRaw
userRaw: userRaw,
responseType: responseType
};
req.on('response', self._responseHandler(requestParamsJSONCopy, req, userOptions, callback));
req.on('error', self._errorHandler(req, callback));
Expand Down
8 changes: 6 additions & 2 deletions lib/amazon-mws.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
AmazonMws.DEFAULT_HOST = 'mws.amazonservices.com';
AmazonMws.DEFAULT_PORT = '443';
AmazonMws.DEFAULT_BASE_PATH = '/';
AmazonMws.DEFAULT_RESPONSE_FORMAT = '.json';
AmazonMws.DEFAULT_RESPONSE_FORMAT = 'JSON';
AmazonMws.DEFAULT_API_VERSION = null;

// Use node's default timeout:
Expand Down Expand Up @@ -72,7 +72,7 @@ function AmazonMws(accessKey, accessSecret, version) {
this._prepResources();
this.setApiKey(accessKey, accessSecret);
this.setApiVersion(version);
this.setResponseFormat(AmazonMws.DEFAULT_RESPONSE_FORMAT);
// this.setResponseFormat(AmazonMws.DEFAULT_RESPONSE_FORMAT);
}

AmazonMws.prototype = {
Expand All @@ -99,6 +99,10 @@ AmazonMws.prototype = {
this._setApiField('format', format);
},

setContentType: function (contentType) {
this._setApiField('contentType', contentType);
},

setApiVersion: function (version) {
if (version) {
this._setApiField('version', version);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "amazon-mws",
"version": "0.0.23",
"version": "0.0.24",
"description": "Amazon MWS API wrapper",
"keywords": [
"Amazon MWS",
Expand Down
42 changes: 24 additions & 18 deletions test/specs/products.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,23 @@ describe('Products', function () {
expect(options.MarketplaceId).to.be.a('string');
expect(options['ASINList.ASIN.1']).to.be.a('string');

var response = await amazonMws.products.searchFor(options);

expect(response).to.be.a('object');
expect(response).to.have.property('ASIN').to.be.a('string');
expect(response).to.have.property('status').to.be.a('string');
expect(response).to.have.property('Product').to.be.a('object');
expect(response).to.have.property('ResponseMetadata').to.be.a('object');
expect(response).to.have.property('ResponseMetadata').to.have.property('RequestId');
expect(response).to.have.property('Headers').to.be.a('object');
expect(response).to.have.property('Headers').to.have.property('x-mws-quota-max');
expect(response).to.have.property('Headers').to.have.property('x-mws-quota-remaining');
expect(response).to.have.property('Headers').to.have.property('x-mws-quota-resetson');
expect(response).to.have.property('Headers').to.have.property('x-mws-timestamp');
try {
var response = await amazonMws.products.searchFor(options);
expect(response).to.be.a('object');
expect(response).to.have.property('ASIN').to.be.a('string');
expect(response).to.have.property('status').to.be.a('string');
expect(response).to.have.property('Product').to.be.a('object');
expect(response).to.have.property('ResponseMetadata').to.be.a('object');
expect(response).to.have.property('ResponseMetadata').to.have.property('RequestId');
expect(response).to.have.property('Headers').to.be.a('object');
expect(response).to.have.property('Headers').to.have.property('x-mws-quota-max');
expect(response).to.have.property('Headers').to.have.property('x-mws-quota-remaining');
expect(response).to.have.property('Headers').to.have.property('x-mws-quota-resetson');
expect(response).to.have.property('Headers').to.have.property('x-mws-timestamp');
} catch (error) {
console.log('error ', error);
expect(error).to.be.undefined;
}
});

it('It should NOT get my price for INVALID ASIN using GetMyPriceForASIN Action', async function () {
Expand Down Expand Up @@ -131,7 +135,7 @@ describe('Products', function () {
expect(error).to.be.a('object');
expect(error).to.have.property('Type').to.be.a('string');
expect(error).to.have.property('Message').to.be.a('string');
expect(error).to.have.property('Detail').to.be.a('object');
// expect(error).to.have.property('Detail').to.be.a('object');
expect(error).to.have.property('StatusCode').to.be.a('number');
expect(error).to.have.property('RequestId').to.be.a('string');
expect(error).to.have.property('Headers').to.be.a('object');
Expand Down Expand Up @@ -165,10 +169,12 @@ describe('Products', function () {
expect(response).to.have.property('status').to.be.a('string');
expect(response).to.have.property('Product').to.be.a('object');
expect(response).to.have.property('Product').to.have.property('CompetitivePricing');
expect(response).to.have.property('Product').to.have.property('CompetitivePricing').to.have.property('NumberOfOfferListings');
expect(response).to.have.property('Product').to.have.property('CompetitivePricing').to.have.property('NumberOfOfferListings').to.have.property('OfferListingCount').to.be.a('array');
expect(response.Product.CompetitivePricing.NumberOfOfferListings.OfferListingCount[0]).to.have.property('condition');
expect(response.Product.CompetitivePricing.NumberOfOfferListings.OfferListingCount[0]).to.have.property('Value');
if (response.Product.CompetitivePricing.NumberOfOfferListings.OfferListingCount) {
expect(response).to.have.property('Product').to.have.property('CompetitivePricing').to.have.property('NumberOfOfferListings');
expect(response).to.have.property('Product').to.have.property('CompetitivePricing').to.have.property('NumberOfOfferListings').to.have.property('OfferListingCount').to.be.a('array');
expect(response.Product.CompetitivePricing.NumberOfOfferListings.OfferListingCount[0]).to.have.property('condition');
expect(response.Product.CompetitivePricing.NumberOfOfferListings.OfferListingCount[0]).to.have.property('Value');
}
expect(response).to.have.property('ResponseMetadata').to.be.a('object');
expect(response).to.have.property('ResponseMetadata').to.have.property('RequestId');
expect(response).to.have.property('Headers').to.be.a('object');
Expand Down

0 comments on commit e531635

Please sign in to comment.