Skip to content

Commit

Permalink
Integerate Shopping Api (#30)
Browse files Browse the repository at this point in the history
* added shopping api

* organize shopping url

* updated shopping api

* remove console log

* fix test case
  • Loading branch information
pajaydev authored Feb 14, 2019
1 parent 395871a commit 37ab97d
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 59 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"escape": true
},
"rules": {
"strict": 0,
"strict": [
"error",
"global"
],
"curly": 0,
"no-empty": 0,
"no-underscore-dangle": 0,
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 0 additions & 12 deletions demo/getAllCategories.js

This file was deleted.

11 changes: 0 additions & 11 deletions demo/getUserDetails.js

This file was deleted.

41 changes: 41 additions & 0 deletions demo/shopping.js
Original file line number Diff line number Diff line change
@@ -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);
});
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions src/buildURL.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';
/**
* This method is used to build the url based on
* the type of request.
Expand All @@ -13,15 +14,14 @@ 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;
base_url += "&SERVICE-VERSION=1.0.0&RESPONSE-DATA-FORMAT=JSON";
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;
},
Expand All @@ -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&";
Expand Down
2 changes: 1 addition & 1 deletion src/buy-api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

'use strict';
const makeString = require('make-string');
const { makeRequest } = require('./request');

Expand Down
37 changes: 10 additions & 27 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;
},
Expand Down Expand Up @@ -121,7 +100,11 @@ Ebay.prototype = {
getCategorySuggestions,
getItemAspectsForCategory,
getMostWatchedItems,
getSimilarItems
getSimilarItems,
getAllCategories,
getShippingCosts,
getItemStatus,
getUserDetails
};

module.exports = Ebay;
1 change: 1 addition & 0 deletions src/request.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';
let httpRequest = require("https");
const qs = require("querystring");

Expand Down
67 changes: 67 additions & 0 deletions src/shopping.js
Original file line number Diff line number Diff line change
@@ -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
};

1 change: 1 addition & 0 deletions src/taxonomy-api.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';
const { makeRequest } = require('./request');
const { upperCase } = require('./utils');

Expand Down
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

'use strict';
// to make string uppercase.
function upperCase(data) {
if (!isString(data)) data = data.toString();
Expand Down
4 changes: 2 additions & 2 deletions test/buildURL.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 37ab97d

Please sign in to comment.