From 7c4cfec33e0fa8f000796d3041bae6bf4e9b3353 Mon Sep 17 00:00:00 2001 From: ajay2507 Date: Sat, 31 Mar 2018 13:25:54 -0700 Subject: [PATCH 1/5] added getItemByLegacyId method --- demo/browseApi.js | 15 +++++++++------ src/index.js | 23 +++++++++++++++++++++-- src/request.js | 2 +- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/demo/browseApi.js b/demo/browseApi.js index 3be74e4..bc829de 100644 --- a/demo/browseApi.js +++ b/demo/browseApi.js @@ -1,21 +1,24 @@ const Ebay = require('../src/index'); let access_token = ""; let ebay = new Ebay({ - clientID: "--Client ID ----", - clientSecret: '-- Client Secret----', + clientID: "Ajaykuma-nodeapi-PRD-bf1a91299-ed4deb45", + clientSecret: 'PRD-f1a91299c206-f184-45e0-b068-f139', body: { grant_type: "client_credentials", scope: 'https://api.ebay.com/oauth/api_scope' } }); -// Getting access token and calling getItem method. + +// Getting access token and calling getItemByLegacyId method. ebay.getAccessToken() .then((data) => { - ebay.getItem('v1|202117468662|0').then((data) => { - console.log(data); + ebay.getItemByLegacyId({ + "legacyItemId": 2628001 + }).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) - }) + }); }); diff --git a/src/index.js b/src/index.js index a259f62..98d9ceb 100644 --- a/src/index.js +++ b/src/index.js @@ -94,7 +94,7 @@ Ebay.prototype = { }, getItem: function (itemId) { - console.log(this.options); + // 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; @@ -107,8 +107,27 @@ Ebay.prototype = { }); }, + 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 makeRequest('api.ebay.com', `/buy/browse/v1/item/get_item_by_legacy_id?${param}`, 'GET', this.options.body, auth).then((result) => { + console.log("Successsssssss"); + let resultJSON = JSON.parse(result); + //this.setAccessToken(resultJSON); + return resultJSON; + }).then((error) => { + console.log(error.errors); + console.log("Error Occurred ===> " + error.errors[0].message); + }); + }, + setAccessToken: function (token) { - console.log("inside access tokeeeeee" + token); + this.options.access_token = token; }, 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, From d0b820c66c92505a5ec7833be759f68af1f945cd Mon Sep 17 00:00:00 2001 From: ajay2507 Date: Sat, 31 Mar 2018 14:25:47 -0700 Subject: [PATCH 2/5] demo for getting item by legacy id --- demo/browseApi.js | 31 ++++++++++++++++++++++++++++--- src/index.js | 1 - 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/demo/browseApi.js b/demo/browseApi.js index bc829de..0e50aa4 100644 --- a/demo/browseApi.js +++ b/demo/browseApi.js @@ -1,8 +1,8 @@ const Ebay = require('../src/index'); let access_token = ""; let ebay = new Ebay({ - clientID: "Ajaykuma-nodeapi-PRD-bf1a91299-ed4deb45", - clientSecret: 'PRD-f1a91299c206-f184-45e0-b068-f139', + clientID: "--Client ID -----", + clientSecret: '-- Client Secret----', body: { grant_type: "client_credentials", scope: 'https://api.ebay.com/oauth/api_scope' @@ -10,11 +10,36 @@ let ebay = new Ebay({ } }); +// Getting access token and calling getItem method. +ebay.getAccessToken() + .then((data) => { + ebay.getItem('v1|202117468662|0').then((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) + }) + }); + + +// 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 + "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 diff --git a/src/index.js b/src/index.js index 98d9ceb..36af48e 100644 --- a/src/index.js +++ b/src/index.js @@ -116,7 +116,6 @@ Ebay.prototype = { let param = "legacy_item_id=" + legacyOptions.legacyItemId; param += legacyOptions.legacyVariationSku ? "&legacy_variation_sku=" + legacyOptions.legacyVariationSku : ''; return makeRequest('api.ebay.com', `/buy/browse/v1/item/get_item_by_legacy_id?${param}`, 'GET', this.options.body, auth).then((result) => { - console.log("Successsssssss"); let resultJSON = JSON.parse(result); //this.setAccessToken(resultJSON); return resultJSON; From 1b53c7e8331ae78ec3a7c865a1b4f76213baa96f Mon Sep 17 00:00:00 2001 From: ajay2507 Date: Sat, 31 Mar 2018 20:51:48 -0700 Subject: [PATCH 3/5] get items by group id --- .gitignore | 3 ++- demo/browseApi.js | 19 +++++++++++++++++-- src/index.js | 19 +++++++++++++++---- 3 files changed, 34 insertions(+), 7 deletions(-) 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 0e50aa4..df1c734 100644 --- a/demo/browseApi.js +++ b/demo/browseApi.js @@ -1,11 +1,11 @@ const Ebay = require('../src/index'); let access_token = ""; let ebay = new Ebay({ - clientID: "--Client ID -----", + clientID: "---Client Id------", clientSecret: '-- Client Secret----', body: { grant_type: "client_credentials", - scope: 'https://api.ebay.com/oauth/api_scope' + scope: 'PRD-f1a91299c206-f184-45e0-b068-f139' } }); @@ -47,3 +47,18 @@ ebay.getAccessToken() }); }); + +//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/index.js b/src/index.js index 36af48e..1876c61 100644 --- a/src/index.js +++ b/src/index.js @@ -125,6 +125,20 @@ Ebay.prototype = { }); }, + 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)); + }); + }) + + + }, + setAccessToken: function (token) { this.options.access_token = token; @@ -136,17 +150,14 @@ 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; }); } - }; module.exports = Ebay; From 246e491633a7b299aa13b102fdbf95c0aca0bdef Mon Sep 17 00:00:00 2001 From: ajay2507 Date: Mon, 2 Apr 2018 22:26:00 -0700 Subject: [PATCH 4/5] added search items method and organized the ebay buy --- src/buy-api.js | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/index.js | 57 +++++++---------------------------------- 2 files changed, 78 insertions(+), 48 deletions(-) create mode 100644 src/buy-api.js diff --git a/src/buy-api.js b/src/buy-api.js new file mode 100644 index 0000000..782c9eb --- /dev/null +++ b/src/buy-api.js @@ -0,0 +1,69 @@ +const 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; + }); +}; + +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("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("Missing Access token, Generate access token"); + 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 1876c61..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,53 +96,6 @@ 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; - }); - }, - - 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 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); - }); - }, - - 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)); - }); - }) - - - }, - setAccessToken: function (token) { this.options.access_token = token; @@ -157,7 +114,11 @@ Ebay.prototype = { self.setAccessToken(resultJSON.access_token); return resultJSON; }); - } + }, + getItem, + getItemByLegacyId, + getItemByItemGroup, + searchItems }; module.exports = Ebay; From f41db9ffe233d0b1bb070b552b8634af660f446b Mon Sep 17 00:00:00 2001 From: ajay2507 Date: Fri, 6 Apr 2018 23:55:19 -0700 Subject: [PATCH 5/5] final changes --- demo/browseApi.js | 8 +++++--- src/buy-api.js | 10 ++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/demo/browseApi.js b/demo/browseApi.js index df1c734..0c3fd12 100644 --- a/demo/browseApi.js +++ b/demo/browseApi.js @@ -1,11 +1,11 @@ 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: 'PRD-f1a91299c206-f184-45e0-b068-f139' + scope: 'https://api.ebay.com/oauth/api_scope' } }); @@ -62,3 +62,5 @@ ebay.getAccessToken() }); + + diff --git a/src/buy-api.js b/src/buy-api.js index 782c9eb..8434426 100644 --- a/src/buy-api.js +++ b/src/buy-api.js @@ -1,5 +1,6 @@ +let { getRequest, makeRequest, base64Encode } = require('./request'); + const 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; @@ -46,9 +47,10 @@ const getItemByItemGroup = function (itemGroupId) { }; const searchItems = function (searchConfig) { - if (!searchConfig) throw new 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("Missing Access token, Generate access token"); + 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 + "&" : '';