From 37ab97d3df85ae036d7b5d5bcf012443d66bdee6 Mon Sep 17 00:00:00 2001 From: Ajaykumar Date: Thu, 14 Feb 2019 04:58:02 -0800 Subject: [PATCH] Integerate Shopping Api (#30) * added shopping api * organize shopping url * updated shopping api * remove console log * fix test case --- .eslintrc | 5 ++- README.md | 1 + demo/getAllCategories.js | 12 ------- demo/getUserDetails.js | 11 ------- demo/shopping.js | 41 ++++++++++++++++++++++++ package.json | 1 - src/buildURL.js | 6 ++-- src/buy-api.js | 2 +- src/index.js | 37 ++++++---------------- src/request.js | 1 + src/shopping.js | 67 ++++++++++++++++++++++++++++++++++++++++ src/taxonomy-api.js | 1 + src/utils.js | 2 +- test/buildURL.test.js | 4 +-- 14 files changed, 132 insertions(+), 59 deletions(-) delete mode 100644 demo/getAllCategories.js delete mode 100644 demo/getUserDetails.js create mode 100644 demo/shopping.js create mode 100644 src/shopping.js diff --git a/.eslintrc b/.eslintrc index 5f2f808..f079835 100644 --- a/.eslintrc +++ b/.eslintrc @@ -9,7 +9,10 @@ "escape": true }, "rules": { - "strict": 0, + "strict": [ + "error", + "global" + ], "curly": 0, "no-empty": 0, "no-underscore-dangle": 0, diff --git a/README.md b/README.md index 93ee4be..c7e0629 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ The intent is to simplify the request process by handling the tedious logic. It' * [Search Items Based on Price and Condition](#searchitemsbyfilter) * [Taxonomy Api(getDefaultCategoryTreeId, getCategoryTree, getCategorySubtree, getCategorySuggestions)](#taxonomyapi) * [Get Most Watched item, Get Most Similar Items](#merchandisingapi) + * [Get All Categories, GetUserDetails, GetShippingCost, GetItemStatus](#shoppingapi) * [Test](#test) * [Issues](#issues) * [Contribution](#contribution) diff --git a/demo/getAllCategories.js b/demo/getAllCategories.js deleted file mode 100644 index dc82f26..0000000 --- a/demo/getAllCategories.js +++ /dev/null @@ -1,12 +0,0 @@ -const Ebay = require('../src/index'); - -let ebay = new Ebay({ - clientID: "-- Client App id ----", - details: "childCategories" //optional parameter -}); - -ebay.getAllCategories().then((data) => { - console.log(data); //extract data.CategoryArray -}, (error) => { - console.log(error); -}) \ No newline at end of file diff --git a/demo/getUserDetails.js b/demo/getUserDetails.js deleted file mode 100644 index 93ee91e..0000000 --- a/demo/getUserDetails.js +++ /dev/null @@ -1,11 +0,0 @@ -const Ebay = require('../src/index'); - -let ebay = new Ebay({ - clientID: "-- Client App ID ----", - details: true // To require detailed info or put false -}); -ebay.getUserDetails("ajaykumapratha_0").then((data) => { - console.log(data); -}, (error) => { - console.log(error); -}); \ No newline at end of file diff --git a/demo/shopping.js b/demo/shopping.js new file mode 100644 index 0000000..7ecc187 --- /dev/null +++ b/demo/shopping.js @@ -0,0 +1,41 @@ +'use strict'; + +const Ebay = require('../src/index'); + +let ebay = new Ebay({ + clientID: "--AppID/ClientID--", +}); + +ebay.getAllCategories('1234').then((data) => { + console.log(data); //extract data.CategoryArray +}, (error) => { + console.log(error); +}); + + +// 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) => { + console.log(error); +}); + + +// Get Item Status +// https://developer.ebay.com/devzone/shopping/docs/callref/GetItemStatus.html +ebay.getItemStatus(["153265274986", "153265274986"]).then((data) => { + console.log(data); +}, (error) => { + console.log(error); +}); + +// https://developer.ebay.com/devzone/shopping/docs/callref/GetShippingCosts.html +ebay.getShippingCosts({ + itemId: "153265274986", destCountryCode: 'US', + destPostalCode: '95128' +}).then((data) => { + console.log(data); +}, (error) => { + console.log(error); +}); \ No newline at end of file diff --git a/package.json b/package.json index d02f3dc..78edf8a 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,6 @@ "devDependencies": { "chai": "^4.1.2", "eslint": "^5.8.0", - "make-string": "^1.0.2", "mocha": "^5.0.1", "nock": "^9.2.3", "sinon": "^4.4.5" diff --git a/src/buildURL.js b/src/buildURL.js index 6b506b7..72feadd 100644 --- a/src/buildURL.js +++ b/src/buildURL.js @@ -1,3 +1,4 @@ +'use strict'; /** * This method is used to build the url based on * the type of request. @@ -13,7 +14,6 @@ const buildURL = { * @private */ buildSearchUrl(options) { - console.log(options); let base_url = `http://${options.baseSvcUrl}/services/search/FindingService/v1?`; base_url += "SECURITY-APPNAME=" + options.clientID; base_url += "&OPERATION-NAME=" + options.operationName; @@ -21,7 +21,7 @@ const buildURL = { base_url += options.param ? "&" + options.param + "=" + options.name : ''; base_url += options.limit ? "&paginationInput.entriesPerPage=" + options.limit : ''; base_url += options.globalID ? "&GLOBAL-ID=" + options.globalID : ''; - base_url += options.pageNumber ? "&paginationInput.pageNumber=" + options.pageNumber: ''; + base_url += options.pageNumber ? "&paginationInput.pageNumber=" + options.pageNumber : ''; return base_url; }, @@ -34,7 +34,7 @@ const buildURL = { * @private */ buildShoppingUrl(options) { - let base_url = `http://${options.baseUrl}/Shopping?`; + let base_url = `https://${options.baseUrl}/Shopping?`; base_url += "appid=" + options.clientID; base_url += "&callname=" + options.operationName; base_url += "&version=967&siteid=0&responseencoding=JSON&"; diff --git a/src/buy-api.js b/src/buy-api.js index 99abc2c..4927074 100644 --- a/src/buy-api.js +++ b/src/buy-api.js @@ -1,4 +1,4 @@ - +'use strict'; const makeString = require('make-string'); const { makeRequest } = require('./request'); diff --git a/src/index.js b/src/index.js index e24f761..590ecaf 100644 --- a/src/index.js +++ b/src/index.js @@ -1,9 +1,13 @@ -//let baseURL = "http://svcs.ebay.com/services/search/FindingService/v1"; +'use strict'; const { getRequest, makeRequest, base64Encode } = require('./request'); const { getItem, getItemByLegacyId, getItemByItemGroup, searchItems } = require('./buy-api'); +const { getAllCategories, + getShippingCosts, + getItemStatus, + getUserDetails } = require('./shopping'); const { getDefaultCategoryTreeId, getCategoryTree, getCategorySubtree, @@ -60,40 +64,15 @@ Ebay.prototype = { ) }, - 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) => { - return JSON.parse(data); - }, console.error - ) - }, - getVersion: function () { this.options.operationName = "getVersion"; const url = urlObject.buildSearchUrl(this.options); - console.log(url); return getRequest(url).then((data) => { return JSON.parse(data)["getVersionResponse"][0]; }, console.error ) }, - getUserDetails: function (userID) { - if (!userID) throw new Error("User ID is null or invalid"); - this.options.operationName = "GetUserProfile"; - this.options.param = "UserID"; - this.options.name = userID; - this.options.includeSelector = this.options.details ? "Details" : null; - const url = urlObject.buildShoppingUrl(this.options); - return getRequest(url).then((data) => { - return JSON.parse(data); - }, console.error - ) - }, - setAccessToken: function (token) { this.options.access_token = token; }, @@ -121,7 +100,11 @@ Ebay.prototype = { getCategorySuggestions, getItemAspectsForCategory, getMostWatchedItems, - getSimilarItems + getSimilarItems, + getAllCategories, + getShippingCosts, + getItemStatus, + getUserDetails }; module.exports = Ebay; diff --git a/src/request.js b/src/request.js index eec41dc..b8d86ba 100644 --- a/src/request.js +++ b/src/request.js @@ -1,3 +1,4 @@ +'use strict'; let httpRequest = require("https"); const qs = require("querystring"); diff --git a/src/shopping.js b/src/shopping.js new file mode 100644 index 0000000..db12604 --- /dev/null +++ b/src/shopping.js @@ -0,0 +1,67 @@ +'use strict'; + +const { getRequest } = require('./request'); +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) => { + return JSON.parse(data); + }, console.error + ) +}; + +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) => { + return JSON.parse(data); + }, console.error + ) +}; + +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) => { + return JSON.parse(data); + }, console.error + ) +}; + +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; + return getRequest(url).then((data) => { + return JSON.parse(data); + }, console.error + ) +}; + +module.exports = { + getAllCategories, + getUserDetails, + getItemStatus, + getShippingCosts +}; + diff --git a/src/taxonomy-api.js b/src/taxonomy-api.js index d372a4f..655f65f 100644 --- a/src/taxonomy-api.js +++ b/src/taxonomy-api.js @@ -1,3 +1,4 @@ +'use strict'; const { makeRequest } = require('./request'); const { upperCase } = require('./utils'); diff --git a/src/utils.js b/src/utils.js index 7feaf9a..699af4e 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,4 +1,4 @@ - +'use strict'; // to make string uppercase. function upperCase(data) { if (!isString(data)) data = data.toString(); diff --git a/test/buildURL.test.js b/test/buildURL.test.js index 3fa379f..603c1c0 100644 --- a/test/buildURL.test.js +++ b/test/buildURL.test.js @@ -21,7 +21,7 @@ describe("test building url methods", () => { }); it("test Shopping url without selector", () => { - let expected_search_url = "http://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&keywords=iphone" let options = { name: "iphone", operationName: "demoShoppingName", @@ -33,7 +33,7 @@ describe("test building url methods", () => { }); it("test Shopping url including selector", () => { - let expected_search_url = "http://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&keywords=iphone&IncludeSelector=true" let options = { name: "iphone", operationName: "demoShoppingName",