Socket
Socket
Sign inDemoInstall

ebay-api

Package Overview
Dependencies
6
Maintainers
1
Versions
68
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.4 to 0.0.5

examples/GetOrders.js

215

index.js

@@ -14,3 +14,3 @@ // eBay API client for Node.js

// - use arrays for repeating keys
var buildParams = function(params) {
var buildUrlParams = function(params) {
var urlFilters = []; // string parts to be joined

@@ -73,22 +73,49 @@

// build URL to PRODUCTION endpoint of GET APIs
// build URL to API endpoints
// set sandbox=true for sandbox, otherwise production
// - params is a 1D obj
// - filters is an obj of { filterType:[filters] } (where filters is an array of ItemFilter)
var buildRequestUrl = function(serviceName, params, filters) {
// params,filters only apply to GET requests; for POST pass in empty {} or null
var buildRequestUrl = function(serviceName, params, filters, sandbox) {
var url;
params = params || {};
filters = filters || {};
sandbox = (typeof sandbox === 'boolean') ? sandbox : false;
switch (serviceName) {
case 'FindingService':
url = "https://svcs.ebay.com/services/search/" + serviceName + "/v1?";
if (sandbox) {
// url = // @todo
throw new Error("Sandbox endpoing for FindingService not yet implemented. Please add.");
}
else url = "https://svcs.ebay.com/services/search/" + serviceName + "/v1?";
break;
case 'Shopping':
url = "http://open.api.ebay.com/shopping?";
if (sandbox) {
// url = // @todo
throw new Error("Sandbox endpoing for Shopping service not yet implemented. Please add.");
}
else url = "http://open.api.ebay.com/shopping?";
break;
case 'Trading': // ...and the other XML APIs
if (sandbox) url = 'https://api.sandbox.ebay.com/ws/api.dll';
else url = 'https://api.ebay.com/ws/api.dll';
// params and filters don't apply to URLs w/ these
return url;
// break;
default:
url = "https://svcs.ebay.com/" + serviceName + '?';
if (sandbox) {
// url = // @todo
throw new Error("Sandbox endpoing for " + serviceName + " service not yet implemented. Please add.");
}
else url = "https://svcs.ebay.com/" + serviceName + '?';
}
url += buildParams(params); // no trailing &
url += buildUrlParams(params); // no trailing &

@@ -105,6 +132,45 @@ _(filters).each(function(typeFilters, type) {

// default params per service type
var defaultParams = function(serviceName, opType, appId) {
var params = {
'OPERATION-NAME': opType,
// build XML input for XML-POST requests
// params should include: authToken, ...
var buildXmlInput = function(opType, params) {
var xmlBuilder = require('xml');
var data = {}, top;
switch(opType) {
// @todo others might have different top levels...
case 'GetOrders':
default:
data[opType + 'Request'] = []; // e.g. <GetOrdersRequest>
top = data[opType + 'Request'];
top.push({ '_attr' : { 'xmlns' : "urn:ebay:apis:eBLBaseComponents" } });
}
if (typeof params.authToken !== 'undefined') {
top.push({ 'RequesterCredentials' : [ { 'eBayAuthToken' : params.authToken } ] });
delete params.authToken;
}
// handle top level
// (e.g. OrderStatus, etc)
// @todo better way to handle deeper inputs?
_(params).each(function(value, key) {
var el = {};
el[key] = value;
top.push(el);
});
// console.log(util.inspect(data,true,10));
data = [ data ];
return '<?xml version="1.0" encoding="UTF-8"?>' + "\n" + xmlBuilder(data, true);
};
// default params per service type.
// for GET requests these go into URL. for POST requests these go into headers.
// options differ by service, see below.
var defaultParams = function(options) {
var params = {},
defaultGetParams = {
'OPERATION-NAME': options.opType,
'GLOBAL-ID': 'EBAY-US',

@@ -114,7 +180,11 @@ 'RESPONSE-DATA-FORMAT': 'JSON',

};
options = options || {};
switch (serviceName) {
switch (options.serviceName) {
// [GET params >
case 'FindingService':
_.extend(params, {
'SECURITY-APPNAME': appId,
params = _.extend({}, defaultGetParams, {
'SECURITY-APPNAME': options.appId ? options.appId : null,
'SERVICE-VERSION': '1.11.0'

@@ -125,5 +195,5 @@ });

case 'MerchandisingService':
_.extend(params, {
'SERVICE-NAME': serviceName,
'CONSUMER-ID': appId,
params = _.extend({}, defaultGetParams, {
'SERVICE-NAME': options.serviceName,
'CONSUMER-ID': options.appId ? options.appId : null,
'SERVICE-VERSION': '1.5.0' // based on response data

@@ -134,10 +204,24 @@ });

case 'Shopping':
_.extend(params, {
'appid': appId,
params = _.extend({}, defaultGetParams, {
'appid': options.appId ? options.appId : null,
'version': '771',
'siteid': '0',
'responseencoding': 'JSON',
'callname': opType
'callname': options.opType
});
break;
// [POST params >
case 'Trading':
params = {
'X-EBAY-API-CALL-NAME' : options.opType,
'X-EBAY-API-COMPATIBILITY-LEVEL' : '773',
'X-EBAY-API-SITEID' : '0', // US
'X-EBAY-API-DEV-NAME': options.devName,
'X-EBAY-API-CERT-NAME': options.cert,
'X-EBAY-API-APP-NAME': options.appName
};
break;
}

@@ -150,3 +234,3 @@

// make a single GET request to a service
// make a single GET request to a JSON service
var ebayApiGetRequest = function(options, callback) {

@@ -160,2 +244,3 @@ if (! options.serviceName) return callback(new Error("Missing serviceName"));

options.parser = options.parser || parseItemsFromResponse;
options.sandbox = options.sandbox || false;

@@ -166,5 +251,6 @@ if (options.serviceName === 'MerchandisingService') {

_.defaults(options.params, defaultParams(options.serviceName, options.opType, options.appId));
// fill in default params. explicit options above will override defaults.
_.defaults(options.params, defaultParams(options));
var url = buildRequestUrl(options.serviceName, options.params, options.filters);
var url = buildRequestUrl(options.serviceName, options.params, options.filters, options.sandbox);
// console.log('url for', options.opType, 'request:\n', url.replace(/\&/g, '\n&'));

@@ -184,3 +270,3 @@

else if (response.statusCode !== 200) {
return callback(new Error(util.format("Bad response status code", response.statusCode, result)));
return callback(new Error(util.format("Bad response status code", response.statusCode, result.toString())));
}

@@ -251,3 +337,82 @@

// PAGINATE multiple requests in parallel (max 100 per page, 100 pages = 10k items)
// make a single POST request to an XML service
var ebayApiPostXmlRequest = function(options, callback) {
if (! options.serviceName) return callback(new Error("Missing serviceName"));
if (! options.opType) return callback(new Error("Missing opType"));
options.params = options.params || {};
options.reqOptions = options.reqOptions || {};
options.sandbox = options.sandbox || false;
// options.parser = options.parser || ...; // @todo
// app/auth params go into headers (see defaultParams())
options.reqOptions.headers = options.reqOptions.headers || {};
_.defaults(options.reqOptions.headers, defaultParams(options));
// console.dir(options);
var url = buildRequestUrl(options.serviceName, {}, {}, options.sandbox);
// console.log('URL:', url);
options.reqOptions.data = buildXmlInput(options.opType, options.params);
// console.dir(options.reqOptions);
var request = restler.post(url, options.reqOptions);
request.on('complete', function(result, response) {
if (result instanceof Error) {
var error = result;
error.message = "Completed with error: " + error.message;
return callback(error);
}
else if (response.statusCode !== 200) {
return callback(new Error(util.format("Bad response status code", response.statusCode, result.toString())));
}
async.waterfall([
// convert xml to json
function toJson(next) {
var xml2js = require('xml2js'),
parser = new xml2js.Parser();
parser.parseString(result, function parseXmlCallback(error, data) {
if (error) {
error.message = "Error parsing XML: " + error.message;
return next(error);
}
next(data);
});
},
function parseData(data, next) {
//// @todo parse the response
// options.parser(data, next);
next(data);
}
],
function(error, data){
if (error) return callback(error);
callback(null, data);
});
});
// emitted when some errors have occurred
// either this OR 'completed' should fire
request.on('error', function(error, response) {
error.message = "Request error: " + error.message;
callback(error);
});
};
module.exports.ebayApiPostXmlRequest = ebayApiPostXmlRequest;
// PAGINATE multiple GET/JSON requests in parallel (max 100 per page, 100 pages = 10k items)
var paginateGetRequest = function(options, callback) {

@@ -254,0 +419,0 @@ if (! options.serviceName) return callback(new Error("Missing serviceName"));

6

package.json
{
"name": "ebay-api",
"description": "eBay API Client",
"version": "0.0.4",
"version": "0.0.5",
"homepage": "https://github.com/newleafdigital/nodejs-ebay-api",

@@ -15,3 +15,5 @@ "author": "Ben Buckman <ben@newleafdigital.com> (http://newleafdigital.com)",

"restler": "~2.0.1",
"underscore": "~1.3.3"
"underscore": "~1.3.3",
"xml": "0.0.7",
"xml2js": "~0.1.14"
},

@@ -18,0 +20,0 @@ "devDependencies": {},

@@ -6,3 +6,3 @@ eBay API client for Node.js

This was built to power the "eBay Picks" section of [AntiquesNearMe.com](http://antiquesnearme.com). It can currently query the FindingService, MerchandisingService, and Shopping API via GET requests, and other services can be added as needed. (Pull requests welcome!)
This was built to power the "eBay Picks" section of [AntiquesNearMe.com](http://antiquesnearme.com). It can currently query the FindingService, MerchandisingService, and Shopping API via JSON-GET requests, and parts of the Trading API via XML-POST. Other services can be added as needed. (Pull requests welcome!)

@@ -25,4 +25,2 @@ ## To use

This library only works with the recent generation of GET/JSON APIs. It does not work with XML or SOAP. (So this library cannot yet be used to query the GetCategories API, for example.) Anyone is welcome to add these layers and contribute them back.
Make sure to obey the eBay API [License](http://developer.ebay.com/join/licenses/individual/) and [Terms](https://www.x.com/developers/ebay/programs/affiliates/terms) when using this library.

@@ -57,2 +55,3 @@

- parser: function which takes the response data and extracts items (or other units depending on the query). Defaults to `parseItemsFromResponse`. To return the raw data, pass in a function like `function(data, callback) { callback(null, data); }`.
- sandbox: true/false (default false = production). May need to add additional endpoint URLs to the code as needed.

@@ -87,2 +86,25 @@ `callback` gets `(error, items)` or `(error, data)` depending on the parser.

### `ebayApiPostXmlRequest(options, callback)`
Make an individual request to a POST-XML service.
`options` must contain:
- serviceName: e.g. 'FindingService'
- opType: e.g. 'findItemsAdvanced'
and can optionally contain:
- (for authentication)
- devName
- cert
- appName
- params (for the XML input)
- reqOptions: headers and other options to pass to the request
- IMPT: Some parameters for these endpoints, such as _SITE-ID_ and _authToken_, should go into the headers, not into `params`. See the API documentation.
- sandbox: true/false (default false = production). May need to add additional endpoint URLs to the code as needed.
`callback` gets `(error, data)`. (There is not currently a default parser for these endpoints.)
## Helpers

@@ -89,0 +111,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc