disconnect
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -0,1 +1,5 @@ | ||
1.2.0 / 2017-06-07 | ||
* Query parameter is now optional for `database.search()` | ||
* Implemented different request limits for authenticated and non-authenticated clients | ||
1.1.0 / 2017-02-23 | ||
@@ -2,0 +6,0 @@ ================== |
'use strict'; | ||
var https = require('https'), | ||
zlib = require('zlib'), | ||
url = require('url'), | ||
pkg = require('../package.json'), | ||
error = require('./error.js'), | ||
util = require('./util.js'); | ||
zlib = require('zlib'), | ||
url = require('url'), | ||
pkg = require('../package.json'), | ||
error = require('./error.js'), | ||
util = require('./util.js'); | ||
@@ -17,9 +17,10 @@ module.exports = DiscogsClient; | ||
var defaultConfig = { | ||
host: 'api.discogs.com', | ||
port: 443, | ||
userAgent: 'DisConnectClient/'+pkg.version+' +'+pkg.homepage, | ||
apiVersion: 'v2', | ||
outputFormat: 'discogs', // Possible values: 'discogs' / 'plaintext' / 'html' | ||
requestLimit: 240, // Maximum number of requests to the Discogs API per interval | ||
requestLimitInterval: 60000 // Request interval in milliseconds | ||
host: 'api.discogs.com', | ||
port: 443, | ||
userAgent: 'DisConnectClient/' + pkg.version + ' +' + pkg.homepage, | ||
apiVersion: 'v2', | ||
outputFormat: 'discogs', // Possible values: 'discogs' / 'plaintext' / 'html' | ||
requestLimit: 25, // Maximum number of requests to the Discogs API per interval | ||
requestLimitAuth: 60, // Maximum number of requests to the Discogs API per interval when authenticated | ||
requestLimitInterval: 60000 // Request interval in milliseconds | ||
}; | ||
@@ -33,4 +34,4 @@ | ||
var queue = require('./queue.js')({ | ||
maxCalls: defaultConfig.requestLimit, | ||
interval: defaultConfig.requestLimitInterval | ||
maxCalls: defaultConfig.requestLimit, | ||
interval: defaultConfig.requestLimitInterval | ||
}); | ||
@@ -45,29 +46,35 @@ | ||
function DiscogsClient(userAgent, auth){ | ||
// Allow the class to be called as a function, returning an instance | ||
if(!(this instanceof DiscogsClient)){ | ||
function DiscogsClient(userAgent, auth) { | ||
// Allow the class to be called as a function, returning an instance | ||
if (!(this instanceof DiscogsClient)) { | ||
return new DiscogsClient(userAgent, auth); | ||
} | ||
// Set the default configuration | ||
this.config = util.merge({}, defaultConfig); | ||
// Set the custom User Agent when provided | ||
if(typeof userAgent === 'string'){ | ||
this.config.userAgent = userAgent; | ||
} | ||
// No userAgent provided, but instead we have an accessObject | ||
if((arguments.length === 1) && (typeof userAgent === 'object')){ auth = userAgent; } | ||
// Set auth data when provided | ||
if(auth && (typeof auth === 'object')){ | ||
if(!auth.hasOwnProperty('method')){ | ||
auth.method = 'discogs'; | ||
} | ||
if(!auth.hasOwnProperty('level')){ | ||
if(auth.userToken){ | ||
auth.level = 2; | ||
}else if(auth.consumerKey && auth.consumerSecret){ | ||
auth.level = 1; | ||
} | ||
} | ||
this.auth = util.merge({}, auth); | ||
} | ||
} | ||
// Set the default configuration | ||
this.config = util.merge({}, defaultConfig); | ||
// Set the custom User Agent when provided | ||
if (typeof userAgent === 'string') { | ||
this.config.userAgent = userAgent; | ||
} | ||
// No userAgent provided, but instead we have an accessObject | ||
if ((arguments.length === 1) && (typeof userAgent === 'object')) { | ||
auth = userAgent; | ||
} | ||
// Set auth data when provided | ||
if (auth && (typeof auth === 'object')) { | ||
queue.setConfig({maxCalls: this.config.requestLimitAuth}); | ||
if (!auth.hasOwnProperty('method')) { | ||
auth.method = 'discogs'; | ||
} | ||
if (!auth.hasOwnProperty('level')) { | ||
if (auth.userToken) { | ||
auth.level = 2; | ||
} else if (auth.consumerKey && auth.consumerSecret) { | ||
auth.level = 1; | ||
} | ||
} | ||
this.auth = util.merge({}, auth); | ||
// Unauthenticated new client instances will decrease the shared request limit | ||
} else { | ||
queue.setConfig({maxCalls: this.config.requestLimit}); | ||
} | ||
} | ||
@@ -80,9 +87,9 @@ | ||
*/ | ||
DiscogsClient.prototype.setConfig = function(customConfig){ | ||
util.merge(this.config, customConfig); | ||
queue.setConfig({ | ||
maxCalls: this.config.requestLimit, | ||
interval: this.config.requestLimitInterval | ||
}); | ||
return this; | ||
DiscogsClient.prototype.setConfig = function(customConfig) { | ||
util.merge(this.config, customConfig); | ||
queue.setConfig({ | ||
maxCalls: (this.authenticated() ? this.config.requestLimitAuth : this.config.requestLimit), | ||
interval: this.config.requestLimitInterval | ||
}); | ||
return this; | ||
}; | ||
@@ -96,5 +103,5 @@ | ||
DiscogsClient.prototype.authenticated = function(level){ | ||
level = level||0; | ||
return (!(typeof this.auth === 'undefined') && (this.auth.level > 0) && (this.auth.level >= level)); | ||
DiscogsClient.prototype.authenticated = function(level) { | ||
level = level || 0; | ||
return (!(typeof this.auth === 'undefined') && (this.auth.level > 0) && (this.auth.level >= level)); | ||
}; | ||
@@ -108,4 +115,4 @@ | ||
DiscogsClient.prototype.getIdentity = function(callback){ | ||
return this.get({url: '/oauth/identity', authLevel: 2}, callback); | ||
DiscogsClient.prototype.getIdentity = function(callback) { | ||
return this.get({url: '/oauth/identity', authLevel: 2}, callback); | ||
}; | ||
@@ -118,19 +125,19 @@ | ||
DiscogsClient.prototype.about = function(callback){ | ||
var clientInfo = { | ||
version: pkg.version, | ||
userAgent: this.config.userAgent, | ||
authMethod: (this.auth ? this.auth.method : 'none'), | ||
authLevel: (this.auth ? this.auth.level : 0) | ||
}; | ||
if(typeof callback === 'function'){ | ||
return this.get('', function(err, data){ | ||
data && (data.disconnect = clientInfo); | ||
callback(err, data); | ||
}); | ||
} | ||
return this.get('').then(function(data){ | ||
data && (data.disconnect = clientInfo); | ||
return data; | ||
}); | ||
DiscogsClient.prototype.about = function(callback) { | ||
var clientInfo = { | ||
version: pkg.version, | ||
userAgent: this.config.userAgent, | ||
authMethod: (this.auth ? this.auth.method : 'none'), | ||
authLevel: (this.auth ? this.auth.level : 0) | ||
}; | ||
if (typeof callback === 'function') { | ||
return this.get('', function(err, data) { | ||
data && (data.disconnect = clientInfo); | ||
callback(err, data); | ||
}); | ||
} | ||
return this.get('').then(function(data) { | ||
data && (data.disconnect = clientInfo); | ||
return data; | ||
}); | ||
}; | ||
@@ -150,98 +157,104 @@ | ||
DiscogsClient.prototype._rawRequest = function(options, callback){ | ||
var data = options.data||null, | ||
method = options.method||'GET', | ||
urlParts = url.parse(options.url), | ||
encoding = options.encoding||'utf8'; | ||
// Build request headers | ||
var headers = { | ||
'User-Agent': this.config.userAgent, | ||
'Accept': 'application/json,application/vnd.discogs.'+this.config.apiVersion+'.'+this.config.outputFormat+'+json,application/octet-stream', | ||
'Accept-Encoding': 'gzip,deflate', | ||
'Host': this.config.host, | ||
'Connection': 'close', | ||
'Content-Length': 0 | ||
}; | ||
// Add content headers for POST/PUT requests that contain data | ||
if(data){ | ||
if(typeof data === 'object'){ data = JSON.stringify(data); } // Convert data to a JSON string when data is an object/array | ||
headers['Content-Type'] = 'application/json'; // Discogs accepts data in JSON format | ||
headers['Content-Length'] = Buffer.byteLength(data, 'utf8'); | ||
} | ||
// Add Authorization header when authenticated (or in the process of authenticating) | ||
if(this.auth && (this.auth.consumerKey || this.auth.userToken)){ | ||
var authHeader = ''; | ||
if(this.auth.method === 'oauth'){ | ||
var fullUrl = (urlParts.protocol && urlParts.host) ? urlParts.href : 'https://'+this.config.host+urlParts.path; | ||
authHeader = this.oauth().toHeader(method, fullUrl); | ||
}else if(this.auth.method === 'discogs'){ | ||
authHeader = 'Discogs'; | ||
if(this.auth.userToken){ | ||
authHeader += ' token='+this.auth.userToken; | ||
}else if(this.auth.consumerKey){ | ||
authHeader += ' key='+this.auth.consumerKey+', secret='+this.auth.consumerSecret; | ||
} | ||
} | ||
headers['Authorization'] = authHeader; | ||
} | ||
// Set the HTTPS request options | ||
var requestOptions = { | ||
host: urlParts.host||this.config.host, | ||
port: urlParts.port||this.config.port, | ||
path: urlParts.path, | ||
method: method, | ||
headers: headers | ||
}; | ||
// Build the HTTPS request | ||
var req = https.request(requestOptions, function(res){ | ||
var data = '', rateLimit = null, add = function(chunk){ data += chunk.toString(); }; | ||
// Pass the data to the callback and pass an error on unsuccessful HTTP status | ||
var passData = function(){ | ||
var err = null, status = parseInt(res.statusCode, 10); | ||
if(status > 399){ // Unsuccessful HTTP status? Then pass an error to the callback | ||
var match = data.match(/^\{"message": "(.+)"\}/i); | ||
err = new error.DiscogsError(status, ((match&&match[1]) ? match[1] : null)); | ||
} | ||
callback(err, data, rateLimit); | ||
}; | ||
// Find and add rate limiting when present | ||
if(res.headers['x-discogs-ratelimit']){ | ||
rateLimit = { | ||
limit: parseInt(res.headers['x-discogs-ratelimit'], 10), | ||
used: parseInt(res.headers['x-discogs-ratelimit-used'], 10), | ||
remaining: parseInt(res.headers['x-discogs-ratelimit-remaining'], 10) | ||
}; | ||
} | ||
// Get the response content and pass it to the callback | ||
switch(res.headers['content-encoding']){ | ||
case 'gzip': | ||
var gunzip = zlib.createGunzip().on('data', add).on('end', passData); | ||
res.pipe(gunzip); | ||
break; | ||
case 'deflate': | ||
var inflate = zlib.createInflate().on('data', add).on('end', passData); | ||
res.pipe(inflate); | ||
break; | ||
default: | ||
// Set encoding when provided | ||
res.setEncoding(encoding); | ||
res.on('data', add).on('end', passData); | ||
} | ||
}).on('error', function(err){ | ||
callback(err); | ||
}); | ||
// When present, write the data to the request | ||
if(data){ req.write(data); } | ||
req.end(); | ||
return this; | ||
DiscogsClient.prototype._rawRequest = function(options, callback) { | ||
var data = options.data || null, | ||
method = options.method || 'GET', | ||
urlParts = url.parse(options.url), | ||
encoding = options.encoding || 'utf8'; | ||
// Build request headers | ||
var headers = { | ||
'User-Agent': this.config.userAgent, | ||
'Accept': 'application/json,application/vnd.discogs.' + this.config.apiVersion + '.' + this.config.outputFormat + '+json,application/octet-stream', | ||
'Accept-Encoding': 'gzip,deflate', | ||
'Host': this.config.host, | ||
'Connection': 'close', | ||
'Content-Length': 0 | ||
}; | ||
// Add content headers for POST/PUT requests that contain data | ||
if (data) { | ||
if (typeof data === 'object') { | ||
data = JSON.stringify(data); | ||
} // Convert data to a JSON string when data is an object/array | ||
headers['Content-Type'] = 'application/json'; // Discogs accepts data in JSON format | ||
headers['Content-Length'] = Buffer.byteLength(data, 'utf8'); | ||
} | ||
// Add Authorization header when authenticated (or in the process of authenticating) | ||
if (this.auth && (this.auth.consumerKey || this.auth.userToken)) { | ||
var authHeader = ''; | ||
if (this.auth.method === 'oauth') { | ||
var fullUrl = (urlParts.protocol && urlParts.host) ? urlParts.href : 'https://' + this.config.host + urlParts.path; | ||
authHeader = this.oauth().toHeader(method, fullUrl); | ||
} else if (this.auth.method === 'discogs') { | ||
authHeader = 'Discogs'; | ||
if (this.auth.userToken) { | ||
authHeader += ' token=' + this.auth.userToken; | ||
} else if (this.auth.consumerKey) { | ||
authHeader += ' key=' + this.auth.consumerKey + ', secret=' + this.auth.consumerSecret; | ||
} | ||
} | ||
headers['Authorization'] = authHeader; | ||
} | ||
// Set the HTTPS request options | ||
var requestOptions = { | ||
host: urlParts.host || this.config.host, | ||
port: urlParts.port || this.config.port, | ||
path: urlParts.path, | ||
method: method, | ||
headers: headers | ||
}; | ||
// Build the HTTPS request | ||
var req = https.request(requestOptions, function(res) { | ||
var data = '', rateLimit = null, add = function(chunk) { | ||
data += chunk.toString(); | ||
}; | ||
// Pass the data to the callback and pass an error on unsuccessful HTTP status | ||
var passData = function() { | ||
var err = null, status = parseInt(res.statusCode, 10); | ||
if (status > 399) { // Unsuccessful HTTP status? Then pass an error to the callback | ||
var match = data.match(/^\{"message": "(.+)"\}/i); | ||
err = new error.DiscogsError(status, ((match && match[1]) ? match[1] : null)); | ||
} | ||
callback(err, data, rateLimit); | ||
}; | ||
// Find and add rate limiting when present | ||
if (res.headers['x-discogs-ratelimit']) { | ||
rateLimit = { | ||
limit: parseInt(res.headers['x-discogs-ratelimit'], 10), | ||
used: parseInt(res.headers['x-discogs-ratelimit-used'], 10), | ||
remaining: parseInt(res.headers['x-discogs-ratelimit-remaining'], 10) | ||
}; | ||
} | ||
// Get the response content and pass it to the callback | ||
switch (res.headers['content-encoding']) { | ||
case 'gzip': | ||
var gunzip = zlib.createGunzip().on('data', add).on('end', passData); | ||
res.pipe(gunzip); | ||
break; | ||
case 'deflate': | ||
var inflate = zlib.createInflate().on('data', add).on('end', passData); | ||
res.pipe(inflate); | ||
break; | ||
default: | ||
// Set encoding when provided | ||
res.setEncoding(encoding); | ||
res.on('data', add).on('end', passData); | ||
} | ||
}).on('error', function(err) { | ||
callback(err); | ||
}); | ||
// When present, write the data to the request | ||
if (data) { | ||
req.write(data); | ||
} | ||
req.end(); | ||
return this; | ||
}; | ||
@@ -261,51 +274,51 @@ | ||
DiscogsClient.prototype._request = function(options, callback){ | ||
var client = this, | ||
doRequest = function(){ | ||
client._rawRequest(options, function(err, data, rateLimit){ | ||
if(data && options.json && (data.indexOf('<!') !== 0)){ | ||
data = JSON.parse(data); | ||
} | ||
callback(err, data, rateLimit); | ||
}); | ||
}, | ||
prepareRequest = function(){ | ||
// Check whether authentication is required | ||
if(!options.authLevel || client.authenticated(options.authLevel)){ | ||
if(options.queue){ // Add API request to the execution queue | ||
queue.add(function(err){ | ||
if(!err){ | ||
doRequest(callback); | ||
}else{ // Can't add to the queue because it's full | ||
callback(err); | ||
} | ||
}); | ||
}else{ // Don't queue, just do the request | ||
doRequest(callback); | ||
} | ||
}else{ | ||
callback(new error.AuthError()); | ||
} | ||
}; | ||
// By default, queue requests | ||
if(!options.hasOwnProperty('queue')){ | ||
options.queue = true; | ||
} | ||
// By default, expect responses to be JSON | ||
if(!options.hasOwnProperty('json')){ | ||
options.json = true; | ||
} | ||
if(typeof callback === 'function'){ | ||
prepareRequest(); | ||
return this; | ||
} | ||
// No callback provided? Return a Promise | ||
return new Promise(function(resolve, reject){ | ||
callback = function(err, data){ | ||
(err && reject(err)) || resolve(data); | ||
}; | ||
prepareRequest(); | ||
}); | ||
DiscogsClient.prototype._request = function(options, callback) { | ||
var client = this, | ||
doRequest = function() { | ||
client._rawRequest(options, function(err, data, rateLimit) { | ||
if (data && options.json && (data.indexOf('<!') !== 0)) { | ||
data = JSON.parse(data); | ||
} | ||
callback(err, data, rateLimit); | ||
}); | ||
}, | ||
prepareRequest = function() { | ||
// Check whether authentication is required | ||
if (!options.authLevel || client.authenticated(options.authLevel)) { | ||
if (options.queue) { // Add API request to the execution queue | ||
queue.add(function(err) { | ||
if (!err) { | ||
doRequest(callback); | ||
} else { // Can't add to the queue because it's full | ||
callback(err); | ||
} | ||
}); | ||
} else { // Don't queue, just do the request | ||
doRequest(callback); | ||
} | ||
} else { | ||
callback(new error.AuthError()); | ||
} | ||
}; | ||
// By default, queue requests | ||
if (!options.hasOwnProperty('queue')) { | ||
options.queue = true; | ||
} | ||
// By default, expect responses to be JSON | ||
if (!options.hasOwnProperty('json')) { | ||
options.json = true; | ||
} | ||
if (typeof callback === 'function') { | ||
prepareRequest(); | ||
return this; | ||
} | ||
// No callback provided? Return a Promise | ||
return new Promise(function(resolve, reject) { | ||
callback = function(err, data) { | ||
(err && reject(err)) || resolve(data); | ||
}; | ||
prepareRequest(); | ||
}); | ||
}; | ||
@@ -320,5 +333,7 @@ | ||
DiscogsClient.prototype.get = function(options, callback){ | ||
if(typeof options === 'string'){ options = {url: options}; } | ||
return this._request(options, callback); | ||
DiscogsClient.prototype.get = function(options, callback) { | ||
if (typeof options === 'string') { | ||
options = {url: options}; | ||
} | ||
return this._request(options, callback); | ||
}; | ||
@@ -334,5 +349,7 @@ | ||
DiscogsClient.prototype.post = function(options, data, callback){ | ||
if(typeof options === 'string'){ options = {url: options}; } | ||
return this._request(util.merge(options, {method: 'POST', data: data}), callback); | ||
DiscogsClient.prototype.post = function(options, data, callback) { | ||
if (typeof options === 'string') { | ||
options = {url: options}; | ||
} | ||
return this._request(util.merge(options, {method: 'POST', data: data}), callback); | ||
}; | ||
@@ -348,5 +365,7 @@ | ||
DiscogsClient.prototype.put = function(options, data, callback){ | ||
if(typeof options === 'string'){ options = {url: options}; } | ||
return this._request(util.merge(options, {method: 'PUT', data: data}), callback); | ||
DiscogsClient.prototype.put = function(options, data, callback) { | ||
if (typeof options === 'string') { | ||
options = {url: options}; | ||
} | ||
return this._request(util.merge(options, {method: 'PUT', data: data}), callback); | ||
}; | ||
@@ -361,5 +380,7 @@ | ||
DiscogsClient.prototype.delete = function(options, callback){ | ||
if(typeof options === 'string'){ options = {url: options}; } | ||
return this._request(util.merge(options, {method: 'DELETE'}), callback); | ||
DiscogsClient.prototype.delete = function(options, callback) { | ||
if (typeof options === 'string') { | ||
options = {url: options}; | ||
} | ||
return this._request(util.merge(options, {method: 'DELETE'}), callback); | ||
}; | ||
@@ -372,5 +393,5 @@ | ||
DiscogsClient.prototype.oauth = function(){ | ||
var OAuth = require('./oauth.js'); | ||
return new OAuth(this.auth); | ||
DiscogsClient.prototype.oauth = function() { | ||
var OAuth = require('./oauth.js'); | ||
return new OAuth(this.auth); | ||
}; | ||
@@ -382,5 +403,5 @@ | ||
*/ | ||
DiscogsClient.prototype.database = function(){ | ||
return require('./database.js')(this); | ||
DiscogsClient.prototype.database = function() { | ||
return require('./database.js')(this); | ||
}; | ||
@@ -392,5 +413,5 @@ | ||
*/ | ||
DiscogsClient.prototype.marketplace = function(){ | ||
return require('./marketplace.js')(this); | ||
DiscogsClient.prototype.marketplace = function() { | ||
return require('./marketplace.js')(this); | ||
}; | ||
@@ -402,5 +423,5 @@ | ||
*/ | ||
DiscogsClient.prototype.user = function(){ | ||
return require('./user.js')(this); | ||
DiscogsClient.prototype.user = function() { | ||
return require('./user.js')(this); | ||
}; |
@@ -5,171 +5,176 @@ 'use strict'; | ||
module.exports = function(client){ | ||
var database = {}; | ||
module.exports = function(client) { | ||
var database = {}; | ||
/** | ||
* Expose Discogs database status constants | ||
*/ | ||
database.status = { accepted: 'Accepted', draft: 'Draft', deleted: 'Deleted', rejected: 'Rejected' }; | ||
/** | ||
* Get artist data | ||
* @param {(number|string)} artist - The Discogs artist ID | ||
* @param {object} [options] - Show releases by the artist + pagination params | ||
* @param {function} [callback] - Callback function | ||
* @return {DiscogsClient|Promise} | ||
*/ | ||
database.getArtist = function(artist, callback){ | ||
return client.get('/artists/'+artist, callback); | ||
}; | ||
/** | ||
* Expose Discogs database status constants | ||
*/ | ||
/** | ||
* Get artist release data | ||
* @param {(number|string)} artist - The Discogs artist ID | ||
* @param {object} [params] - Optional pagination params | ||
* @param {function} [callback] - Callback function | ||
* @return {DiscogsClient|Promise} | ||
*/ | ||
database.getArtistReleases = function(artist, params, callback){ | ||
var path = '/artists/'+artist+'/releases'; | ||
if((arguments.length === 2) && (typeof params === 'function')){ | ||
callback = params; | ||
}else{ | ||
path = util.addParams(path, params); | ||
} | ||
return client.get(path, callback); | ||
}; | ||
database.status = {accepted: 'Accepted', draft: 'Draft', deleted: 'Deleted', rejected: 'Rejected'}; | ||
/** | ||
* Get release data | ||
* @param {(number|string)} release - The Discogs release ID | ||
* @param {function} [callback] - Callback | ||
* @return {DiscogsClient|Promise} | ||
*/ | ||
database.getRelease = function(release, callback){ | ||
return client.get('/releases/'+release, callback); | ||
}; | ||
/** | ||
* Get the release rating for the given user | ||
* @param {(number|string)} release - The Discogs release ID | ||
* @param {string} user - The Discogs user name | ||
* @param {function} [callback] - Callback function | ||
* @return {DiscogsClient|Promise} | ||
*/ | ||
database.getReleaseRating = function(release, user, callback){ | ||
return client.get('/releases/'+release+'/rating/'+util.escape(user), callback); | ||
}; | ||
/** | ||
* Set (or remove) a release rating for the given logged in user | ||
* @param {(number|string)} release - The Discogs release ID | ||
* @param {string} user - The Discogs user name | ||
* @param {number} rating - The new rating for a release between 1 and 5. Null = remove rating | ||
* @param {function} [callback] - Callback function | ||
* @return {DiscogsClient|Promise} | ||
*/ | ||
database.setReleaseRating = function(release, user, rating, callback){ | ||
var url = '/releases/'+release+'/rating/'+util.escape(user); | ||
if(!rating){ | ||
return client.delete({url: url, authLevel: 2}, callback); | ||
}else{ | ||
return client.put({url: url, authLevel: 2}, {rating: ((rating > 5) ? 5 : rating)}, callback); | ||
} | ||
}; | ||
/** | ||
* Get artist data | ||
* @param {(number|string)} artist - The Discogs artist ID | ||
* @param {object} [options] - Show releases by the artist + pagination params | ||
* @param {function} [callback] - Callback function | ||
* @return {DiscogsClient|Promise} | ||
*/ | ||
/** | ||
* Get master release data | ||
* @param {(number|string)} master - The Discogs master release ID | ||
* @param {function} [callback] - Callback function | ||
* @return {DiscogsClient|Promise} | ||
*/ | ||
database.getArtist = function(artist, callback) { | ||
return client.get('/artists/' + artist, callback); | ||
}; | ||
database.getMaster = function(master, callback){ | ||
return client.get('/masters/'+master, callback); | ||
}; | ||
/** | ||
* Get the release versions contained in the given master release | ||
* @param {(number|string)} master - The Discogs master release ID | ||
* @param {object} [params] - optional pagination params | ||
* @param {function} [callback] - Callback function | ||
* @return {DiscogsClient|Promise} | ||
*/ | ||
/** | ||
* Get artist release data | ||
* @param {(number|string)} artist - The Discogs artist ID | ||
* @param {object} [params] - Optional pagination params | ||
* @param {function} [callback] - Callback function | ||
* @return {DiscogsClient|Promise} | ||
*/ | ||
database.getMasterVersions = function(master, params, callback){ | ||
var path = '/masters/'+master+'/versions'; | ||
if((arguments.length === 2) && (typeof params === 'function')){ | ||
callback = params; | ||
}else{ | ||
path = util.addParams(path, params); | ||
} | ||
return client.get(path, callback); | ||
}; | ||
database.getArtistReleases = function(artist, params, callback) { | ||
var path = '/artists/' + artist + '/releases'; | ||
if ((arguments.length === 2) && (typeof params === 'function')) { | ||
callback = params; | ||
} else { | ||
path = util.addParams(path, params); | ||
} | ||
return client.get(path, callback); | ||
}; | ||
/** | ||
* Get label data | ||
* @param {(number|string)} label - The Discogs label ID | ||
* @param {function} [callback] - Callback function | ||
* @return {DiscogsClient|Promise} | ||
*/ | ||
database.getLabel = function(label, callback){ | ||
return client.get('/labels/'+label, callback); | ||
}; | ||
/** | ||
* Get release data | ||
* @param {(number|string)} release - The Discogs release ID | ||
* @param {function} [callback] - Callback | ||
* @return {DiscogsClient|Promise} | ||
*/ | ||
/** | ||
* Get label release data | ||
* @param {(number|string)} label - The Discogs label ID | ||
* @param {object} [params] - Optional pagination params | ||
* @param {function} [callback] - Callback function | ||
* @return {DiscogsClient|Promise} | ||
*/ | ||
database.getLabelReleases = function(label, params, callback){ | ||
var path = '/labels/'+label+'/releases'; | ||
if((arguments.length === 2) && (typeof params === 'function')){ | ||
callback = params; | ||
}else{ | ||
path = util.addParams(path, params); | ||
} | ||
return client.get(path, callback); | ||
}; | ||
/** | ||
* Get an image | ||
* @param {string} url - The full image url | ||
* @param {function} [callback] - Callback function | ||
* @return {DiscogsClient|Promise} | ||
*/ | ||
database.getImage = function(url, callback){ | ||
return client.get({url: url, encoding: 'binary', queue: false, json: false}, callback); | ||
}; | ||
database.getRelease = function(release, callback) { | ||
return client.get('/releases/' + release, callback); | ||
}; | ||
/** | ||
* Search the database | ||
* @param {string} query - The search query | ||
* @param {object} [params] - Search parameters as defined on http://www.discogs.com/developers/#page:database,header:database-search | ||
* @param {function} [callback] - Callback function | ||
* @return {DiscogsClient|Promise} | ||
*/ | ||
/** | ||
* Get the release rating for the given user | ||
* @param {(number|string)} release - The Discogs release ID | ||
* @param {string} user - The Discogs user name | ||
* @param {function} [callback] - Callback function | ||
* @return {DiscogsClient|Promise} | ||
*/ | ||
database.search = function(query, params, callback){ | ||
var obj = {}; | ||
if((arguments.length === 2) && (typeof params === 'function')){ | ||
callback = params; | ||
}else if(params){ | ||
obj = params; | ||
} | ||
obj.q = query||''; | ||
return client.get({url: util.addParams('/database/search', obj), authLevel: 1}, callback); | ||
}; | ||
return database; | ||
database.getReleaseRating = function(release, user, callback) { | ||
return client.get('/releases/' + release + '/rating/' + util.escape(user), callback); | ||
}; | ||
/** | ||
* Set (or remove) a release rating for the given logged in user | ||
* @param {(number|string)} release - The Discogs release ID | ||
* @param {string} user - The Discogs user name | ||
* @param {number} rating - The new rating for a release between 1 and 5. Null = remove rating | ||
* @param {function} [callback] - Callback function | ||
* @return {DiscogsClient|Promise} | ||
*/ | ||
database.setReleaseRating = function(release, user, rating, callback) { | ||
var url = '/releases/' + release + '/rating/' + util.escape(user); | ||
if (!rating) { | ||
return client.delete({url: url, authLevel: 2}, callback); | ||
} else { | ||
return client.put({url: url, authLevel: 2}, {rating: ((rating > 5) ? 5 : rating)}, callback); | ||
} | ||
}; | ||
/** | ||
* Get master release data | ||
* @param {(number|string)} master - The Discogs master release ID | ||
* @param {function} [callback] - Callback function | ||
* @return {DiscogsClient|Promise} | ||
*/ | ||
database.getMaster = function(master, callback) { | ||
return client.get('/masters/' + master, callback); | ||
}; | ||
/** | ||
* Get the release versions contained in the given master release | ||
* @param {(number|string)} master - The Discogs master release ID | ||
* @param {object} [params] - optional pagination params | ||
* @param {function} [callback] - Callback function | ||
* @return {DiscogsClient|Promise} | ||
*/ | ||
database.getMasterVersions = function(master, params, callback) { | ||
var path = '/masters/' + master + '/versions'; | ||
if ((arguments.length === 2) && (typeof params === 'function')) { | ||
callback = params; | ||
} else { | ||
path = util.addParams(path, params); | ||
} | ||
return client.get(path, callback); | ||
}; | ||
/** | ||
* Get label data | ||
* @param {(number|string)} label - The Discogs label ID | ||
* @param {function} [callback] - Callback function | ||
* @return {DiscogsClient|Promise} | ||
*/ | ||
database.getLabel = function(label, callback) { | ||
return client.get('/labels/' + label, callback); | ||
}; | ||
/** | ||
* Get label release data | ||
* @param {(number|string)} label - The Discogs label ID | ||
* @param {object} [params] - Optional pagination params | ||
* @param {function} [callback] - Callback function | ||
* @return {DiscogsClient|Promise} | ||
*/ | ||
database.getLabelReleases = function(label, params, callback) { | ||
var path = '/labels/' + label + '/releases'; | ||
if ((arguments.length === 2) && (typeof params === 'function')) { | ||
callback = params; | ||
} else { | ||
path = util.addParams(path, params); | ||
} | ||
return client.get(path, callback); | ||
}; | ||
/** | ||
* Get an image | ||
* @param {string} url - The full image url | ||
* @param {function} [callback] - Callback function | ||
* @return {DiscogsClient|Promise} | ||
*/ | ||
database.getImage = function(url, callback) { | ||
return client.get({url: url, encoding: 'binary', queue: false, json: false}, callback); | ||
}; | ||
/** | ||
* Search the database | ||
* @param {string} query - The search query | ||
* @param {object} [params] - Search parameters as defined on http://www.discogs.com/developers/#page:database,header:database-search | ||
* @param {function} [callback] - Callback function | ||
* @return {DiscogsClient|Promise} | ||
*/ | ||
database.search = function(query, params, callback) { | ||
var obj = {}; | ||
if ((arguments.length === 2) && (typeof params === 'function')) { | ||
callback = params; | ||
} | ||
if (typeof params === 'object') { | ||
obj = params; | ||
} else if (typeof query === 'object') { | ||
obj = query; | ||
} | ||
if (typeof query === 'string') { | ||
obj.q = query; | ||
} | ||
return client.get({url: util.addParams('/database/search', obj), authLevel: 1}, callback); | ||
}; | ||
return database; | ||
}; |
{ | ||
"name": "disconnect", | ||
"description": "A full featured Discogs API v2.0 client library", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"keywords": ["discogs", "api", "client", "oauth"], | ||
@@ -6,0 +6,0 @@ "homepage": "https://github.com/bartve/disconnect", |
@@ -29,3 +29,3 @@ ## About | ||
require('disconnect') -> new Client() -> oauth() | ||
-> database() | ||
-> database() | ||
-> marketplace() | ||
@@ -32,0 +32,0 @@ -> user() -> collection() |
var wru = require('wru'), | ||
tests = [], | ||
files = ['error','queue','util','client']; | ||
files = ['error','queue','util','client','database']; | ||
@@ -5,0 +5,0 @@ for(var i in files){ |
var wru = require('wru'), | ||
nock = require('nock'), | ||
DiscogsClient = require('../lib/client.js'); | ||
nock = require('nock'), | ||
DiscogsClient = require('../lib/client.js'); | ||
var tests = module.exports = [ | ||
{ | ||
name: 'DiscogsClient: Test instance', | ||
test: function(){ | ||
wru.assert('Instance of DiscogsClient', (new DiscogsClient() instanceof DiscogsClient)); | ||
} | ||
},{ | ||
name: 'DiscogsClient: Test authenticated()', | ||
test: function(){ | ||
wru.assert('Authentication level 1 === false', (new DiscogsClient().authenticated(1) === false)); | ||
} | ||
},{ | ||
name: 'DiscogsClient: Test get()', | ||
test: function(){ | ||
var client = new DiscogsClient(); | ||
client.get({url: '/labels/1'}, wru.async(function(err, data){ | ||
wru.assert('No error', !err); | ||
wru.assert('Correct response data', (data && (data.id === 1))); | ||
})); | ||
} | ||
},{ | ||
name: 'DiscogsClient: Test Promise', | ||
test: function(){ | ||
var client = new DiscogsClient(); | ||
var promise = client.about(); | ||
var isPromise = (typeof promise.then === 'function'); | ||
wru.assert('Returns Promise', isPromise); | ||
if(isPromise){ | ||
promise.then(wru.async(function(data){ | ||
wru.assert('Promis resolved', (typeof data.disconnect !== 'undefined')); | ||
})); | ||
} | ||
} | ||
},{ | ||
name: 'DiscogsClient: Test custom configuration', | ||
test: function(){ | ||
nock('https://www.example.com').get('/labels/1').reply(200, '{"result": "success"}'); | ||
var client = new DiscogsClient().setConfig({host: 'www.example.com'}); | ||
client.get({url: '/labels/1'}, wru.async(function(err, data){ | ||
wru.assert('No error', !err); | ||
wru.assert('Correct response data', (data && data.result === 'success')); | ||
})); | ||
}, | ||
teardown: function(){ | ||
nock.cleanAll(); | ||
} | ||
} | ||
{ | ||
name: 'DiscogsClient: Test instance', | ||
test: function(){ | ||
wru.assert('Instance of DiscogsClient', (new DiscogsClient() instanceof DiscogsClient)); | ||
} | ||
},{ | ||
name: 'DiscogsClient: Test authenticated()', | ||
test: function(){ | ||
wru.assert('Authentication level 1 === false', (new DiscogsClient().authenticated(1) === false)); | ||
} | ||
},{ | ||
name: 'DiscogsClient: Test get()', | ||
test: function(){ | ||
var client = new DiscogsClient(); | ||
client.get({url: '/labels/1'}, wru.async(function(err, data){ | ||
wru.assert('No error', !err); | ||
wru.assert('Correct response data', (data && (data.id === 1))); | ||
})); | ||
} | ||
},{ | ||
name: 'DiscogsClient: Test Promise', | ||
test: function(){ | ||
var client = new DiscogsClient(); | ||
var promise = client.about(); | ||
var isPromise = (typeof promise.then === 'function'); | ||
wru.assert('Returns Promise', isPromise); | ||
if(isPromise){ | ||
promise.then(wru.async(function(data){ | ||
wru.assert('Promis resolved', (typeof data.disconnect !== 'undefined')); | ||
})); | ||
} | ||
} | ||
},{ | ||
name: 'DiscogsClient: Test custom configuration', | ||
test: function(){ | ||
nock('https://www.example.com').get('/labels/1').reply(200, '{"result": "success"}'); | ||
var client = new DiscogsClient().setConfig({host: 'www.example.com'}); | ||
client.get({url: '/labels/1'}, wru.async(function(err, data){ | ||
wru.assert('No error', !err); | ||
wru.assert('Correct response data', (data && data.result === 'success')); | ||
})); | ||
}, | ||
teardown: function(){ | ||
nock.cleanAll(); | ||
} | ||
} | ||
]; | ||
if(!module.parent){ wru.test(tests); } | ||
if(!module.parent){ | ||
wru.test(tests); | ||
} |
69409
23
1460