Skip to content

Commit ab5a612

Browse files
authored
Merge pull request #3 from ajay2507/view-item-search
merging branch Browse Api call
2 parents 1cbeabb + f41db9f commit ab5a612

File tree

5 files changed

+132
-25
lines changed

5 files changed

+132
-25
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
2-
npm-debug.log
2+
npm-debug.log
3+
.vscode/

demo/browseApi.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
const Ebay = require('../src/index');
22
let access_token = "";
33
let ebay = new Ebay({
4-
clientID: "--Client ID ----",
5-
clientSecret: '-- Client Secret----',
4+
clientID: "-- Client ID -----",
5+
clientSecret: '-- Client Secret---',
66
body: {
77
grant_type: "client_credentials",
88
scope: 'https://api.ebay.com/oauth/api_scope'
99

1010
}
1111
});
12+
1213
// Getting access token and calling getItem method.
1314
ebay.getAccessToken()
1415
.then((data) => {
@@ -19,3 +20,47 @@ ebay.getAccessToken()
1920
})
2021
});
2122

23+
24+
// Reference ebay developer page https://developer.ebay.com/api-docs/buy/browse/resources/item/methods/getItemByLegacyId#_samples
25+
// Getting access token and calling getItemByLegacyId method.
26+
ebay.getAccessToken()
27+
.then((data) => {
28+
ebay.getItemByLegacyId({
29+
"legacyItemId": 2628001 // Get Item Details Using a Legacy ID
30+
}).then((data) => {
31+
if (!data) console.log(data);
32+
// Data is in format of JSON
33+
// To check the format of Data, Go to this url (https://jsonblob.com/56cbea67-30b8-11e8-953c-5d1886dcf4a0)
34+
});
35+
});
36+
37+
//Get Item Details Using a Legacy ID and SKU
38+
ebay.getAccessToken()
39+
.then((data) => {
40+
ebay.getItemByLegacyId({
41+
"legacyItemId": 2628001,
42+
"legacyVariationSku": "V-00031-WHM"
43+
}).then((data) => {
44+
if (!data) console.log(data);
45+
// Data is in format of JSON
46+
// To check the format of Data, Go to this url (https://jsonblob.com/56cbea67-30b8-11e8-953c-5d1886dcf4a0)
47+
});
48+
});
49+
50+
51+
//retrieves the details of the individual items in an item group
52+
// reference https://developer.ebay.com/api-docs/buy/browse/resources/item/methods/getItemsByItemGroup#uri.item_group_id
53+
ebay.getAccessToken()
54+
.then((data) => {
55+
ebay.getItemByItemGroup("151915076499").then((data) => {
56+
// Data is in format of JSON
57+
// To check the format of Data, Go to this url (https://jsonblob.com/56cbea67-30b8-11e8-953c-5d1886dcf4a0)
58+
console.log(data)
59+
}, (error) => {
60+
console.log(error);
61+
});
62+
});
63+
64+
65+
66+

