Skip to content

Commit

Permalink
Merge pull request #59 from masterT/master
Browse files Browse the repository at this point in the history
Fix issue #58 and test suite
  • Loading branch information
masterT authored Sep 15, 2016
2 parents bc00b94 + 6d0a335 commit d94e209
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 82 deletions.
187 changes: 123 additions & 64 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
Node.js client for [Amazon Product Advertising API](https://affiliate-program.amazon.com/gp/advertising/api/detail/main.html)
![alt text](http://i.imgur.com/MwfPRfB.gif "Logo Title Text 1")


[![NPM](https://nodei.co/npm/amazon-product-api.png?downloads=true)](https://nodei.co/npm/amazon-product-api/)


## Installation
Install using npm:
```sh
Expand All @@ -19,11 +19,12 @@ Install in Meteor:
meteor add quackware:amazon-product-api
```


## Usage

Require library
```javascript
amazon = require('amazon-product-api');
var amazon = require('amazon-product-api');
```

Create client
Expand All @@ -34,12 +35,16 @@ var client = amazon.createClient({
awsTag: "aws Tag"
});
```
Now you are ready to use the API!

Now you can search for items on amazon:

### Search Items
### ItemSearch

using promises:
> The ItemSearch operation searches for items on Amazon. The Product Advertising API returns up to ten items per search results page.
[📖 Documentation](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemSearch.html)

Using promises:
```javascript
client.itemSearch({
director: 'Quentin Tarantino',
Expand All @@ -54,7 +59,7 @@ client.itemSearch({
});
```

using a callback:
Using a callback:
```javascript
client.itemSearch({
director: 'Quentin Tarantino',
Expand All @@ -66,8 +71,8 @@ client.itemSearch({
if (err) {
console.log(err);
} else {
console.log(results); // products
console.log(response); // response (containing TotalPages, TotalResults, MoreSearchResultsUrl and so on)
console.log(results); // products (Array of Object)
console.log(response); // response (Array where the first element is an Object that contains Request, Item, etc.)
}
});
```
Expand All @@ -88,57 +93,30 @@ co(function *(){
})();
```

### Search query options:
#### Query params:

You can add any [available params](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemSearch.html) for the *itemSearch* method.
You can add any [available params](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemSearch.html) for the *itemSearch* method:

[condition:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemSearch.html) availiable options - 'All', 'New', 'Used', 'Refurbished', 'Collectible'. Defaults to 'All'
[keywords:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemSearch.html) Defaults to ''
[responseGroup:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/CHAP_ResponseGroupsList.html) You can use multiple values by separating them with comma (e.g responseGroup: 'ItemAttributes,Offers,Images'). Defaults to'ItemAttributes'
[searchIndex:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/USSearchIndexParamForItemsearch.html) Defaults to 'All'.
[itemPage:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemSearch.html) Defaults to '1'.
sort: Valid values include 'salesrank','psrank','titlerank','-price','price'.
domain: Defaults to 'webservices.amazon.com'.
- [condition:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemSearch.html) availiable options - 'All', 'New', 'Used', 'Refurbished', 'Collectible'. Defaults to 'All'.

## Example
### Setup your own server that doesn't require signatures and timestamp
```javascript
var amazon = require('amazon-product-api'),
koa = require('koa'),
router = require('koa-router');
- [keywords:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemSearch.html) Defaults to ''

var app = koa();
app.use(router(app));


var client = amazon.createClient({
awsTag: process.env.AWS_TAG,
awsId: process.env.AWS_ID,
awsSecret: process.env.AWS_SECRET
});
- [responseGroup:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/CHAP_ResponseGroupsList.html) You can use multiple values by separating them with comma (e.g responseGroup: 'ItemAttributes,Offers,Images'). Defaults to'ItemAttributes'

- [searchIndex:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/USSearchIndexParamForItemsearch.html) Defaults to 'All'.

app.get('/amazon/:index', function* (){
this.body = yield client.itemSearch({
keywords: this.query.title,
searchIndex: this.params.index,
responseGroup: 'ItemAttributes,Offers,Images'
});
});
- [itemPage:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemSearch.html) Defaults to '1'.

app.listen(3000);
```
- [sort](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/SortingbyPopularityPriceorCondition.html): Valid values include 'salesrank','psrank','titlerank','-price','price', etc.

Working demo:
[Search for Alien DVDs](http://watchlist-koa.herokuapp.com/amazon/DVD?title=alien)
[Search for Streets of Rage videogame](http://watchlist-koa.herokuapp.com/amazon/VideoGames?title=streets%20of%20rage)
[Search for shoes](http://watchlist-koa.herokuapp.com/amazon/Shoes?title=nike%20nevis)

### ItemLookup

> Given an Item identifier, the ItemLookup operation returns some or all of the item attributes, depending on the response group specified in the request.
### Lookup Item
[📖 Documentation](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemLookup.html)

using promises:
Using promises:
```javascript
client.itemLookup({
idType: 'UPC',
Expand All @@ -150,7 +128,7 @@ client.itemLookup({
});
```

using a callback:
Using a callback:
```javascript
client.itemLookup({
idType: 'UPC',
Expand All @@ -165,25 +143,35 @@ client.itemLookup({
});
```

### LookupItem query options:
#### Query params:

You can add any [available params](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemLookup.html) for the *ItemLookup* method.

[condition:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemLookup.html) availiable options - 'All', 'New', 'Used', 'Refurbished', 'Collectible'. Defaults to 'All'
[idType:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemLookup.html) Type of item identifier used to look up an item. Availiable options - 'ASIN', 'SKU', 'UPC', 'EAN', 'ISBN'. Defaults to 'ASIN'.
[includeReviewsSummary:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemLookup.html) availiable options - 'True','False'. Defaults to 'True'.
[itemId:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemLookup.html) One or more (up to ten) positive integers that uniquely identify an item.
[responseGroup:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/CHAP_ResponseGroupsList.html) You can use multiple values by separating them with comma (e.g responseGroup: 'ItemAttributes,Offers,Images'). Defaults to'ItemAttributes'
[searchIndex:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/USSearchIndexParamForItemsearch.html) Defaults to 'All'.
[truncateReviewsAt:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/CHAP_ResponseGroupsList.html) Defaults to '1000'. To return complete reviews, specify '0'.
[variationPage:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/CHAP_ResponseGroupsList.html) Defaults to 'All'.
- [condition:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemLookup.html) availiable options - 'All', 'New', 'Used', 'Refurbished', 'Collectible'. Defaults to 'All'.

- [idType:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemLookup.html) Type of item identifier used to look up an item. Availiable options - 'ASIN', 'SKU', 'UPC', 'EAN', 'ISBN'. Defaults to 'ASIN'.

- [includeReviewsSummary:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemLookup.html) availiable options - 'True','False'. Defaults to 'True'.

- [itemId:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemLookup.html) One or more (up to ten) positive integers that uniquely identify an item.

- [responseGroup:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/CHAP_ResponseGroupsList.html) You can use multiple values by separating them with comma (e.g responseGroup: 'ItemAttributes,Offers,Images'). Defaults to'ItemAttributes'.

- [searchIndex:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/USSearchIndexParamForItemsearch.html) Defaults to 'All'.

- [truncateReviewsAt:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/CHAP_ResponseGroupsList.html) Defaults to '1000'. To return complete reviews, specify '0'.

- [variationPage:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/CHAP_ResponseGroupsList.html) Defaults to 'All'.
domain: Defaults to 'webservices.amazon.com'.


### BrowseNodeLookup

### Browse Node Lookup
> Given a browse node ID, BrowseNodeLookup returns the specified browse node’s name, children, and ancestors. The names and browse node IDs of the children and ancestor browse nodes are also returned. BrowseNodeLookup enables you to traverse the browse node hierarchy to find a browse node.
using promises:
[📖 Documentation](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/BrowseNodeLookup.html)

Using promises:
```javascript
client.browseNodeLookup({
browseNodeId: '549726',
Expand All @@ -195,7 +183,7 @@ client.browseNodeLookup({
});
```

using a callback:
Using a callback:
```javascript
client.browseNodeLookup({
browseNodeId: '549726',
Expand All @@ -209,15 +197,49 @@ client.browseNodeLookup({
});
```

### BrowseNodeLookup query options:
#### Query params:

You can add any [available params](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/BrowseNodeLookup.html) for the *BrowseNodeLookup* method.

[browseNodeId:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/BrowseNodeLookup.html) A positive integer assigned by Amazon that uniquely identifies a product category.
- [browseNodeId:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/BrowseNodeLookup.html) A positive integer assigned by Amazon that uniquely identifies a product category.

- [responseGroup:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/CHAP_ResponseGroupsList.html) You can use multiple values by separating them with comma (e.g responseGroup: 'MostGifted,NewReleases,MostWishedFor,TopSellers'). Defaults to 'BrowseNodeInfo'.


## Specify the endpoint

To use a different endpoint, you need the choose it from the [endpoints list](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/AnatomyOfaRESTRequest.html#EndpointsandWebServices), then pass the **domain** of the endpoint URL to the `domain` param of your query.

[responseGroup:](http://docs.aws.amazon.com/AWSECommerceService/latest/DG/CHAP_ResponseGroupsList.html) You can use multiple values by separating them with comma (e.g responseGroup: 'MostGifted,NewReleases,MostWishedFor,TopSellers'). Defaults to 'BrowseNodeInfo'
By default, the domaine used is `webservices.amazon.com`.

### Passing a custom `request`
#### Example:

I want to query the Canadian store 🇨🇦 .
The endpoint URL is https://webservices.amazon.ca/onca/xml.
The **domain** of the endpoint is `webservices.amazon.ca`.

```javascript
var query = {
artist: 'Radiohead',
searchIndex: 'Music',
sort: 'relevancerank',
itemPage: 1,
availability: 'Available',
responseGroup: 'OfferFull,Large,Images',
domain: 'webservices.amazon.ca'
};

client.itemSearch(query, function (error, results) {
if (error) {
console.log(error);
} else {
console.log(results);
}
})
```


## Passing a custom `request`

You can pass a custom `request` function to be used, for example if you are throttling requests.

Expand All @@ -230,3 +252,40 @@ client.itemSearch({
// ...
});
```


## Example

Setup your own server that doesn't require signatures and timestamp.

```javascript
var amazon = require('amazon-product-api'),
koa = require('koa'),
router = require('koa-router');

var app = koa();
app.use(router(app));


var client = amazon.createClient({
awsTag: process.env.AWS_TAG,
awsId: process.env.AWS_ID,
awsSecret: process.env.AWS_SECRET
});


app.get('/amazon/:index', function* (){
this.body = yield client.itemSearch({
keywords: this.query.title,
searchIndex: this.params.index,
responseGroup: 'ItemAttributes,Offers,Images'
});
});

app.listen(3000);
```

Working demo:
- [Search for Alien DVDs](http://watchlist-koa.herokuapp.com/amazon/DVD?title=alien)
- [Search for Streets of Rage videogame](http://watchlist-koa.herokuapp.com/amazon/VideoGames?title=streets%20of%20rage)
- [Search for shoes](http://watchlist-koa.herokuapp.com/amazon/Shoes?title=nike%20nevis)
10 changes: 4 additions & 6 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ var formatQueryParams = function (query, method, credentials) {
ItemPage: '1'
});

// Constants
params['Version'] = '2013-08-01';

} else if (method === 'ItemLookup') {
// Default
params = setDefaultParams(params, {
Expand All @@ -69,9 +66,6 @@ var formatQueryParams = function (query, method, credentials) {
delete params['SearchIndex'];
}

// Constants
params['Version'] = '2013-08-01';

} else if (method === 'BrowseNodeLookup') {
// Default
params = setDefaultParams(params, {
Expand All @@ -80,6 +74,10 @@ var formatQueryParams = function (query, method, credentials) {
});
}


// Constants
params['Version'] = '2013-08-01';

// Common params
params['AWSAccessKeyId'] = credentials.awsId;
// awsTag is associated with domain, so it ought to be defineable in query.
Expand Down
Loading

0 comments on commit d94e209

Please sign in to comment.