danwrong / restler

REST client library for node.js
MIT License
1.99k stars 391 forks source link

Cannot use multiple instances of a service #218

Open ryedog opened 8 years ago

ryedog commented 8 years ago

It appears that the only way to set the username/password for a service is by setting the username/password properties on this.defaults for a service. But the this.defaults is used across all instances of a service. Therefore creating multiple instances of a service with different usernames/passwords will result with 2 different instances with the same usernames/passwords.

Lets say you've already created the Twitter service (same one from the Reslter examples)

var tweet1 = new Twitter('danwrong', 'password');
console.log('tweet1 username', tweet1.defaults.username);

// Instantiate a second service with a different username
var tweet2 = new Twitter('ryedog', 'password');

// tweet1's is now tweet2's username
console.log('tweet1 username', tweet1.defaults.username);
console.log('tweet2 username', tweet2.defaults.username);

Would be great to be able to create multiple instances of a service with different username & passwords

**Full example code***

// Setup a Service
//
// This is taken directly from the example code on https://github.com/danwrong/restler
var rest = require('restler');

Twitter = rest.service(function(u, p) {
  this.defaults.username = u;
  this.defaults.password = p;
}, {
  baseURL: 'http://twitter.com'
}, {
  update: function(message) {
    return this.post('/statuses/update.json', { data: { status: message } });
  }
});

// Instantiate a new service
var tweet1 = new Twitter('danwrong', 'password');
console.log('tweet1 username', tweet1.defaults.username);

// Instantiate a second service with a different username
var tweet2 = new Twitter('ryedog', 'password');

// client1's username is not the username from client2
//
// Since we're creating a new instance of the service
// shouldn't each instance have it's own properties?
console.log();
console.log('tweet1 username', tweet1.defaults.username);
console.log('tweet2 username', tweet2.defaults.username);