src/buy-api.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
let { getRequest, makeRequest, base64Encode } = require('./request');
2+
3+
const getItem = function (itemId) {
4+
if (!itemId) throw new Error("Item Id is required");
5+
if (!this.options.access_token) throw new Error("Missing Access token, Generate access token");
6+
const auth = "Bearer " + this.options.access_token;
7+
const id = encodeURIComponent(itemId);
8+
return makeRequest('api.ebay.com', `/buy/browse/v1/item/${id}`, 'GET', this.options.body, auth).then((result) => {
9+
console.log("Success");
10+
let resultJSON = JSON.parse(result);
11+
//this.setAccessToken(resultJSON);
12+
return resultJSON;
13+
});
14+
};
15+
16+
const getItemByLegacyId = function (legacyOptions) {
17+
console.log(legacyOptions);
18+
if (!legacyOptions) throw new Error("Error Required input to get Items By LegacyID");
19+
if (!this.options.access_token) throw new Error("Missing Access token, Generate access token");
20+
if (!legacyOptions.legacyItemId) throw new Error("Error Legacy Item Id is required");
21+
const auth = "Bearer " + this.options.access_token;
22+
let param = "legacy_item_id=" + legacyOptions.legacyItemId;
23+
param += legacyOptions.legacyVariationSku ? "&legacy_variation_sku=" + legacyOptions.legacyVariationSku : '';
24+
25+
return new promise
26+
27+
makeRequest('api.ebay.com', `/buy/browse/v1/item/get_item_by_legacy_id?${param}`, 'GET', this.options.body, auth).then((result) => {
28+
let resultJSON = JSON.parse(result);
29+
//this.setAccessToken(resultJSON);
30+
return resultJSON;
31+
}).then((error) => {
32+
console.log(error.errors);
33+
console.log("Error Occurred ===> " + error.errors[0].message);
34+
});
35+
};
36+
37+
const getItemByItemGroup = function (itemGroupId) {
38+
if (typeof itemGroupId == "object") throw new Error("Expecting String or number (Item group id)");
39+
if (!itemGroupId) throw new Error("Error Item Group ID is required");
40+
if (!this.options.access_token) throw new Error("Missing Access token, Generate access token");
41+
const auth = "Bearer " + this.options.access_token;
42+
return new Promise((resolve, reject) => {
43+
makeRequest('api.ebay.com', `/buy/browse/v1/item/get_items_by_item_group?item_group_id=${itemGroupId}`, 'GET', this.options.body, auth).then((result) => {
44+
resolve(JSON.parse(result));
45+
});
46+
})
47+
};
48+
49+
const searchItems = function (searchConfig) {
50+
if (!searchConfig) throw new Error("Error --> Missing or invalid input parameter to search");
51+
if (!searchConfig.keyword || !searchConfig.categoryId) throw new Error("Error --> Keyword or category id is required in query param");
52+
if (!this.options.access_token) throw new Error("Error -->Missing Access token, Generate access token");
53+
if (searchConfig.fieldgroups.length > 0 && !Array.isArray(searchConfig.fieldgroups)) throw new Error("Error -->Field groups should be an array");
54+
const auth = "Bearer " + this.options.access_token;
55+
let queryParam = searchConfig.keyword ? "q=" + searchConfig.keyword + "&" : "";
56+
queryParam = queryParam + searchConfig.categoryId ? "category_ids=" + searchConfig.categoryId + "&" : '';
57+
queryParam = queryParam + searchConfig.limit ? "limit=" + searchConfig.limit + "&" : "";
58+
queryParam = queryParam + searchConfig.fieldgroups.length > 0 ? "fieldgroups=" + searchConfig.fieldgroups.toString() + "&" : "";
59+
return new Promise((resolve, reject) => {
60+
makeRequest('api.ebay.com', `buy/browse/v1/item_summary/search?${queryparam}`, 'GET', this.options.body, auth).then((result) => {
61+
resolve(JSON.parse(result));
62+
});
63+
})
64+
};
65+
66+
module.exports = {
67+
getItem,
68+
getItemByLegacyId,
69+
getItemByItemGroup,
70+
searchItems
71+
}

src/index.js

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
//let baseURL = "http://svcs.ebay.com/services/search/FindingService/v1";
22
let { getRequest, makeRequest, base64Encode } = require('./request');
3+
let { getItem,
4+
getItemByLegacyId,
5+
getItemByItemGroup,
6+
searchItems } = require('./buy-api');
37
let urlObject = require('./buildURL');
48

59
function Ebay(options) {
@@ -92,23 +96,8 @@ Ebay.prototype = {
9296
console.log(error);
9397
})
9498
},
95-
96-
getItem: function (itemId) {
97-
console.log(this.options);
98-
if (!itemId) throw new Error("Item Id is required");
99-
if (!this.options.access_token) throw new Error("Missing Access token, Generate access token");
100-
const auth = "Bearer " + this.options.access_token;
101-
const id = encodeURIComponent(itemId);
102-
return makeRequest('api.ebay.com', `/buy/browse/v1/item/${id}`, 'GET', this.options.body, auth).then((result) => {
103-
console.log("Success");
104-
let resultJSON = JSON.parse(result);
105-
//this.setAccessToken(resultJSON);
106-
return resultJSON;
107-
});
108-
},
109-
11099
setAccessToken: function (token) {
111-
console.log("inside access tokeeeeee" + token);
100+
112101
this.options.access_token = token;
113102
},
114103

@@ -118,17 +107,18 @@ Ebay.prototype = {
118107
if (!this.options.body) throw new Error("Missing Body, required Grant type");
119108
const encodedStr = base64Encode(this.options.clientID + ":" + this.options.clientSecret);
120109
let self = this;
121-
console.log(this.options.body);
122110
const auth = "Basic " + encodedStr;
123111
return makeRequest('api.ebay.com', '/identity/v1/oauth2/token', 'POST', this.options.body, auth).then((result) => {
124-
console.log("Successssssssss");
125112
let resultJSON = JSON.parse(result);
126-
// console.log(this);
113+
console.log(resultJSON);
127114
self.setAccessToken(resultJSON.access_token);
128115
return resultJSON;
129116
});
130-
}
131-
117+
},
118+
getItem,
119+
getItemByLegacyId,
120+
getItemByItemGroup,
121+
searchItems
132122
};
133123

134124
module.exports = Ebay;

src/request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ let getRequest = function getRequest(url) {
2121

2222
let makeRequest = function postRequest(hostName, endpoint, methodName, data, token) {
2323
methodName == "POST" ? dataString = qs.stringify(data) : '';
24-
// console.log(dataString);
24+
console.log(endpoint);
2525
const options = {
2626
"hostname": hostName,
2727
"path": endpoint,

0 commit comments

Comments
 (0)