Permalink
Browse files

wip - this is really taking shape.

  • Loading branch information...
1 parent df29a0b commit 4b662e52fd4e25213e7654f707fd482152de965d @soldair soldair committed Feb 14, 2014
Showing with 125 additions and 27 deletions.
  1. +1 −0 .gitignore
  2. 0 .npmignore
  3. +77 −9 browser.js
  4. +3 −1 index.js
  5. +40 −15 lib/api.js
  6. +4 −2 package.json
View
@@ -13,3 +13,4 @@ results
npm-debug.log
node_modules
+pinoccio-api.js
View
No changes.
View
@@ -1,24 +1,92 @@
var reconnect = require("reconnect/engine.io");
var engineOptions = require('engine.io-options-from-url');
-
+var through = require('through');
var browserutil = require('./lib/browserutil');
-
-var api = require('./lib/api.js')
+var apibase = require('./lib/api.js')
module.exports = window.pinoccioAPI = function(opts){
- var recon = reconnect().connect(engineOptions(opts.api||browserutil.findApiScript()||"https://api.pinocc.io"));
- var a = api(opts,recon);
+ opts = opts||{};
+ var api = through();
+ var undef;
+ api.pending = {};
+ api.id = 0;
+ api.timeout = 10000;
+
+ opts.cookie = opts.cookie||'pinoccio_id';
- a.login(email,pass,function(err,account){
+ api.token = browserutil.getCookie(opts.cookie);
+ api.server = opts.server||opts.api||browserutil.findAPIScript()||"https://api.pinocc.io";
+ api.account = false;
- a.rest('get','/troops',function(){
- var stream = a.sync();
+ var recon = reconnect().connect(engineOptions(api.server+'/engine'));
+ var a = apibase(opts,recon);
+ api.log = function(){
+ console.log.apply(console,arguments);
+ };
+
+ api.login = function(email,pass,cb){
+ api.rest({url:"/v1/login",method:"post",data:{email:email,password:pass}},function(err,data){
+ // todo untrusted host login/reg
+ if(err) return cb(err);
+ api.account = data.account;
+ api.token = data.token;
+ browserutil.setCookie(opts.cookie,api.token);
+ cb(false,data);
})
+ };
+
+ api.register = function(data,cb){
+ api.rest({url:"/v1/register",method:"post",data:data},function(err,data){
+ // todo untrusted host login/reg
+ if(err) return cb(err);
+ api.account = data.account;
+ api.token = data.token;
+ browserutil.setCookie(opts.cookie,api.token);
+ cb(false,data);
+ });
+ };
+
+ api.logout = function(cb){
+ // todo make sure api server clears troop watchers etc. in socket state. the api should not need to think about this.
+ api.rest({url:'/v1/logout',method:'post'},function(err,data){
+ //api.token = undef;
+ api.account = undef;
+ //clearCookie(opts.cookie);
+ cb(err,data);
+ });
+ };
+
+ api.rest = function(obj,cb){
+ // obj must have url and method.
+ // obj may have data
+
+ api.log('rest>',obj,api.token);
+
+ if(api.token){
+ if(!obj.data) obj.data = {};
+ obj.data.token = api.token;
+ }
+
+ if(!obj.method) obj.method = 'get';
+ a.rest(obj,cb);
+ };
+
+ // start is optional. you will not get state events for those that have occured before start.
+ api.sync = function(account,start){
+ return a.sync(account,start);
+ };
- })
+ // request some or all stats by account.
+ // stats are defined as a time series of reports
+ // if multiple reports are provided they
+ api.stats = function(obj){
+ //
+ return a.stats(obj);
+ }
+ return api;
}
View
@@ -8,6 +8,9 @@ var json = require('./lib/json');
var pkg = require('./package.json')
+var apibase = require('./lib/api');
+
+
module.exports = function(config){
var config = config||{};
@@ -53,7 +56,6 @@ module.exports = function(config){
var data = data||{};
if(this.session.token) data.token = this.session.token;
-
var opts = {};
opts.method = (method||"GET").toUpperCase();
opts.headers = {'x-client-version':pkg.version};
View
@@ -1,52 +1,69 @@
// retry rpc calls and resume streams and live streams.
// a connection is a function that returns
var through = require('through');
-var rest = require('./resthttp');
+//var rest = require('./resthttp');
var connection = require('./connection');
var repipe = require('./repipe');
module.exports = function(config,reconnect){
- var resthttp = rest(config);
+ // make sure
+ //var resthttp = rest(config);
var o = {
reconnect:reconnect,
connection:false,
// make rest like api calls via https or the current streaming connection.
restRetries:config.restRetries||2,
restTimeout:config.restTimeout||30000,// this should not be an issue.
- rest:function(method,uri,data,cb){
+ expectingStream:{},
+ rest:function(obj,cb){
var tries = this.restRetries
,z = this;
var timer = setTimeout(function(){
var e = new Error("call timedout");
e.code = "E_TIMEOUT";
_cb(e);
- }.z.restTimeout);
+ },z.restTimeout);
+
+ var _cb = function(err,data){
+
+ console.log('rest response> ',err,data,raw)
- var _cb = function(err,data,raw){
- clearTimeout(timer);
if(err && tries > 0) return call();
if(err) return cb(err);
- cb(data.error,data.data,raw);
+
+ if(data.stream){
+ // im expecting to follow up with a new strem connection for this callback response.
+ //
+ } else {
+ cb(data.error,data.data);
+ clearTimeout(timer);
+ }
};
function call(){
--tries;
- if(z.connection) {
- z.connection.rest(method,uri,data,_cb);
- }
- resthttp(method,uri,data,_cb);
+
+ getConnection(function(err,con){
+ if(err) return _cb(err);
+ con.rest(obj,_cb);
+ });
+
};
call();
},
- // sync account data
- sync:function(){
-
+ // sync the account's data in realtime
+ sync:function(account){
+ var s = through();
+ repipe(s,function(last){
+ return con.mdm.createReadStream({type:'sync',args:{account:account,start:last}});
+ });
+ return s;
},
// stream stats data
- stream:function(o){
+ stats:function(o){
/*
o.troop
o.scout
@@ -66,12 +83,20 @@ module.exports = function(config,reconnect){
};
var pending = [];
+
function getConnection(cb){
if(reconnect && !reconnect._bound) {
reconnect._bound = 1;
if(reconnect.connected) o.connection = connection.rpc(reconnect._connection);
reconnect.on('connect',function(s){
o.connection = connection.rpc(s);
+ o.connection.mdm.on('connection',function(stream){
+ // the server has opened a stream from me.
+ if(stream.meta && stream.meta.type == "rest-stream") {
+
+ };
+ });
+
while(pending.length) pending.shift()(false,o.connection);
}).on('disconnect',function(){
o.connection = false;
View
@@ -5,7 +5,8 @@
"main": "index.js",
"scripts": {
"test": "tape test/*.js",
- "prepublish": "browserify browser.js -o browser.cmp.js"
+ "build":"browserify browser.js -o pinoccio-api.js",
+ "prepublish":"npm run build"
},
"bin": {
"pinoccio": "./bin/pinoccio.js"
@@ -43,7 +44,8 @@
"rpc-stream": "~1.0.5",
"mux-demux": "~3.7.8",
"reconnect": "~2.0.0",
- "cookie": "~0.1.0"
+ "cookie": "~0.1.0",
+ "engine.io-options-from-url": "0.0.3"
},
"devDependencies": {
"tape": "~2.3.2",

0 comments on commit 4b662e5

Please sign in to comment.