Skip to content

Commit 81de134

Browse files
authored
Ebay Merchandising API (#22)
* adding merchandising api support * added demo getMostWatchedItems * fix lint * added getSimilar items api * merge with master
1 parent da795ac commit 81de134

File tree

8 files changed

+127
-35
lines changed

8 files changed

+127
-35
lines changed

.eslintrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@
3636
"functions": false
3737
}
3838
],
39+
"prefer-const": [
40+
"error",
41+
{
42+
"destructuring": "any",
43+
"ignoreReadBeforeAssign": false
44+
}
45+
],
3946
"no-var": "error",
4047
"no-template-curly-in-string": "error"
4148
}

demo/merchandisingApi.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const Ebay = require('../src/index');
2+
let ebay = new Ebay({
3+
clientID: "-- client ID----",
4+
clientSecret: '-- client secret---',
5+
body: {
6+
grant_type: "client_credentials",
7+
scope: 'https://api.ebay.com/oauth/api_scope'
8+
9+
}
10+
});
11+
12+
13+
ebay.getMostWatchedItems({
14+
maxResults: 3, // optional
15+
categoryId: 267 // optional
16+
}).then((data) => {
17+
if (data.errorMessage) {
18+
console.log("Error:" + data.errorMessage);
19+
}
20+
console.log(JSON.stringify(data));
21+
});
22+
23+
24+
ebay.getSimilarItems({
25+
maxResults: 3, // optional
26+
itemId=280254552262 // optional
27+
}).then((data) => {
28+
if (data.errorMessage) {
29+
console.log("Error:" + data.errorMessage);
30+
}
31+
console.log(JSON.stringify(data));
32+
// JSON format of similar items.
33+
});

