Skip to content

Commit

Permalink
Added support for sandbox environment. (#26)
Browse files Browse the repository at this point in the history
* added support for sandbox env

* updated readme
  • Loading branch information
pajaydev authored Jan 14, 2019
1 parent 3bac0d8 commit af1e0a0
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 21 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ let eBay = require('ebay-node-api')
let ebay = new eBay({
clientID: "-- Client APP ID ----",
// options - optional HTTP request timeout to apply to all requests.
env: "SANDBOX" // optional default = "PRODUCTION"
})
```
Creates a new `Ebay` instance.
Expand All @@ -59,11 +60,14 @@ Creates a new `Ebay` instance.
Join eBay developers program.
Register your app here https://go.developer.ebay.com/quick-start-guide.

If you using Sandbox environment, make sure to provide `env` variable in options as mentioned above.

#### Options

- `clientID` - Required(`String`) - Client Id key provided when you register in eBay developers program.
- `limit` - optional(`Number`) - fetch items functionality - Number that limits the number of data you need in response.
- `details` - optional(`Boolean`) - Get User Details functionality - true, if you need details about the user.
- `env` - optional(`String`) - Environment, default value is PRODUCTION.

## Example

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ebay-node-api",
"version": "2.3.1",
"version": "2.5.1",
"description": "Ebay node api client",
"main": "./src/index.js",
"homepage": "https://github.com/ajay2507/ebay-node-api",
Expand Down Expand Up @@ -29,4 +29,4 @@
"nock": "^9.2.3",
"sinon": "^4.4.5"
}
}
}
5 changes: 3 additions & 2 deletions src/buildURL.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const buildURL = {
* @private
*/
buildSearchUrl(options) {
let base_url = "http://svcs.ebay.com/services/search/FindingService/v1?";
console.log(options);
let base_url = `http://${options.baseSvcUrl}/services/search/FindingService/v1?`;
base_url += "SECURITY-APPNAME=" + options.clientID;
base_url += "&OPERATION-NAME=" + options.operationName;
base_url += "&SERVICE-VERSION=1.0.0&RESPONSE-DATA-FORMAT=JSON";
Expand All @@ -33,7 +34,7 @@ const buildURL = {
* @private
*/
buildShoppingUrl(options) {
let base_url = "http://open.api.ebay.com/Shopping?";
let base_url = `http://${options.baseUrl}/Shopping?`;
base_url += "appid=" + options.clientID;
base_url += "&callname=" + options.operationName;
base_url += "&version=967&siteid=0&responseencoding=JSON&";
Expand Down
8 changes: 4 additions & 4 deletions src/buy-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const getItem = function (itemId) {
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) => {
return makeRequest(this.options.baseUrl, `/buy/browse/v1/item/${id}`, 'GET', this.options.body, auth).then((result) => {
return JSON.parse(result);
});
};
Expand All @@ -20,7 +20,7 @@ const getItemByLegacyId = function (legacyOptions) {
let param = "legacy_item_id=" + legacyOptions.legacyItemId;
param += legacyOptions.legacyVariationSku ? "&legacy_variation_sku=" + legacyOptions.legacyVariationSku : '';

makeRequest('api.ebay.com', `/buy/browse/v1/item/get_item_by_legacy_id?${param}`, 'GET', this.options.body, auth).then((result) => {
makeRequest(this.options.baseUrl, `/buy/browse/v1/item/get_item_by_legacy_id?${param}`, 'GET', this.options.body, auth).then((result) => {
return JSON.parse(result);
}).then((error) => {
console.log(error.errors);
Expand All @@ -34,7 +34,7 @@ const getItemByItemGroup = function (itemGroupId) {
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) => {
makeRequest(this.options.baseUrl, `/buy/browse/v1/item/get_items_by_item_group?item_group_id=${itemGroupId}`, 'GET', this.options.body, auth).then((result) => {
resolve(result);
}).then((error) => {
reject(error);
Expand All @@ -55,7 +55,7 @@ const searchItems = function (searchConfig) {
if (searchConfig.fieldgroups != undefined) queryParam = queryParam + "&fieldgroups=" + searchConfig.fieldgroups;
if (searchConfig.filter != undefined) queryParam = queryParam + "&filter=" + encodeURIComponent(makeString(searchConfig.filter, { quotes: "no", braces: 'false' }));
return new Promise((resolve, reject) => {
makeRequest('api.ebay.com', `/buy/browse/v1/item_summary/search?${queryParam}`, 'GET', this.options.body, auth).then((result) => {
makeRequest(this.options.baseUrl, `/buy/browse/v1/item_summary/search?${queryParam}`, 'GET', this.options.body, auth).then((result) => {
resolve(result);
}).then((error) => {
reject(error);
Expand Down
3 changes: 3 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict';

module.exports = {
PROD_BASE_URL: "api.ebay.com",
SANDBOX_BASE_URL: "api.sandbox.ebay.com",
BASE_SVC_URL: "svcs.ebay.com",
BASE_SANDBX_SVC_URL: "svcs.sandbox.ebay.com",
MERCH_SRVC_NAME: "MerchandisingService"
}
15 changes: 13 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,23 @@ const { getDefaultCategoryTreeId,
getCategorySuggestions,
getItemAspectsForCategory } = require('./taxonomy-api');
const { getSimilarItems, getMostWatchedItems } = require('./merchandising');
const { PROD_BASE_URL, SANDBOX_BASE_URL, BASE_SANDBX_SVC_URL, BASE_SVC_URL } = require('./constants');
const urlObject = require('./buildURL');

const PROD_ENV = "PROD";
const SANDBOX_ENV = "SANDBOX";
function Ebay(options) {

if (!options) throw new Error("Options is missing, please provide the input");
if (!options.clientID) throw Error("Client ID is Missing\ncheck documentation to get Client ID http://developer.ebay.com/DevZone/account/");
if (!(this instanceof Ebay)) return new Ebay(options);
if (!options.env) options.env = PROD_ENV;
options.baseUrl = PROD_BASE_URL;
options.baseSvcUrl = BASE_SVC_URL;
// handle sandbox env.
if (options.env === SANDBOX_ENV) {
options.baseUrl = SANDBOX_BASE_URL;
options.baseSvcUrl = BASE_SANDBX_SVC_URL;
}
this.options = options;
this.options.globalID = options.countryCode || "EBAY-US";
}
Expand Down Expand Up @@ -64,6 +74,7 @@ Ebay.prototype = {
getVersion: function () {
this.options.operationName = "getVersion";
const url = urlObject.buildSearchUrl(this.options);
console.log(url);
return getRequest(url).then((data) => {
return JSON.parse(data)["getVersionResponse"][0];
}, console.error
Expand Down Expand Up @@ -94,7 +105,7 @@ Ebay.prototype = {
const encodedStr = base64Encode(this.options.clientID + ":" + this.options.clientSecret);
const self = this;
const auth = "Basic " + encodedStr;
return makeRequest('api.ebay.com', '/identity/v1/oauth2/token', 'POST', this.options.body, auth).then((result) => {
return makeRequest(this.options.baseUrl, '/identity/v1/oauth2/token', 'POST', this.options.body, auth).then((result) => {
const resultJSON = JSON.parse(result);
self.setAccessToken(resultJSON.access_token);
return resultJSON;
Expand Down
7 changes: 3 additions & 4 deletions src/merchandising.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const { getRequest } = require('./request');
const { BASE_SVC_URL, MERCH_SRVC_NAME } = require('./constants');
const { MERCH_SRVC_NAME } = require('./constants');

//https://developer.ebay.com/devzone/merchandising/docs/CallRef/getMostWatchedItems.html#Samples

Expand All @@ -11,12 +11,11 @@ const { BASE_SVC_URL, MERCH_SRVC_NAME } = require('./constants');
* @param {String} categoryId (optional)
*/
const getMostWatchedItems = function (merchOptions) {
console.log(this);
if (!this.options.clientID) throw new Error("Missing App id or client id");
let url = '';
if (merchOptions && merchOptions.categoryId != undefined) url = `&categoryId=${merchOptions.categoryId}`;
if (merchOptions && merchOptions.maxResults) url = `&maxResults=${merchOptions.maxResults}`;
return getRequest(`http://${BASE_SVC_URL}/${MERCH_SRVC_NAME}?OPERATION-NAME=getMostWatchedItems&SERVICE-NAME=${MERCH_SRVC_NAME}&SERVICE-VERSION=1.1.0&CONSUMER-ID=${this.options.clientID}&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD${url}`).then((result) => {
return getRequest(`http://${this.options.baseSvcUrl}/${MERCH_SRVC_NAME}?OPERATION-NAME=getMostWatchedItems&SERVICE-NAME=${MERCH_SRVC_NAME}&SERVICE-VERSION=1.1.0&CONSUMER-ID=${this.options.clientID}&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD${url}`).then((result) => {
return JSON.parse(result);
}).catch((error) => {
console.log(error);
Expand All @@ -34,7 +33,7 @@ const getSimilarItems = function (merchOptions) {
let url = '';
if (merchOptions && merchOptions.itemId) url = `&itemId=${merchOptions.itemId}`;
if (merchOptions && merchOptions.maxResults) url = `${url}&maxResults=${merchOptions.maxResults}`;
return getRequest(`http://${BASE_SVC_URL}/${MERCH_SRVC_NAME}?OPERATION-NAME=getSimilarItems&SERVICE-NAME=${MERCH_SRVC_NAME}&SERVICE-VERSION=1.1.0&CONSUMER-ID=${this.options.clientID}&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD${url}`).then((result) => {
return getRequest(`http://${this.options.baseSvcUrl}/${MERCH_SRVC_NAME}?OPERATION-NAME=getSimilarItems&SERVICE-NAME=${MERCH_SRVC_NAME}&SERVICE-VERSION=1.1.0&CONSUMER-ID=${this.options.clientID}&RESPONSE-DATA-FORMAT=JSON&REST-PAYLOAD${url}`).then((result) => {
return JSON.parse(result);
}).catch((error) => {
console.log(error);
Expand Down
10 changes: 5 additions & 5 deletions src/taxonomy-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const getDefaultCategoryTreeId = function (marketPlaceId) {
marketPlaceId = upperCase(marketPlaceId);
if (!this.options.access_token) throw new Error("Missing Access token, Generate access token");
const auth = "Bearer " + this.options.access_token;
return makeRequest('api.ebay.com', `/commerce/taxonomy/v1_beta/get_default_category_tree_id?marketplace_id=${marketPlaceId}`, 'GET', this.options.body, auth).then((result) => {
return makeRequest(this.options.baseUrl, `/commerce/taxonomy/v1_beta/get_default_category_tree_id?marketplace_id=${marketPlaceId}`, 'GET', this.options.body, auth).then((result) => {
return JSON.parse(result);
});
};
Expand All @@ -26,7 +26,7 @@ const getCategoryTree = function (categoryTreeId) {
if (!categoryTreeId) categoryTreeId = 0;
if (!this.options.access_token) throw new Error("Missing Access token, Generate access token");
const auth = "Bearer " + this.options.access_token;
return makeRequest('api.ebay.com', `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}`, 'GET', this.options.body, auth).then((result) => {
return makeRequest(this.options.baseUrl, `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}`, 'GET', this.options.body, auth).then((result) => {
return JSON.parse(result);
});
};
Expand All @@ -42,7 +42,7 @@ const getCategorySubtree = function (categoryTreeId, categoryId) {
if (!categoryId) throw new Error("Missing Categor id \n Refer documentation here https://developer.ebay.com/api-docs/commerce/taxonomy/resources/category_tree/methods/getCategorySubtree#h2-samples");
if (!this.options.access_token) throw new Error("Missing Access token, Generate access token");
const auth = "Bearer " + this.options.access_token;
return makeRequest('api.ebay.com', `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}/get_category_subtree?category_id=${categoryId}`, 'GET', this.options.body, auth).then((result) => {
return makeRequest(this.options.baseUrl, `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}/get_category_subtree?category_id=${categoryId}`, 'GET', this.options.body, auth).then((result) => {
return JSON.parse(result);
});
};
Expand All @@ -58,7 +58,7 @@ const getCategorySuggestions = function (categoryTreeId, keyword) {
if (!keyword) throw new Error("Missing keyword \n Refer documentation here https://developer.ebay.com/api-docs/commerce/taxonomy/resources/category_tree/methods/getCategorySuggestions");
if (!this.options.access_token) throw new Error("Missing Access token, Generate access token");
const auth = "Bearer " + this.options.access_token;
return makeRequest('api.ebay.com', `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}/get_category_suggestions?q=${keyword}`, 'GET', this.options.body, auth).then((result) => {
return makeRequest(this.options.baseUrl, `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}/get_category_suggestions?q=${keyword}`, 'GET', this.options.body, auth).then((result) => {
return JSON.parse(result);
});
};
Expand All @@ -73,7 +73,7 @@ const getItemAspectsForCategory = function (categoryTreeId, categoryId) {
if (!categoryId) throw new Error("Missing Category id \n Refer documentation here https://developer.ebay.com/api-docs/commerce/taxonomy/resources/category_tree/methods/getItemAspectsForCategory#h2-samples");
if (!this.options.access_token) throw new Error("Missing Access token, Generate access token");
const auth = "Bearer " + this.options.access_token;
return makeRequest('api.ebay.com', `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}/get_item_aspects_for_category?category_id=${categoryId}`, 'GET', this.options.body, auth).then((result) => {
return makeRequest(this.options.baseUrl, `/commerce/taxonomy/v1_beta/category_tree/${categoryTreeId}/get_item_aspects_for_category?category_id=${categoryId}`, 'GET', this.options.body, auth).then((result) => {
return JSON.parse(result);
});
};
Expand Down
7 changes: 5 additions & 2 deletions test/buildURL.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ describe("test building url methods", () => {
param: "keywords",
clientID: "testID",
limit: 6,
globalID: "EBAY-US"
globalID: "EBAY-US",
baseSvcUrl: 'svcs.ebay.com'
}
expect(buildURL.buildSearchUrl(options)).to.be.equal(expected_search_url);
});
Expand All @@ -26,6 +27,7 @@ describe("test building url methods", () => {
operationName: "demoShoppingName",
param: "keywords",
clientID: "testID",
baseUrl: 'open.api.ebay.com'
}
expect(buildURL.buildShoppingUrl(options)).to.be.equal(expected_search_url);
});
Expand All @@ -37,7 +39,8 @@ describe("test building url methods", () => {
operationName: "demoShoppingName",
param: "keywords",
clientID: "testID",
includeSelector: true
includeSelector: true,
baseUrl: 'open.api.ebay.com'
}
expect(buildURL.buildShoppingUrl(options)).to.be.equal(expected_search_url);
});
Expand Down

0 comments on commit af1e0a0

Please sign in to comment.