Skip to content

Commit 2d7d4c7

Browse files
committed
using build url method to fetch data
1 parent da2ee80 commit 2d7d4c7

4 files changed

Lines changed: 49 additions & 41 deletions

File tree

demo/getAllCategories.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const Ebay = require('../src/index');
22

33
let ebay = new Ebay({
4-
clientID: "--Enter your Client ID"
4+
clientID: "Ajaykuma-nodeapi-PRD-bf1a91299-ed4deb45"
55
});
66

77
ebay.getAllCategories().then((data) => {

src/buildURL.js

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,48 @@
1-
function ebayURL() {
1+
/**
2+
* This method is used to build the url based on
3+
* the type of request.
4+
*/
25

3-
}
4-
5-
ebayURL.prototype = {
6-
buildSearchUrl: function (param, data) {
6+
const buildURL = {
7+
/**
8+
* Builds the findings(search) URL.
9+
*
10+
* @param {Object} options
11+
* @param {String} data
12+
* @return {String} build url
13+
* @private
14+
*/
15+
buildSearchUrl(options) {
716
let base_url = "http://svcs.ebay.com/services/search/FindingService/v1?";
17+
base_url += "SECURITY-APPNAME=" + options.clientID;
18+
base_url += "&OPERATION-NAME=" + options.operationName;
19+
base_url += "&SERVICE-VERSION=1.0.0&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD&";
20+
base_url += options.param + "=" + options.name;
21+
base_url += "&paginationInput.entriesPerPage=" + options.limit;
22+
base_url += "&GLOBAL-ID=" + options.globalID;
23+
24+
return base_url;
25+
},
26+
27+
/**
28+
* Builds the Shopping(open api) URL.
29+
*
30+
* @param {String} param
31+
* @param {String} data
32+
* @return {String} build url
33+
* @private
34+
*/
35+
buildShoppingUrl() {
36+
let base_url = "http://open.api.ebay.com/Shopping?";
837
base_url += "SECURITY-APPNAME=" + this.options.clientID;
938
base_url += "&OPERATION-NAME=" + configData["findItemsByKeywords"]["OPERATION-NAME"];
10-
base_url += "&SERVICE-VERSION=1.0.0&RESPONSE-DATA-FORMAT=JSON";
11-
base_url += "&callback=" + configData["findItemsByKeywords"]["callback"] + "&REST-PAYLOAD&";
39+
base_url += "&SERVICE-VERSION=1.0.0&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD&";
1240
base_url += param + "=" + data;
1341
base_url += "&paginationInput.entriesPerPage=" + this.options.limit;
1442
base_url += "&GLOBAL-ID=" + this.options.globalID;
15-
1643
return base_url;
17-
},
18-
19-
buildShoppingUrl: function () {
20-
2144
}
22-
}
45+
};
46+
47+
module.exports = buildURL;
2348

src/config.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/index.js

Lines changed: 10 additions & 19 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 configData = require('./config');
32
let makeRequest = require('./request');
3+
let urlObject = require('./buildURL');
44

55
function Ebay(options) {
66
console.log(options);
@@ -13,25 +13,16 @@ function Ebay(options) {
1313
}
1414

1515
Ebay.prototype = {
16-
buildAPIUrl: function (keyword) {
17-
let base_url = "http://svcs.ebay.com/services/search/FindingService/v1?";
18-
base_url += "SECURITY-APPNAME=" + this.options.clientID;
19-
base_url += "&OPERATION-NAME=" + configData["findItemsByKeywords"]["OPERATION-NAME"];
20-
base_url += "&SERVICE-VERSION=1.0.0&RESPONSE-DATA-FORMAT=JSON";
21-
base_url += "&REST-PAYLOAD&keywords=" + keyword;
22-
base_url += "&paginationInput.entriesPerPage=" + this.options.limit;
23-
base_url += "&GLOBAL-ID=" + this.options.globalID;
24-
25-
return base_url;
26-
},
2716

2817
findItemsByKeywords: function (keyword) {
2918
this.options.name = keyword;
30-
let url = this.buildAPIUrl(keyword);
31-
//console.log(url);
32-
return makeRequest(url).then((result) => {
33-
console.log(result);
34-
return result;
19+
this.options.operationName = "findItemsByKeywords";
20+
this.options.param = "keywords";
21+
let url = urlObject.buildSearchUrl(this.options, keyword);
22+
console.log(url);
23+
return makeRequest(url).then((data) => {
24+
let result = JSON.parse(data);
25+
return result["findItemsByKeywordsResponse"];
3526

3627
}, (error) => {
3728
console.log(error);
@@ -42,8 +33,8 @@ Ebay.prototype = {
4233
getAllCategories: function () {
4334
//console.log(url);
4435
return makeRequest("http://open.api.ebay.com/Shopping?callname=GetCategoryInfo&appid=" + this.options.clientID + "&version=967&siteid=0&CategoryID=-1&responseencoding=JSON&IncludeSelector=ChildCategories").then((data) => {
45-
// console.log(data);
46-
return data;
36+
let result = JSON.parse(data);
37+
return result;
4738
}, (error) => {
4839
console.log(error);
4940
})

0 commit comments

Comments
 (0)