diff --git a/.gitignore b/.gitignore index 9303c34..4bce00c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ -npm-debug.log \ No newline at end of file +npm-debug.log +.vscode/ \ No newline at end of file diff --git a/demo/browseApi.js b/demo/browseApi.js index 3be74e4..0c3fd12 100644 --- a/demo/browseApi.js +++ b/demo/browseApi.js @@ -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) => { @@ -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); + }); + }); + + + + diff --git a/src/buy-api.js b/src/buy-api.js new file mode 100644 index 0000000..8434426 --- /dev/null +++ b/src/buy-api.js @@ -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 +} diff --git a/src/index.js b/src/index.js index a259f62..bb37383 100644 --- a/src/index.js +++ b/src/index.js @@ -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) { @@ -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; }, @@ -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; diff --git a/src/request.js b/src/request.js index 8c00356..0ae84f9 100644 --- a/src/request.js +++ b/src/request.js @@ -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,