Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should all object be available through the session? #37

Open
ls12styler opened this issue Sep 29, 2013 · 0 comments
Open

Should all object be available through the session? #37

ls12styler opened this issue Sep 29, 2013 · 0 comments

Comments

@ls12styler
Copy link

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
    });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant