From 424a1a0c159e39f78cd42912582c00b6743db352 Mon Sep 17 00:00:00 2001 From: Thermatix Date: Thu, 1 May 2014 15:02:36 +0100 Subject: [PATCH 1/3] Adds methods for accessing events (long polling not suppourted at the moment) --- lib/methodLayer/events.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/methodLayer/events.js b/lib/methodLayer/events.js index dcb96ee..ac7d74a 100644 --- a/lib/methodLayer/events.js +++ b/lib/methodLayer/events.js @@ -10,4 +10,19 @@ 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) +} + +getForEnterprise = function (client,query,callback) { + var path = urlB.host('api').object('events').url + '?stream_type=admin_logs' + client.get(path,callback) +} + +longpolling = function (client,query,callback) { + var path = urlB.host('api').object('events').url + client.options(path,callback) +} \ No newline at end of file From 12b5dec3ae46611ad3a577e6c945b94cb6ad3045 Mon Sep 17 00:00:00 2001 From: Thermatix Date: Fri, 2 May 2014 10:30:06 +0100 Subject: [PATCH 2/3] Adds options to client, adds event functions, updates readme --- lib/methodLayer/events.js | 61 +++++++++++++++++++++++++++++++++++++-- lib/modules/client.js | 9 ++++++ readme.md | 2 +- 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/lib/methodLayer/events.js b/lib/methodLayer/events.js index ac7d74a..6df09b8 100644 --- a/lib/methodLayer/events.js +++ b/lib/methodLayer/events.js @@ -2,6 +2,7 @@ var urlB = require("../modules/urlBuilder").build var qs = require('querystring') var crypto = require('crypto') var stream = require('stream') +var eventEmitter = require('events').EventEmitter var util = require('util') // pass = needs id but can 'pass' query with object instead (along with id). @@ -17,12 +18,66 @@ getForUser = function (client,query,callback) { 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) } - -longpolling = function (client,query,callback) { +exports.getForEnterprise = getForEnterprise +longPolling = function (client,query,callback) { var path = urlB.host('api').object('events').url client.options(path,callback) -} \ No newline at end of file +} +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 Date: Fri, 2 May 2014 14:36:13 +0100 Subject: [PATCH 3/3] fixes typo in multipart parser --- lib/methodLayer/events.js | 1 - package.json | 2 +- readme.md | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/methodLayer/events.js b/lib/methodLayer/events.js index 6df09b8..81e4146 100644 --- a/lib/methodLayer/events.js +++ b/lib/methodLayer/events.js @@ -2,7 +2,6 @@ var urlB = require("../modules/urlBuilder").build var qs = require('querystring') var crypto = require('crypto') var stream = require('stream') -var eventEmitter = require('events').EventEmitter var util = require('util') // pass = needs id but can 'pass' query with object instead (along with id). diff --git a/package.json b/package.json index 9b181e8..ab81fab 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name" : "boxsdk", "preferGlobal" : false, - "version" : "0.2.10", + "version" : "0.2.11", "author" : "Thermatix ", "description" : "An SDK to make using the BOX API easier", "contributors" :[], diff --git a/readme.md b/readme.md index b5e0364..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