Skip to content

Commit

Permalink
Shopping Api : Added getSingleItems and getMultipleitems (#84)
Browse files Browse the repository at this point in the history
* v2.7.7

* add getSingleItem for shipping api

* added getMultipleitems shopping api support

* version bump and update read me

* update version
  • Loading branch information
pajaydev authored Apr 25, 2020
1 parent 807f719 commit c98f5fd
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 59 deletions.
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ The intent is to simplify the request process by handling the tedious logic. It'

* [Installation](#installation)
* [Usage](#usage)
* [Examples](#examples)
* [Starter Guide](#starter-guide)
* [Test](#test)
* [Issues](#issues)
* [Contribution](#contribution)
* [LICENSE](#license)



## 🚚 Installation

```shell
Expand All @@ -46,6 +46,42 @@ let ebay = new eBay({

Check out the [Starter Guide](https://pajaydev.github.io/ebay-node-api) documentation with examples to get started.

## Examples

```javascript
// findItemsBykeyword
ebay.findItemsByKeywords({
keywords: 'Garmin nuvi 1300 Automotive GPS Receiver',
sortOrder: 'PricePlusShippingLowest', //https://developer.ebay.com/devzone/finding/callref/extra/fndcmpltditms.rqst.srtordr.html
pageNumber: 2,
limit: 10
}).then((data) => {
console.log(data);
}, (error) => {
console.log(error);
});

// Get Single item listing on eBay
ebay.getSingleItem('153265274986').then((data) => {
console.log(data);
});

// Search Items by Keyword
ebay.getAccessToken()
.then((data) => {
ebay.searchItems({
keyword: 'drone',
limit: '3'
}).then((data) => {
console.log(data);
// Data is in format of JSON
// To check the format of Data, Go to this url (https://developer.ebay.com/api- docs/buy/browse/resources/item_summary/methods/search#w4-w1-w4-SearchforItemsbyKeyword-0)
})
});
```

[More Examples](https://pajaydev.github.io/ebay-node-api)

## Test
All test files are present inside test folder. You can run using

Expand Down
17 changes: 13 additions & 4 deletions demo/shopping.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ ebay.getAllCategories('1234').then((data) => {
});


// Get User Profile
// https://developer.ebay.com/devzone/shopping/docs/callref/GetUserProfile.html
// // Get User Profile
// // https://developer.ebay.com/devzone/shopping/docs/callref/GetUserProfile.html
ebay.getUserDetails({ userId: 'ajaykumapratha_0', details: true }).then((data) => {
console.log(data);
}, (error) => {
Expand All @@ -33,10 +33,19 @@ ebay.getItemStatus(['153265274986', '153265274986']).then((data) => {

// https://developer.ebay.com/devzone/shopping/docs/callref/GetShippingCosts.html
ebay.getShippingCosts({
itemId: '153265274986', destCountryCode: 'US',
destPostalCode: '95128'
itemId: '153265274986', destinationCountryCode: 'US',
destinationPostalCode: '95128'
}).then((data) => {
console.log(data);
}, (error) => {
console.log(error);
});

//https://developer.ebay.com/devzone/shopping/docs/callref/getsingleitem.html
ebay.getSingleItem('153265274986').then((data) => {
console.log(data);
});

ebay.getMultipleItems({ itemId: ['153265274986', '153265274986'] }).then((data) => {
console.log(data);
});
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ebay-node-api",
"version": "2.8.1",
"version": "2.8.3",
"description": "Ebay node api client",
"main": "./src/index.js",
"homepage": "https://github.com/pajaydev/ebay-node-api",
Expand Down Expand Up @@ -32,7 +32,7 @@
"extends": "ajay"
},
"dependencies": {
"eslint-config-ajay": "^1.0.2",
"eslint-config-ajay": "^1.0.6",
"make-string": "^1.0.3",
"oauth-ebay": "^1.0.0"
},
Expand Down
10 changes: 4 additions & 6 deletions src/buildURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@ const buildURL = {
* @return {String} url
* @private
*/
buildShoppingUrl(options) {
buildShoppingUrl(options, operationName) {
let baseUrl = `https://${options.baseUrl}/Shopping?`;
baseUrl += 'appid=' + options.clientID;
baseUrl += '&callname=' + options.operationName;
baseUrl += '&version=967&siteid=0&responseencoding=JSON&';
baseUrl += options.param + '=' + options.name;
baseUrl += `appid=${options.clientID}`;
baseUrl += `&callname=${operationName}`;
baseUrl += `&version=967&siteid=${options.siteId || 0}&responseencoding=JSON`;
baseUrl += options.includeSelector ? '&IncludeSelector=' + options.includeSelector : '';
//base_url += '&GLOBAL-ID=' + oglobalID;
return baseUrl;
}

Expand Down
3 changes: 3 additions & 0 deletions src/common-utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ module.exports = {
return resultJSON;
});
},
setSiteId: function (siteId) {
this.options.siteId = siteId;
},
setHeaders(self, headerObj) {
self.headers = Object.assign({}, self.headers, headerObj);
},
Expand Down
11 changes: 3 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
'use strict';
const ebayBuyApi = require('./buy-api');
const { getAllCategories,
getShippingCosts,
getItemStatus,
getUserDetails } = require('./shopping');
const shoppingApi = require('./shopping');
const { getDefaultCategoryTreeId,
getCategoryTree,
getCategorySubtree,
Expand Down Expand Up @@ -47,6 +44,7 @@ function Ebay(options) {
this.options = options;
setHeaders(this, options.headers);
this.options.globalID = options.countryCode || 'EBAY-US';
this.options.siteId = options.siteId || '0';
}

Ebay.prototype = {
Expand All @@ -61,10 +59,7 @@ Ebay.prototype = {
getItemAspectsForCategory,
getMostWatchedItems,
getSimilarItems,
getAllCategories,
getShippingCosts,
getItemStatus,
getUserDetails,
...shoppingApi,
...ebayBuyApi,
...ebayFindingApi
};
Expand Down
77 changes: 49 additions & 28 deletions src/shopping.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,83 @@ const urlObject = require('./buildURL');
const makeString = require('make-string');

const getAllCategories = function (categoryID) {
this.options.name = categoryID ? categoryID : -1;
this.options.operationName = 'GetCategoryInfo';
this.options.param = 'CategoryID';
const url = urlObject.buildShoppingUrl(this.options);
return getRequest(url).then((data) => {
const requestURL = `${urlObject.buildShoppingUrl(this.options, 'GetCategoryInfo')}&${stringifyUrl({ 'CategoryID': categoryID || -1 })}`;
return getRequest(requestURL).then((data) => {
return JSON.parse(data);
}, console.error // eslint-disable-line no-console
);
};

const getUserDetails = function (input) {
if (!input || typeof input !== 'object') throw new Error('Invalid input');
if (!input.userId) throw new Error('Invalid Input, UserId is required');
this.options.operationName = 'GetUserProfile';
this.options.param = 'UserID';
this.options.name = input.userId;
this.options.includeSelector = input.details ? 'Details' : null;
const url = urlObject.buildShoppingUrl(this.options);
return getRequest(url).then((data) => {
if (!input.userId) throw new Error('invalid_request_error -> userId is null or invalid');
const requestUrl = `${urlObject.buildShoppingUrl(this.options, 'GetUserProfile')}&${stringifyUrl(input)}`;
return getRequest(requestUrl).then((data) => {
return JSON.parse(data);
}, console.error // eslint-disable-line no-console
);
};

const getItemStatus = function (itemIds) {
if (!itemIds) throw new Error('User ID is null or invalid');
this.options.operationName = 'GetItemStatus';
this.options.param = 'ItemID';
this.options.name = makeString(itemIds, { braces: 'false', quotes: 'no' });
const url = urlObject.buildShoppingUrl(this.options);
return getRequest(url).then((data) => {
if (!itemIds) throw new Error('invalid_request_error -> itemIds is null or invalid');
const paramsObj = {
'ItemID': makeString(itemIds, { braces: 'false', quotes: 'no' })
};
const requestUrl = `${urlObject.buildShoppingUrl(this.options, 'GetItemStatus')}&${stringifyUrl(paramsObj)}`;
return getRequest(requestUrl).then((data) => {
return JSON.parse(data);
}, console.error // eslint-disable-line no-console
);
};

const getShippingCosts = function (input) {
if (!input || typeof input !== 'object') throw new Error('Invalid input');
if (!input.itemId) throw new Error('Item ID is null or invalid');
this.options.operationName = 'GetShippingCosts';
this.options.param = 'ItemID';
this.options.name = input.itemId;
const countryCodeParam = input.destCountryCode ? '&DestinationCountryCode=' + input.destCountryCode : '';
const postalCodeParam = input.destPostalCode ? '&DestinationPostalCode=' + input.destPostalCode : '';
const params = countryCodeParam + postalCodeParam;
let url = urlObject.buildShoppingUrl(this.options);
url = url + params;
if (!input.itemId) throw new Error('invalid_request_error -> Item id is null or invalid');
const url = `${urlObject.buildShoppingUrl(this.options, 'GetShippingCosts')}&${stringifyUrl(input)} `;
return getRequest(url).then((data) => {
return JSON.parse(data);
}, console.error // eslint-disable-line no-console
);
};

/**
* @method getMultipleItems {Function}
* Retrieves publicly visible details about for one or more listings on eBay.
* @param {Object} options (required)
*/
const getMultipleItems = function (options) {
if (!options || !options.itemId) throw new Error('invalid_request_error -> Item ID is null or invalid');
const requestUrl = `${urlObject.buildShoppingUrl(this.options, 'GetMultipleItems')}&${stringifyUrl({ 'itemId': makeString(options.itemId, { braces: 'false', quotes: 'no' }) })}`;
return getRequest(requestUrl).then((data) => {
return JSON.parse(data);
}, console.error // eslint-disable-line no-console
);
};


/**
* @method getSingleItem {Function}
* Retrieves publicly visible details about one listing on eBay.
* @param {String} itemId (required)
*/
const getSingleItem = function (itemId) {
if (!itemId) throw new Error('invalid_request_error -> Item ID is null or invalid');
const requestUrl = `${urlObject.buildShoppingUrl(this.options, 'GetSingleItem')}&${stringifyUrl({ 'ItemID': itemId })} `;
return getRequest(requestUrl).then((data) => {
return JSON.parse(data);
}, console.error // eslint-disable-line no-console
);
};

const stringifyUrl = (obj) => {
return makeString(obj, { braces: 'false', assignment: '=', quotes: 'no', seperator: '&' });
};

module.exports = {
getAllCategories,
getUserDetails,
getItemStatus,
getShippingCosts
getShippingCosts,
getSingleItem,
getMultipleItems
};
12 changes: 5 additions & 7 deletions test/buildURL.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,29 @@ describe('test building url methods', () => {
globalID: 'EBAY-US',
baseSvcUrl: 'svcs.ebay.com'
};
expect(buildURL.buildSearchUrl(options)).to.be.equal(expected_search_url);
expect(buildURL.buildSearchUrl(options, 'findItemsByKeywords')).to.be.equal(expected_search_url);
});

it('test Shopping url without selector', () => {
let expected_search_url = 'https://open.api.ebay.com/Shopping?appid=testID&callname=demoShoppingName&version=967&siteid=0&responseencoding=JSON&keywords=iphone';
let expected_search_url = 'https://open.api.ebay.com/Shopping?appid=testID&callname=demoShoppingName&version=967&siteid=0&responseencoding=JSON';
let options = {
name: 'iphone',
operationName: 'demoShoppingName',
param: 'keywords',
clientID: 'testID',
baseUrl: 'open.api.ebay.com'
};
expect(buildURL.buildShoppingUrl(options)).to.be.equal(expected_search_url);
expect(buildURL.buildShoppingUrl(options, 'demoShoppingName')).to.be.equal(expected_search_url);
});

it('test Shopping url including selector', () => {
let expected_search_url = 'https://open.api.ebay.com/Shopping?appid=testID&callname=demoShoppingName&version=967&siteid=0&responseencoding=JSON&keywords=iphone&IncludeSelector=true';
let expected_search_url = 'https://open.api.ebay.com/Shopping?appid=testID&callname=demoShoppingName&version=967&siteid=0&responseencoding=JSON&IncludeSelector=true';
let options = {
name: 'iphone',
operationName: 'demoShoppingName',
param: 'keywords',
clientID: 'testID',
includeSelector: true,
baseUrl: 'open.api.ebay.com'
};
expect(buildURL.buildShoppingUrl(options)).to.be.equal(expected_search_url);
expect(buildURL.buildShoppingUrl(options, 'demoShoppingName')).to.be.equal(expected_search_url);
});
});
7 changes: 4 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1862,9 +1862,10 @@ [email protected], escape-string-regexp@^1.0.2, escape-string-regexp@^1
version "1.0.5"
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"

eslint-config-ajay@^1.0.2:
version "1.0.3"
resolved "https://registry.yarnpkg.com/eslint-config-ajay/-/eslint-config-ajay-1.0.3.tgz#3d5a2016469ae17eab7e4ce951f520faaeb75075"
eslint-config-ajay@^1.0.6:
version "1.0.6"
resolved "https://registry.yarnpkg.com/eslint-config-ajay/-/eslint-config-ajay-1.0.6.tgz#c741e63ca92b004006e7cdbae9d93185fdf102ba"
integrity sha512-guK8sw0qFnuSbAwFy6SL407gzmAAhKGtwizJ4tsXmE9nVK7NXkeJSQsW8CVBM3RBrM91tgWlUZ3hNU9OHkbmPA==
dependencies:
eslint "^5.16.0"

Expand Down

0 comments on commit c98f5fd

Please sign in to comment.