我正在编写我的第一个node.js包,以帮助使REST API更易于使用,并且我无法格式化包以允许使用该包的人在他们的应用程序中执行以下操作。
var Water= require("water-reservation");
var water = Water({username: myusername, password: mypassword});
// Problem here because Water({}) needs to do a REST call to get a bearer
// token to use the API I am trying to simplify. Thus, it tries to
// get_water(callabck) before the bearer_token var is set in my package.
water.get_water(function(err, result){
if(err){
console.log(err);
console.log("----------------------------------------------");
}
else{
console.log(result);
console.log("----------------------------------------------");
}
});
在我当前的设置中,我获取用户的用户名和密码,并将其传递给REST端点,以获取我在所有REST调用包中使用的持票令牌。在完成在我的包中设置bearer_token变量之前,异步性质将在用户文件中调用get_water。
这是我的包的样子:
var request = require('request');
var bearer_token = "";
var api_url = "";
var Water = function(credentials){
api_url = credentials.api_url;
var username = credentials.username;
var password = credentials.password;
get_bearer_token(username, password, function(err, access_token){
bearer_token = access_token;
});
};
function get_bearer_token(username, password, callback){
var request_options = {
url: api_url + "/auth",
method: "GET",
'auth': {
'user': username,
'pass': password
}
};
request(request_options, function(err, res, body){
if(err) {
return callback("Water Initialization Error: " + err, null);
}
else {
return callback(null, body);
}
});
}
// Get water
Water.prototype.get_water = function(callback) {
var request_options = {
url: api_url + "/water",
method: "GET",
'auth': {
'bearer': bearer_token
}
};
request(request_options, function(err, res, body){
if(err) {
return callback(err, null);
}
else{
return callback(null, body);
}
});
};
// more package functions...
module.exports = Water;
我正在尝试找到一种方法来进行设置,以便用户可以使用如上所述的包。我不是那种风格,但它似乎是用户理解和使用最简单的方式。
我能想到解决这个问题的唯一方法是给Water({})添加一个回调,但这会让用户在回调中包装所有与水相关的代码会造成很多麻烦。我知道可以通过查看Twitter包来完成,但我仍然可以把它放在他们如何做到这一点上。任何帮助将不胜感激,谢谢。
答案 0 :(得分:1)
您应该在发出请求时委派获取令牌的责任。这样包就会立即初始化。为了提高效率,您可以在第一次获取令牌时缓存令牌。我在这里做了一个例子,但是你可以使用像async这样的东西来整理它:
var request = require('request');
var Water = function (credentials) {
this.api_url = credentials.api_url;
this.username = credentials.username;
this.password = credentials.password;
return this;
};
Water.prototype.get_bearer_token = function (callback) {
// We already have the bearer token, so return straight away
if (this.bearer_token) return callback(null, this.bearer_token);
var self = this;
var request_options = {
url: this.api_url + "/auth",
method: "GET",
'auth': {
'user': this.username,
'pass': this.password
}
};
request(request_options, function(err, res, body){
if(err) {
return callback("Water Initialization Error: " + err, null);
}
else {
self.bearer_token = body;
return callback(null, body);
}
});
}
// Get water
Water.prototype.get_water = function(callback) {
var self = this;
this.get_bearer_token(function (err, token) {
if (err) return callback(err);
var request_options = {
url: self.api_url + "/water",
method: "GET",
'auth': {
'bearer': token
}
};
request(request_options, function(err, res, body){
if(err) {
return callback(err, null);
}
else{
return callback(null, body);
}
});
});
};
// more package functions..
module.exports = Water;
使用它:
var Water = require('water-reservation');
var water = new Water({
username: 'username',
password: 'pass',
api_url: 'http://example.com'
});
water.get_water(function (err, res) {
});