Skip to content

Commit

Permalink
Added findItemsineBayStores support (#94)
Browse files Browse the repository at this point in the history
* v2.7.7

* simple

* add find items in ebay stores api
  • Loading branch information
pajaydev authored May 21, 2020
1 parent c52bdcb commit f275408
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 71 deletions.
9 changes: 9 additions & 0 deletions demo/findingApi.js → demo/finding.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,12 @@ ebay.getVersion().then((data) => {
}, (error) => {
console.log(error);
});


// Find ebay stores here https://www.ebay.com/sns
// https://developer.ebay.com/devzone/finding/callref/findItemsIneBayStores.html
ebay.findItemsIneBayStores({storeName: 'Battery Gallery'}).then((data) => {
console.log(data);
}, (error) => {
console.log(error);
});
41 changes: 33 additions & 8 deletions demo/getAccessToken.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,38 @@
const EbayToken = require('oauth-ebay');
const Ebay = require('../src/index');
const { clientId, clientSecret } = require('./credentials/index');

let ebay = new EbayToken({
let ebay = new Ebay({
clientID: clientId,
clientSecret: clientSecret,
grantType: 'client_credentials'
body: {
grant_type: 'client_credentials',
scope: 'https://api.ebay.com/oauth/api_scope'

}
});
ebay.getAccessToken().then((data) => {
console.log(data);
}, (error) => {
console.log(error);
});

//console.log(ebay.getAccessToken());

// // //Search for Items by Keyword.
ebay.getAccessToken()
.then((data) => {
console.log("generate tokensss");
console.log(data);
});

console.log("++++++++++++++++++++");

ebay.getAccessToken()
.then((data) => {
console.log("generate tokensss");
console.log(data);
});


setTimeout(() => {
ebay.getAccessToken()
.then((data) => {
console.log("generate tokensss");
console.log(data);
});
}, 7200);
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.4",
"version": "2.8.5",
"description": "Ebay node api client",
"main": "./src/index.js",
"homepage": "https://github.com/pajaydev/ebay-node-api",
Expand All @@ -9,7 +9,7 @@
"test": "mocha && npm run lint",
"docs": "docsify init ./docs",
"serve-docs": "docsify serve docs",
"publish": "gh-pages --dist docs --dotfiles --message 'chore: Publish docs'",
"docs-publish": "gh-pages --dist docs --dotfiles --message 'chore: Publish docs'",
"prepublish": "npm run test"
},
"author": "Ajaykumar prathap",
Expand Down
40 changes: 38 additions & 2 deletions src/common-utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,42 @@ const { makeRequest } = require('../request');
const base64Encode = (encodeData) => {
const buff = Buffer.from(encodeData);;
return buff.toString('base64');
}
};

/**
* Constructs query param based on some logic to support filter and aspect_filter params.
* output will be keywords=iphone&itemFilter(0).name=Condition&itemFilter(0).value=3000&itemFilter(1).name=FreeShippingOnly&itemFilter(1).value=true&itemFilter(2).name=SoldItemsOnly&itemFilter(2).value=true
* @param {Object} options
*/
const constructAdditionalParams = (options) => {
let params = '';
let count = 0;
for (let key in options) {
if (options.hasOwnProperty(key)) {
if (key === 'entriesPerPage' || key === 'pageNumber') {
params = `${params}paginationInput.${key}=${options[key]}&`;
}
else if (key === 'keywords' || key === 'categoryId' || key === 'productId' || key === 'sortOrder' || key === 'storeName') {
const encodeParam = encodeURIComponent(options[key]);
params = `${params}${key}=${encodeParam}&`;
}
else if (key === 'affiliate') {
const innerParams = options[key];
for (let innerKey in innerParams) {
params = `${params}${key}.${innerKey}=${innerParams[innerKey]}&`;
}
}
else {
params = `${params}itemFilter(${count}).name=${key}&
itemFilter(${count}).value=${options[key]}&`;
count += 1;
}
}
}
// replace extra space
params = params.replace(/\s/g, '');
return params.substring(0, params.length - 1);
};

module.exports = {
setAccessToken: function (token) {
Expand Down Expand Up @@ -63,5 +98,6 @@ module.exports = {
}
return url;
},
base64Encode
base64Encode,
constructAdditionalParams
};
56 changes: 19 additions & 37 deletions src/findingApi.js → src/finding.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

const urlObject = require('./buildURL');
const { getRequest } = require('./request');
const utils = require('./common-utils/index');
const FIND_ITEMS_BY_KEYWORD = 'findItemsByKeywords';
const FIND_ITEMS_BY_CATEGORY = 'findItemsByCategory';
const FIND_COMPLETED_ITEMS = 'findCompletedItems';
const FIND_ITEMS_ADV = 'findItemsAdvanced';
const FIND_EBAY_STORES = 'findItemsIneBayStores';

const findItemsByKeywords = function (options) {
if (!options) {
Expand All @@ -16,7 +18,7 @@ const findItemsByKeywords = function (options) {
// support only keyword string.
if (!options.keywords) options = { keywords: options };
options.keywords = encodeURIComponent(options.keywords);
this.options.additionalParam = constructAdditionalParams(options);
this.options.additionalParam = utils.constructAdditionalParams(options);
const url = urlObject.buildSearchUrl(this.options);
return getRequest(url).then((data) => {
return JSON.parse(data).findItemsByKeywordsResponse;
Expand Down Expand Up @@ -48,7 +50,7 @@ const findCompletedItems = function (options) {
options.keywords = encodeURIComponent(options.keywords);
}
this.options.operationName = FIND_COMPLETED_ITEMS;
this.options.additionalParam = constructAdditionalParams(options);
this.options.additionalParam = utils.constructAdditionalParams(options);
const url = urlObject.buildSearchUrl(this.options);
return getRequest(url).then((data) => {
return JSON.parse(data).findCompletedItemsResponse;
Expand All @@ -69,8 +71,9 @@ const findItemsAdvanced = function (options) {
options.keywords = encodeURIComponent(options.keywords);
}
this.options.operationName = FIND_ITEMS_ADV;
this.options.additionalParam = constructAdditionalParams(options);
this.options.additionalParam = utils.constructAdditionalParams(options);
const url = urlObject.buildSearchUrl(this.options);
console.log(url);
return getRequest(url).then((data) => {
return JSON.parse(data).findItemsAdvancedResponse;
}, console.error // eslint-disable-line no-console
Expand All @@ -96,57 +99,36 @@ const findItemsByProduct = function (options) {
if (!options.productId) throw new Error('INVALID_REQUEST_PARMS --> Product ID is required.');
let type = options.type ? options.type : 'ReferenceID';
this.options.operationName = 'findItemsByProduct';
this.options.additionalParam = constructAdditionalParams(options);
this.options.additionalParam = utils.constructAdditionalParams(options);
let url = urlObject.buildSearchUrl(this.options);
url = `${url}&productId.@type=${type}`;
console.log(url);
return getRequest(url).then((data) => {
return JSON.parse(data).findItemsByProductResponse;

}, console.error // eslint-disable-line no-console
);
};

const findItemsIneBayStores = function (options) {
if (!options) throw new Error('INVALID_REQUEST_PARMS --> Please enter the Valid input.');
if (!options.storeName) throw new Error('INVALID_REQUEST_PARMS --> Store name is required.');
this.options.operationName = FIND_EBAY_STORES;
this.options.additionalParam = utils.constructAdditionalParams(options);
console.log(urlObject.buildSearchUrl(this.options));
return getRequest(urlObject.buildSearchUrl(this.options)).then((data) => {
return JSON.parse(data).findItemsIneBayStoresResponse;

/**
* Constructs query param based on some logic to support filter and aspect_filter params.
* output will be keywords=iphone&itemFilter(0).name=Condition&itemFilter(0).value=3000&itemFilter(1).name=FreeShippingOnly&itemFilter(1).value=true&itemFilter(2).name=SoldItemsOnly&itemFilter(2).value=true
* @param {Object} options
*/
const constructAdditionalParams = (options) => {
let params = '';
let count = 0;
for (let key in options) {
if (options.hasOwnProperty(key)) {
if (key === 'entriesPerPage' || key === 'pageNumber') {
params = `${params}paginationInput.${key}=${options[key]}&`;
}
else if (key === 'keywords' || key === 'categoryId' || key === 'productId' || key === 'sortOrder') {
params = `${params}${key}=${options[key]}&`;
}
else if (key === 'affiliate') {
const innerParams = options[key];
for (let innerKey in innerParams) {
params = `${params}${key}.${innerKey}=${innerParams[innerKey]}&`;
}
}
else {
params = `${params}itemFilter(${count}).name=${key}&
itemFilter(${count}).value=${options[key]}&`;
count += 1;
}
}
}
// replace extra space
params = params.replace(/\s/g, '');
return params.substring(0, params.length - 1);
}, console.error // eslint-disable-line no-console
);
};

module.exports = {
findItemsByKeywords,
findItemsByCategory,
findCompletedItems,
constructAdditionalParams,
findItemsByProduct,
findItemsAdvanced,
findItemsIneBayStores,
getVersion
};
27 changes: 6 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
'use strict';
const ebayBuyApi = require('./buy-api');
const shoppingApi = require('./shopping');
const { getDefaultCategoryTreeId,
getCategoryTree,
getCategorySubtree,
getCategorySuggestions,
getItemAspectsForCategory } = require('./taxonomy-api');
const ebayFindingApi = require('./findingApi');
const { setAccessToken,
getAccessToken,
setHeaders,
getHeaders
} = require('./common-utils');
const taxonomyApi = require('./taxonomy-api');
const ebayFindingApi = require('./finding');
const commonUtils = require('./common-utils');
const { getSimilarItems, getMostWatchedItems } = require('./merchandising');
const { PROD_BASE_URL, SANDBOX_BASE_URL, BASE_SANDBX_SVC_URL, BASE_SVC_URL } = require('./constants');
const PROD_ENV = 'PROD';
Expand Down Expand Up @@ -42,25 +34,18 @@ function Ebay(options) {
options.baseSvcUrl = BASE_SANDBX_SVC_URL;
}
this.options = options;
setHeaders(this, options.headers);
commonUtils.setHeaders(this, options.headers);
this.options.globalID = options.countryCode || 'EBAY-US';
this.options.siteId = options.siteId || '0';
}

Ebay.prototype = {
setAccessToken,
getAccessToken,
setHeaders,
getHeaders,
getDefaultCategoryTreeId,
getCategoryTree,
getCategorySubtree,
getCategorySuggestions,
getItemAspectsForCategory,
getMostWatchedItems,
getSimilarItems,
...commonUtils,
...shoppingApi,
...ebayBuyApi,
...taxonomyApi,
...ebayFindingApi
};

Expand Down
2 changes: 1 addition & 1 deletion test/finding.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const expect = require('chai').expect;
const should = require('chai').should();
const nock = require('nock');
const Ebay = require('../src/index');
const { constructAdditionalParams } = require('../src/findingApi');
const { constructAdditionalParams } = require('../src/common-utils/index');
const nockFindingApi = nock('https://svcs.ebay.com/');

describe('test ebay finding Api', () => {
Expand Down

0 comments on commit f275408

Please sign in to comment.