Floby / node-libspotify

Node bindings for the libspotify C library
projects.flo.by/node-libspotify/
102 stars 66 forks source link

Should all object be available through the session? #37

Open ls12styler opened 11 years ago

ls12styler commented 11 years ago

Hi,

I'm wondering what peoples thoughts would be on interacting with all sub systems via single session object?

I think it makes the API a little cleaner.

Example chanegs to libspotify.js:

// **libspotify.js
var Session = require('./Session');
var b = require('bindings')('spotify.node');

function Spotify (config) {
    this.config = config || {};
}

Spotify.prototype.login = function (user, pass, cb) {
    var sess = new Session({
        applicationKey: this.config.applicationKey
    });

    // Use callback as main event handler
    sess.login(user, pass, cb);
}

exports = Spotify;

Example changes to session.js:

Session.prototype.login = function login(login, password, cb) {
    if('string' != typeof login) throw new TypeError('login should be a string');
    if('string' != typeof password) throw new TypeError('password should be a string');
    if('function' != typeof cb) cb = function () {};
    var self = this;
    this.once('login', function (err) {
        return cb(err, self);
    });
    b.session_login(this._sp_session, login, password);
 };

+Session.prototype.search = function (query, options, cb) {
    var s = new Search(this._sp_session, query);
    s.once('ready', cb);
    for (var i in options) {
        s[i] = options[i];
    }
    // Run the search
    s.execute();
}

In your script:

// Create new Spotify instance
var Spotify = new sp.Spotify({
    applicationKey: __dirname + '/../conf/spotify_appkey.key'
});

// Login and find something to play
Spotify.login(cred.login, cred.password, function (err, session) {
    console.log(session); // should log Session object
    // session is now an object to work with.
    // all processing/searching/playing would be done via session.
    session.search('sometrack', {trackCount:20}, function (err, search) {
        console.log(search); // Should log the Search object
    });
});