index.js | |
---|---|
Memonicmemonic api wrapper for nodejs.
it support basic auth only.
curruntly this wrapper only support two api call | var request = require('request');
var LRU = require("lru-cache")
var _ = require('underscore');
var memonicUrl = 'https://api.memonic.com/v2';
var apiKey = '';
var pool = LRU(100); |
constructor function.it takes one parameter | function Memonic( _apikey ) {
apiKey = _apikey;
this.userId = '';
this.auth = '';
this.isConnected = false;
this.queue = [];
} |
connectit accept 3 parameter | Memonic.prototype.connect = function( id, passwd, callback ) {
this.auth = 'Basic ' + new Buffer(id + ':' + passwd).toString('base64');
if( pool.get( id + ':' + passwd ) ) {
this.userId = pool.get( id + ':' + passwd );
this.isConnected = true;
if(callback) {
callback(null, this.userId );
}
return;
}
this.isConnected = false;
var self = this;
var url = memonicUrl + '/users.json?apikey=' + apiKey;
request(
{
'method' : 'GET'
, 'url' : url
, 'headers' : {
'Authorization' : this.auth
}
},function(err, response, body){
if(err) {
if(callback) {
callback(err);
}
}else {
var result = JSON.parse(body);
if(result.message) {
!!callback && callback(result.message);
return;
}else if( !result.users || result.users.length < 1) {
!!callabck && callback('memonic user info incorrect');
return;
}
self.userId = result.users[0].id;
pool.set( id + ':' + passwd , self.userId );
self.isConnected = true;
if(callback) {
callback(null, self.userId);
}
|
if waiting api call exist then execute, | while(self.queue.length > 0 ) {
var waitFunc = self.queue.shift(); |
console.log(waitFunc.toString()); | waitFunc();
}
}
}
);
} |
itemsget all memonic note items list.
option type is object. possible option name is | Memonic.prototype.items = function( option, callback, connected ) {
var self = this;
if(!this.isConnected && !connected ) {
this.queue.push(function(){
self.items(option, callback, true);
});
return;
}
if(!option) option = {};
var url = memonicUrl + '/users/'+ this.userId +'/items.json?apikey=' + apiKey;
_(['page','pagesize']).each(function(v){
if(option[v]) url = url + '&' + v + '=' + option[v];
});
request(
{
'method' : 'GET'
, 'url' : url
, 'headers' : {
'Authorization' : this.auth
}
},function(err, response, body){
if(err) {
callback(err);
}else {
callback(null, JSON.parse(body));
}
}
);
} |
itemget a memonic note item.
option type is object. possible option name is | Memonic.prototype.item = function( itemId, option, callback, connected ) {
var self = this;
if(!this.isConnected && !connected ) {
this.queue.push(function(){
self.item(itemId,option,callback, true);
});
return;
}
option = option || {};
var url = memonicUrl + '/users/'+ this.userId +'/items/' + itemId +'.json?apikey=' + apiKey;
_(['page','pagesize']).each(function(v){
if(option[v]) url = url + '&' + v + '=' + option[v];
});
request(
{
'method' : 'GET'
, 'url' : url
, 'headers' : {
'Authorization' : this.auth
}
},function(err, response, body){
if(err) {
callback(err);
}else {
callback(null, JSON.parse(body));
}
}
);
}
module.exports = Memonic;
|