|
| 1 | +/* |
| 2 | +* CBRAIN Project |
| 3 | +* |
| 4 | +* Copyright (C) 2008-2012 |
| 5 | +* The Royal Institution for the Advancement of Learning |
| 6 | +* McGill University |
| 7 | +* |
| 8 | +* This program is free software: you can redistribute it and/or modify |
| 9 | +* it under the terms of the GNU General Public License as published by |
| 10 | +* the Free Software Foundation, either version 3 of the License, or |
| 11 | +* (at your option) any later version. |
| 12 | +* |
| 13 | +* This program is distributed in the hope that it will be useful, |
| 14 | +* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +* GNU General Public License for more details.query |
| 17 | +* |
| 18 | +* You should have received a copy of the GNU General Public License |
| 19 | +* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | +* |
| 21 | +*/ |
| 22 | + |
| 23 | +/* |
| 24 | +* Author: Natacha Beck <natacha.beck@mcgill.ca> |
| 25 | +*/ |
| 26 | + |
| 27 | +/** |
| 28 | +* TODO general doc |
| 29 | +*/ |
| 30 | + |
| 31 | +"use strict"; |
| 32 | + |
| 33 | +exports.cbrainAPI = function(cbrain_server_url) { |
| 34 | + if (! cbrain_server_url) { throw new Error("Need to be provided with 'cbrain_server_url'."); }; |
| 35 | + cbrain_server_url = cbrain_server_url.replace(/\/*$/,"/"); |
| 36 | + |
| 37 | + var authenticity_token = undefined; |
| 38 | + |
| 39 | + var request = require('request').defaults({ |
| 40 | + jar: true, |
| 41 | + headers: {'Accept': 'application/json'}, |
| 42 | + }); |
| 43 | + |
| 44 | + var session = { |
| 45 | + |
| 46 | + ////////////////////////////////////////////// |
| 47 | + // Authentication and licenses |
| 48 | + ////////////////////////////////////////////// |
| 49 | + |
| 50 | + /** |
| 51 | + * Connects to the server, supplies the credentials |
| 52 | + * and maintains the tokens necessary for the session. |
| 53 | + * |
| 54 | + * agent.login('jack', '&jill') |
| 55 | + * |
| 56 | + */ |
| 57 | + login: function(username, password, callback) { |
| 58 | + if (!username) {callback("Need to be provided with a username."); return;}; |
| 59 | + if (!password) {callback("Need to be provided with a password."); return;}; |
| 60 | + |
| 61 | + setAuthToken( function(err, response) { |
| 62 | + createSession(username, password, function(err,response) { |
| 63 | + err ? callback(err) : callback(null,response); |
| 64 | + }); |
| 65 | + }); |
| 66 | + }, |
| 67 | + |
| 68 | + ////////////////////////////////////////////// |
| 69 | + // Some generic method |
| 70 | + ////////////////////////////////////////////// |
| 71 | + |
| 72 | + /** |
| 73 | + * @doc function |
| 74 | + * @name index |
| 75 | + * @param {string} controller name to specify the request. |
| 76 | + * @param {obj} filters is an optional list of attribute. |
| 77 | + * @param {function} callback Callback function. |
| 78 | + * @description |
| 79 | + * A wrapper method for all index method. |
| 80 | + */ |
| 81 | + index: function(controller, filters, callback) { |
| 82 | + if (!controller) {callback("Need to be provided with a controller name."); return;}; |
| 83 | + |
| 84 | + var path = "/" + controller; |
| 85 | + // Construct the form hash |
| 86 | + filters = filters === null || (typeof filters !== 'undefined') ? filters : {}; |
| 87 | + |
| 88 | + var form = { |
| 89 | + update_filter: "filter_hash", |
| 90 | + clear_filter: "filter_hash", |
| 91 | + }; |
| 92 | + merge_options(form,filters); |
| 93 | + |
| 94 | + // Do the request |
| 95 | + doRequest("GET", path, form, function (err, response, body) { |
| 96 | + var parsed = parse_json_body(body); |
| 97 | + if (!parsed) { |
| 98 | + var status_code = response.statusCode; |
| 99 | + callback("Cannot parsed JSON response in index of " + controller + " response status code: " + status_code); |
| 100 | + } else { |
| 101 | + callback(null,parsed); |
| 102 | + }; |
| 103 | + }); |
| 104 | + }, |
| 105 | + |
| 106 | + /** |
| 107 | + * @doc function |
| 108 | + * @name show |
| 109 | + * @param {string} controller name to specify the request. |
| 110 | +
|
| 111 | + * @param {string} path to specify the request. |
| 112 | + * @param {number} an id. |
| 113 | + * @param {function} callback Callback function. |
| 114 | + * @description |
| 115 | + * A wrapper method for all show method. |
| 116 | + */ |
| 117 | + show: function(controller, id, callback) { |
| 118 | + if (!controller) {callback("Need to be provided with a controller name."); return;}; |
| 119 | + if (!id) {callback("Need to be provided with an id");return;}; |
| 120 | + |
| 121 | + var path = "/" + controller + "/" + id; |
| 122 | + doRequest("GET", path, {}, function (err, response, body ) { |
| 123 | + var parsed = parse_json_body(body); |
| 124 | + if (!parsed) { |
| 125 | + var status_code = response.statusCode; |
| 126 | + callback("Cannot parsed JSON response in show, response status code: " + status_code ); |
| 127 | + } else { |
| 128 | + callback(null,parsed); |
| 129 | + }; |
| 130 | + }); |
| 131 | + }, |
| 132 | + |
| 133 | + ////////////////////////////////////////////// |
| 134 | + // Data Registration |
| 135 | + ////////////////////////////////////////////// |
| 136 | + |
| 137 | + ////////////////////////////////////////////// |
| 138 | + // DataProviders Actions |
| 139 | + ////////////////////////////////////////////// |
| 140 | + |
| 141 | + ////////////////////////////////////////////// |
| 142 | + // Userfiles Actions |
| 143 | + ////////////////////////////////////////////// |
| 144 | + |
| 145 | + ////////////////////////////////////////////// |
| 146 | + // Userfiles Actions |
| 147 | + ////////////////////////////////////////////// |
| 148 | + |
| 149 | + ////////////////////////////////////////////// |
| 150 | + // DataProviders Monitoring |
| 151 | + ////////////////////////////////////////////// |
| 152 | + |
| 153 | + ////////////////////////////////////////////// |
| 154 | + // Computing Site Monitoring |
| 155 | + ////////////////////////////////////////////// |
| 156 | + |
| 157 | + ////////////////////////////////////////////// |
| 158 | + // Users Actions |
| 159 | + ////////////////////////////////////////////// |
| 160 | + |
| 161 | + ////////////////////////////////////////////// |
| 162 | + // Tasks Actions |
| 163 | + ////////////////////////////////////////////// |
| 164 | + |
| 165 | + }; |
| 166 | + |
| 167 | + |
| 168 | + ////////////////////////////////////////////// |
| 169 | + // Low method called by login |
| 170 | + ////////////////////////////////////////////// |
| 171 | + |
| 172 | + // Set the authenticity_token |
| 173 | + function setAuthToken(callback) { |
| 174 | + var form = {}; |
| 175 | + doRequest( "GET", "/session/new", form, function (err, response, body) { |
| 176 | + if (err) {callback(errorr); return;}; |
| 177 | + var parsed = parse_json_body(body); |
| 178 | + authenticity_token = parsed.authenticity_token; |
| 179 | + |
| 180 | + if (!authenticity_token) { |
| 181 | + callback("Cannot obtain authentication token?!? Server response:" + parsed); |
| 182 | + } else { |
| 183 | + callback(null,authenticity_token); |
| 184 | + }; |
| 185 | + }); |
| 186 | + }; |
| 187 | + |
| 188 | + // Create a new session |
| 189 | + function createSession(username, password, callback) { |
| 190 | + if (!username) {callback("Need to be provided with a username."); return;}; |
| 191 | + if (!password) {callback("Need to be provided with a password."); return;}; |
| 192 | + |
| 193 | + var form = { login: username, |
| 194 | + password: password |
| 195 | + }; |
| 196 | + doRequest("POST", "/session", form, function (err, response, body) { |
| 197 | + if (err) {callback(err); return;}; |
| 198 | + callback(null,true); |
| 199 | + }); |
| 200 | + }; |
| 201 | + |
| 202 | + |
| 203 | + ////////////////////////////////////////////// |
| 204 | + // Low level methods |
| 205 | + ////////////////////////////////////////////// |
| 206 | + |
| 207 | + /** |
| 208 | + * @doc function |
| 209 | + * @name doRequest |
| 210 | + * @param {string} method must be a HTTP action (one of :POST, :GET, :PUT, or :DELETE). |
| 211 | + * @param {sting} path is a relative path to append to the URL of the main's URI. |
| 212 | + * @param {obj} form is a hash table to sets body to a querystring representation of value. |
| 213 | + * @param {function} callback Callback function. |
| 214 | + * @description |
| 215 | + * LOW LEVEL METHOD. Used by the other high level methods |
| 216 | + * of thi API. |
| 217 | + * |
| 218 | + */ |
| 219 | + function doRequest(method, path, form, callback) { |
| 220 | + // TODO: Check if throw works |
| 221 | + if (!method) { callback("Need HTTP method (POST, GET, etc)."); return; }; |
| 222 | + if (!path) { callback("Need CBRAIN route."); return; }; |
| 223 | + |
| 224 | + if (method.match(/GET|HEAD/)) { |
| 225 | + // nothing special to do for the moment |
| 226 | + } |
| 227 | + // POST, DELETE etc |
| 228 | + else { |
| 229 | + merge_options( form, {authenticity_token: authenticity_token}); |
| 230 | + } |
| 231 | + |
| 232 | + var url = cbrain_server_url; // # contains trailing / |
| 233 | + path = path.replace(/^\/*/,""); |
| 234 | + var uri = url + path; // slash is inside url |
| 235 | + |
| 236 | + var request_options = { |
| 237 | + method: method, |
| 238 | + uri: uri, |
| 239 | + form: form, |
| 240 | + }; |
| 241 | + |
| 242 | + request(request_options, callback); |
| 243 | + }; |
| 244 | + |
| 245 | + // Decode the json BODY of a reply. |
| 246 | + // Returns the parsed JSON. If anything went wrong, |
| 247 | + // set the internal error message and returns nil. |
| 248 | + function parse_json_body(body) { |
| 249 | + var parsed = undefined; |
| 250 | + try { |
| 251 | + parsed = JSON.parse(body); |
| 252 | + } catch (e) { |
| 253 | + return null; |
| 254 | + } |
| 255 | + return parsed; |
| 256 | + }; |
| 257 | + |
| 258 | + return session; |
| 259 | +}; |
| 260 | + |
| 261 | +// Utility method in order to concat to obj. |
| 262 | +function merge_options(obj1,obj2){ |
| 263 | + Object.keys(obj2).forEach(function(key) { |
| 264 | + obj1[key] = obj1[key] || obj2[key]; |
| 265 | + }) |
| 266 | +}; |
| 267 | + |
0 commit comments