Skip to content

Commit

Permalink
Merge pull request #3 from ajay2507/view-item-search
Browse files Browse the repository at this point in the history
merging branch Browse Api call
  • Loading branch information
pajaydev authored Apr 15, 2018
2 parents 1cbeabb + f41db9f commit ab5a612
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
npm-debug.log
npm-debug.log
.vscode/
49 changes: 47 additions & 2 deletions demo/browseApi.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const Ebay = require('../src/index');
let access_token = "";
let ebay = new Ebay({
clientID: "--Client ID ----",
clientSecret: '-- Client Secret----',
clientID: "-- Client ID -----",
clientSecret: '-- Client Secret---',
body: {
grant_type: "client_credentials",
scope: 'https://api.ebay.com/oauth/api_scope'

}
});

// Getting access token and calling getItem method.
ebay.getAccessToken()
.then((data) => {
Expand All @@ -19,3 +20,47 @@ ebay.getAccessToken()
})
});


// Reference ebay developer page https://developer.ebay.com/api-docs/buy/browse/resources/item/methods/getItemByLegacyId#_samples
// Getting access token and calling getItemByLegacyId method.
ebay.getAccessToken()
.then((data) => {
ebay.getItemByLegacyId({
"legacyItemId": 2628001 // Get Item Details Using a Legacy ID
}).then((data) => {
if (!data) console.log(data);
// Data is in format of JSON
// To check the format of Data, Go to this url (https://jsonblob.com/56cbea67-30b8-11e8-953c-5d1886dcf4a0)
});
});

//Get Item Details Using a Legacy ID and SKU
ebay.getAccessToken()
.then((data) => {
ebay.getItemByLegacyId({
"legacyItemId": 2628001,
"legacyVariationSku": "V-00031-WHM"
}).then((data) => {
if (!data) console.log(data);
// Data is in format of JSON
// To check the format of Data, Go to this url (https://jsonblob.com/56cbea67-30b8-11e8-953c-5d1886dcf4a0)
});
});


//retrieves the details of the individual items in an item group
// reference https://developer.ebay.com/api-docs/buy/browse/resources/item/methods/getItemsByItemGroup#uri.item_group_id
ebay.getAccessToken()
.then((data) => {
ebay.getItemByItemGroup("151915076499").then((data) => {
// Data is in format of JSON
// To check the format of Data, Go to this url (https://jsonblob.com/56cbea67-30b8-11e8-953c-5d1886dcf4a0)
console.log(data)
}, (error) => {
console.log(error);
});
});




71 changes: 71 additions & 0 deletions src/buy-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
let { getRequest, makeRequest, base64Encode } = require('./request');

const getItem = function (itemId) {
if (!itemId) throw new Error("Item Id is required");
if (!this.options.access_token) throw new Error("Missing Access token, Generate access token");
const auth = "Bearer " + this.options.access_token;
const id = encodeURIComponent(itemId);
return makeRequest('api.ebay.com', `/buy/browse/v1/item/${id}`, 'GET', this.options.body, auth).then((result) => {
console.log("Success");
let resultJSON = JSON.parse(result);
//this.setAccessToken(resultJSON);
return resultJSON;
});
};