demo/searchApi.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const Ebay = require('../src/index');
22
let ebay = new Ebay({
3-
clientID: "-- client id --",
4-
clientSecret: '-- client secret ---',
3+
clientID: "-- client id ---",
4+
clientSecret: ' -- client secret -- ',
55
body: {
66
grant_type: "client_credentials",
77
scope: 'https://api.ebay.com/oauth/api_scope'
@@ -75,7 +75,7 @@ ebay.getAccessToken()
7575
ebay.searchItems({
7676
keyword: "iphone",
7777
limit: 3,
78-
filter: { price: "[300..800]", priceCurrency: "USD", conditions: "{NEW}" }
78+
filter: { price: "[300..800]", priceCurrency: "USD", conditions: "NEW" }
7979
}).then((data) => {
8080
console.log(data);
8181
// Data is in format of JSON

src/buy-api.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1+
12
const makeString = require('make-string');
2-
let { makeRequest } = require('./request');
3+
const { makeRequest } = require('./request');
34

45
const getItem = function (itemId) {
56
if (!itemId) throw new Error("Item Id is required");
67
if (!this.options.access_token) throw new Error("Missing Access token, Generate access token");
78
const auth = "Bearer " + this.options.access_token;
89
const id = encodeURIComponent(itemId);
910
return makeRequest('api.ebay.com', `/buy/browse/v1/item/${id}`, 'GET', this.options.body, auth).then((result) => {
10-
let resultJSON = JSON.parse(result);
11-
//this.setAccessToken(resultJSON);
12-
return resultJSON;
11+
return JSON.parse(result);
1312
});
1413
};
1514

@@ -22,8 +21,7 @@ const getItemByLegacyId = function (legacyOptions) {
2221
param += legacyOptions.legacyVariationSku ? "&legacy_variation_sku=" + legacyOptions.legacyVariationSku : '';
2322

2423
makeRequest('api.ebay.com', `/buy/browse/v1/item/get_item_by_legacy_id?${param}`, 'GET', this.options.body, auth).then((result) => {
25-
let resultJSON = JSON.parse(result);
26-
return resultJSON;
24+
return JSON.parse(result);
2725
}).then((error) => {
2826
console.log(error.errors);
2927
console.log("Error Occurred ===> " + error.errors[0].message);

src/constants.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
module.exports = {
4+
BASE_SVC_URL: "svcs.ebay.com",
5+
MERCH_SRVC_NAME: "MerchandisingService"
6+
}

src/index.js

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//let baseURL = "http://svcs.ebay.com/services/search/FindingService/v1";
2-
let { getRequest, makeRequest, base64Encode } = require('./request');
3-
let { getItem,
2+
const { getRequest, makeRequest, base64Encode } = require('./request');
3+
const { getItem,
44
getItemByLegacyId,
55
getItemByItemGroup,
66
searchItems } = require('./buy-api');
@@ -9,7 +9,8 @@ const { getDefaultCategoryTreeId,
99
getCategorySubtree,
1010
getCategorySuggestions,
1111
getItemAspectsForCategory } = require('./taxonomy-api');
12-
let urlObject = require('./buildURL');
12+
const { getSimilarItems, getMostWatchedItems } = require('./merchandising');
13+
const urlObject = require('./buildURL');
1314

1415
function Ebay(options) {
1516

@@ -27,10 +28,9 @@ Ebay.prototype = {
2728
this.options.name = keyword;
2829
this.options.operationName = "findItemsByKeywords";
2930
this.options.param = "keywords";
30-
let url = urlObject.buildSearchUrl(this.options);
31+
const url = urlObject.buildSearchUrl(this.options);
3132
return getRequest(url).then((data) => {
32-
let result = JSON.parse(data);
33-
return result["findItemsByKeywordsResponse"];
33+
return JSON.parse(data)["findItemsByKeywordsResponse"];
3434

3535
}, console.error
3636
)
@@ -41,10 +41,9 @@ Ebay.prototype = {
4141
this.options.name = categoryID;
4242
this.options.operationName = "findItemsByCategory";
4343
this.options.param = "categoryId";
44-
let url = urlObject.buildSearchUrl(this.options);
44+
const url = urlObject.buildSearchUrl(this.options);
4545
return getRequest(url).then((data) => {
46-
let result = JSON.parse(data);
47-
return result["findItemsByCategoryResponse"];
46+
return JSON.parse(data)["findItemsByCategoryResponse"];
4847

4948
}, console.error
5049

@@ -55,20 +54,18 @@ Ebay.prototype = {
5554
this.options.name = categoryID ? categoryID : -1;
5655
this.options.operationName = "GetCategoryInfo";
5756
this.options.param = "CategoryID";
58-
let url = urlObject.buildShoppingUrl(this.options);
57+
const url = urlObject.buildShoppingUrl(this.options);
5958
return getRequest(url).then((data) => {
60-
let result = JSON.parse(data);
61-
return result;
59+
return JSON.parse(data);
6260
}, console.error
6361
)
6462
},
6563

6664
getVersion: function () {
6765
this.options.operationName = "getVersion";
68-
let url = urlObject.buildSearchUrl(this.options);
66+
const url = urlObject.buildSearchUrl(this.options);
6967
return getRequest(url).then((data) => {
70-
let result = JSON.parse(data);
71-
return result["getVersionResponse"][0];
68+
return JSON.parse(data)["getVersionResponse"][0];
7269
}, console.error
7370
)
7471
},
@@ -79,16 +76,14 @@ Ebay.prototype = {
7976
this.options.param = "UserID";
8077
this.options.name = userID;
8178
this.options.includeSelector = this.options.details ? "Details" : null;
82-
let url = urlObject.buildShoppingUrl(this.options);
79+
const url = urlObject.buildShoppingUrl(this.options);
8380
return getRequest(url).then((data) => {
84-
let result = JSON.parse(data);
85-
return result;
81+
return JSON.parse(data);
8682
}, console.error
8783
)
8884
},
8985

9086
setAccessToken: function (token) {
91-
9287
this.options.access_token = token;
9388
},
9489

@@ -97,10 +92,10 @@ Ebay.prototype = {
9792
if (!this.options.clientSecret) throw new Error("Missing Client Secret or Cert Id");
9893
if (!this.options.body) throw new Error("Missing Body, required Grant type");
9994
const encodedStr = base64Encode(this.options.clientID + ":" + this.options.clientSecret);
100-
let self = this;
95+
const self = this;
10196
const auth = "Basic " + encodedStr;
10297
return makeRequest('api.ebay.com', '/identity/v1/oauth2/token', 'POST', this.options.body, auth).then((result) => {
103-
let resultJSON = JSON.parse(result);
98+
const resultJSON = JSON.parse(result);
10499
self.setAccessToken(resultJSON.access_token);
105100
return resultJSON;
106101
});
@@ -113,7 +108,9 @@ Ebay.prototype = {
113108
getCategoryTree,
114109
getCategorySubtree,
115110
getCategorySuggestions,
116-
getItemAspectsForCategory
111+
getItemAspectsForCategory,
112+
getMostWatchedItems,
113+
getSimilarItems
117114
};
118115

119116
module.exports = Ebay;

src/merchandising.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
const { getRequest } = require('./request');
4+
const { BASE_SVC_URL, MERCH_SRVC_NAME } = require('./constants');
5+
6+
//https://developer.ebay.com/devzone/merchandising/docs/CallRef/getMostWatchedItems.html#Samples
7+
8+
/**
9+
* @method getMostWatchedItems {Function}
10+
* Add interest and excitement for buyers by showing them what other people are watching.
11+
* @param {String} categoryId (optional)
12+
*/
13+
const getMostWatchedItems = function (merchOptions) {
14+
console.log(this);
15+
if (!this.options.clientID) throw new Error("Missing App id or client id");
16+
let url = '';
17+
if (merchOptions && merchOptions.categoryId != undefined) url = `&categoryId=${merchOptions.categoryId}`;
18+
if (merchOptions && merchOptions.maxResults) url = `&maxResults=${merchOptions.maxResults}`;
19+
return getRequest(`http://${BASE_SVC_URL}/${MERCH_SRVC_NAME}?OPERATION-NAME=getMostWatchedItems&SERVICE-NAME=${MERCH_SRVC_NAME}&SERVICE-VERSION=1.1.0&CONSUMER-ID=${this.options.clientID}&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD${url}`).then((result) => {
20+
return JSON.parse(result);
21+
}).catch((error) => {
22+
console.log(error);
23+
});
24+
};
25+
26+
27+
/**
28+
* @method getSimilarItems {Function}
29+
* Gets similar Items based on the Item id provided.
30+
* @param {String} categoryId (optional)
31+
*/
32+
const getSimilarItems = function (merchOptions) {
33+
if (!this.options.clientID) throw new Error("Missing App id or client id");
34+
let url = '';
35+
if (merchOptions && merchOptions.itemId) url = `&itemId=${merchOptions.itemId}`;
36+
if (merchOptions && merchOptions.maxResults) url = `${url}&maxResults=${merchOptions.maxResults}`;
37+
return getRequest(`http://${BASE_SVC_URL}/${MERCH_SRVC_NAME}?OPERATION-NAME=getSimilarItems&SERVICE-NAME=${MERCH_SRVC_NAME}&SERVICE-VERSION=1.1.0&CONSUMER-ID=${this.options.clientID}&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD${url}`).then((result) => {
38+
return JSON.parse(result);
39+
}).catch((error) => {
40+
console.log(error);
41+
});
42+
};
43+
44+
module.exports = {
45+
getMostWatchedItems,
46+
getSimilarItems
47+
}

src/request.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
let httpRequest = require("https");
22
const qs = require("querystring");
33

4-
let getRequest = function getRequest(url) {
4+
const getRequest = (url) => {
55
if (url.includes("http://")) httpRequest = require("http");
66
return new Promise(function (resolve, reject) {
77
httpRequest.get(url, res => {
@@ -11,6 +11,10 @@ let getRequest = function getRequest(url) {
1111
body += data;
1212
});
1313
res.on("end", () => {
14+
console.log(JSON.parse(body).errorMessage);
15+
if (JSON.parse(body).errorMessage) {
16+
reject(body);
17+
}
1418
resolve(body);
1519
});
1620
res.on("error", (error) => {
@@ -22,7 +26,7 @@ let getRequest = function getRequest(url) {
2226

2327
}
2428

25-
let makeRequest = function postRequest(hostName, endpoint, methodName, data, token) {
29+
const makeRequest = function postRequest(hostName, endpoint, methodName, data, token) {
2630
methodName == "POST" ? dataString = qs.stringify(data) : '';
2731
// console.log(endpoint);
2832
const options = {
@@ -60,8 +64,8 @@ let makeRequest = function postRequest(hostName, endpoint, methodName, data, tok
6064
}
6165

6266

63-
let base64Encode = (encodeData) => {
64-
let buff = new Buffer(encodeData);
67+
const base64Encode = (encodeData) => {
68+
const buff = new Buffer(encodeData);
6569
return buff.toString('base64');
6670
}
6771

0 commit comments

Comments
 (0)