diff --git a/lib/methodLayer/events.js b/lib/methodLayer/events.js index dcb96ee..81e4146 100644 --- a/lib/methodLayer/events.js +++ b/lib/methodLayer/events.js @@ -10,4 +10,73 @@ var util = require('util') // query = can pass query // all with query have default values -// will probably need to impliment with streams \ No newline at end of file +// will probably need to impliment with streams + +getForUser = function (client,query,callback) { + var path = urlB.host('api').object('events').url + client.get(path,callback) +} + +export.getForUser = getForUser +getForEnterprise = function (client,query,callback) { + var path = urlB.host('api').object('events').url + '?stream_type=admin_logs' + client.get(path,callback) +} +exports.getForEnterprise = getForEnterprise +longPolling = function (client,query,callback) { + var path = urlB.host('api').object('events').url + client.options(path,callback) +} +exports.longPolling = longPolling + +pollStreamWrapper = function (client,query,opt){ + + req = function(reqPath,caller) { + client.options(reqPath,function (response,statcode){ + var response = JSON.stringify(response) + switch(response.message){ + case 'reconnect': + process.nextTick(req(reqPath,caller)) + break + case 'new_change': + client.get(caller.path + '?' + caller.admin + caller.limit +'stream_position=' + caller.streamPosition, function (res,sc) { + var res = JSON.stringify(res) + caller.streamPosition = res.next_Stream_position + for(entry in res.entries){ + push(entry) + } + process.nextTick(req(reqPath,caller)) + }) + break + default: + throw new Error('Unknown response') + } + }) + } + + streamer = function (client,query,opt){ + if(typeof query == 'undefined' || query == null) throw new Error("'query' can't be undefined or null, pass {} for no queryies") + this.path = urlB.host('api').object('events').url + this.streamPath = '' + this.streamPosition = query.streamPosition || 0 + this.admin = query.admin == true ? 'stream_type=admin_logs&' : '' + this.limit = 'limit=' + query.limit + '&' || '' + client.get(this.path + '?stream_position='(this.streamPosition || 'now'), function (response,statcode) { + this.streamPosition = JSON.stringify(response).next_Stream_position + client.options(path,function (response,statcode){ + this.streamPath = JSON.stringify(response).url + }) + }) + Readable.call(this, opt) + } + + util.inherits(streamer,stream.Readable) + + streamer.prototype._read() { + process.nextTick(req(this.streamPath,this)) + } + + return new streamer(client,query,opt) +} + +exports.streamer = pollStreamWrapper \ No newline at end of file diff --git a/lib/modules/client.js b/lib/modules/client.js index 9debbc6..862afcf 100644 --- a/lib/modules/client.js +++ b/lib/modules/client.js @@ -95,6 +95,15 @@ var client = { this.headers = func.getHeaders(this.aToken, 'get',{user : this.user}) func.makeRequest(callb, this.path, null, this.headers,'get') }, + options:function (path, callb, extraHeader) { + var headerKeys = Object.keys(extraHeader) + for(var i=0; i", "description" : "An SDK to make using the BOX API easier", "contributors" :[], diff --git a/readme.md b/readme.md index 1467c23..5ecd942 100644 --- a/readme.md +++ b/readme.md @@ -6,7 +6,7 @@ The client can be used to make Oauth easier, do get,post,put and delete calls an ##Authentication First make a client builder and pass the app ID and secret. -## + The client Builder should be outside of any requests, that way it's accessible from all requests; it's there so you don't have to give the client your appID and secret each time you need it. ###Setup Client Factory: ```javascript @@ -211,7 +211,7 @@ Functions implemented are: - Files (all functions) - Comments (all functions) - Collaborations (all functions) -- Events (no functions) +- Events (all functions (streamer is available but untested)) - Search (all functions) - Users (all functions) - Groups (all functions)