const getItemByLegacyId = function (legacyOptions) {
console.log(legacyOptions);
if (!legacyOptions) throw new Error("Error Required input to get Items By LegacyID");
if (!this.options.access_token) throw new Error("Missing Access token, Generate access token");
if (!legacyOptions.legacyItemId) throw new Error("Error Legacy Item Id is required");
const auth = "Bearer " + this.options.access_token;
let param = "legacy_item_id=" + legacyOptions.legacyItemId;
param += legacyOptions.legacyVariationSku ? "&legacy_variation_sku=" + legacyOptions.legacyVariationSku : '';

return new promise

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

const getItemByItemGroup = function (itemGroupId) {
if (typeof itemGroupId == "object") throw new Error("Expecting String or number (Item group id)");
if (!itemGroupId) throw new Error("Error Item Group ID is required");
if (!this.options.access_token) throw new Error("Missing Access token, Generate access token");
const auth = "Bearer " + this.options.access_token;
return new Promise((resolve, reject) => {
makeRequest('api.ebay.com', `/buy/browse/v1/item/get_items_by_item_group?item_group_id=${itemGroupId}`, 'GET', this.options.body, auth).then((result) => {
resolve(JSON.parse(result));
});
})
};

const searchItems = function (searchConfig) {
if (!searchConfig) throw new Error("Error --> Missing or invalid input parameter to search");
if (!searchConfig.keyword || !searchConfig.categoryId) throw new Error("Error --> Keyword or category id is required in query param");
if (!this.options.access_token) throw new Error("Error -->Missing Access token, Generate access token");
if (searchConfig.fieldgroups.length > 0 && !Array.isArray(searchConfig.fieldgroups)) throw new Error("Error -->Field groups should be an array");
const auth = "Bearer " + this.options.access_token;
let queryParam = searchConfig.keyword ? "q=" + searchConfig.keyword + "&" : "";
queryParam = queryParam + searchConfig.categoryId ? "category_ids=" + searchConfig.categoryId + "&" : '';
queryParam = queryParam + searchConfig.limit ? "limit=" + searchConfig.limit + "&" : "";
queryParam = queryParam + searchConfig.fieldgroups.length > 0 ? "fieldgroups=" + searchConfig.fieldgroups.toString() + "&" : "";
return new Promise((resolve, reject) => {
makeRequest('api.ebay.com', `buy/browse/v1/item_summary/search?${queryparam}`, 'GET', this.options.body, auth).then((result) => {
resolve(JSON.parse(result));
});
})
};

module.exports = {
getItem,
getItemByLegacyId,
getItemByItemGroup,
searchItems
}
32 changes: 11 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
//let baseURL = "http://svcs.ebay.com/services/search/FindingService/v1";
let { getRequest, makeRequest, base64Encode } = require('./request');
let { getItem,
getItemByLegacyId,
getItemByItemGroup,
searchItems } = require('./buy-api');
let urlObject = require('./buildURL');

function Ebay(options) {
Expand Down Expand Up @@ -92,23 +96,8 @@ Ebay.prototype = {
console.log(error);
})
},

getItem: function (itemId) {
console.log(this.options);
if (!itemId) throw new Error("Item Id is required");
if (!this.options.access_token) throw new Error("Missing Access token, Generate access token");
const auth = "Bearer " + this.options.access_token;
const id = encodeURIComponent(itemId);
return makeRequest('api.ebay.com', `/buy/browse/v1/item/${id}`, 'GET', this.options.body, auth).then((result) => {
console.log("Success");
let resultJSON = JSON.parse(result);
//this.setAccessToken(resultJSON);
return resultJSON;
});
},

setAccessToken: function (token) {
console.log("inside access tokeeeeee" + token);

this.options.access_token = token;
},

Expand All @@ -118,17 +107,18 @@ Ebay.prototype = {
if (!this.options.body) throw new Error("Missing Body, required Grant type");
const encodedStr = base64Encode(this.options.clientID + ":" + this.options.clientSecret);
let self = this;
console.log(this.options.body);
const auth = "Basic " + encodedStr;
return makeRequest('api.ebay.com', '/identity/v1/oauth2/token', 'POST', this.options.body, auth).then((result) => {
console.log("Successssssssss");
let resultJSON = JSON.parse(result);
// console.log(this);
console.log(resultJSON);
self.setAccessToken(resultJSON.access_token);
return resultJSON;
});
}

},
getItem,
getItemByLegacyId,
getItemByItemGroup,
searchItems
};

module.exports = Ebay;
2 changes: 1 addition & 1 deletion src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let getRequest = function getRequest(url) {

let makeRequest = function postRequest(hostName, endpoint, methodName, data, token) {
methodName == "POST" ? dataString = qs.stringify(data) : '';
// console.log(dataString);
console.log(endpoint);
const options = {
"hostname": hostName,
"path": endpoint,
Expand Down

0 comments on commit ab5a612

Please sign in to comment.