Skip to content

Commit afc38b8

Browse files
committed
Initial import.
This code was extracted from the CBRAIN platform, and will be maintained separately from now on.
1 parent 53a2e36 commit afc38b8

File tree

70 files changed

+6215
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+6215
-0
lines changed

install_doc.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/bin/bash
2+
3+
if test $# -ne 1 ; then
4+
cat <<USAGE
5+
6+
Usage: $0 doc_install_directory
7+
8+
This script will install the documentation for
9+
the CBRAIN APIs in the directory given in argument.
10+
11+
It is simply wrapper around the "rdoc" and "perldoc"
12+
commands, so the documentation will be generated for
13+
the Perl and Ruby APIs. You will need the commands
14+
"rdoc", "perldoc" and "pod2html" to generate everything.
15+
16+
USAGE
17+
exit 2
18+
fi
19+
20+
BASE_DOC="$1"
21+
22+
# Run this script where it is.
23+
if ! test -d "ruby" -a -d "perl" -a -d "java" ; then
24+
echo "Error: this script should be run from the directory where the CBRAIN APIs are located."
25+
exit 20
26+
fi
27+
28+
# Directories
29+
PERLAPI_DOC="$BASE_DOC/perl"
30+
RUBYAPI_DOC="$BASE_DOC/ruby"
31+
32+
# Check dirs
33+
if ! test -d "$BASE_DOC" ; then
34+
echo "Error: it seems the base directory given in argument doesn't exist?!?"
35+
exit 20
36+
fi
37+
38+
echo "Generating and installing Perl API doc in plain text and HTML ..."
39+
mkdir -p "$PERLAPI_DOC" || exit 20
40+
perldoc -t perl/CbrainAPI.pm > "$PERLAPI_DOC/CbrainPerlAPI.txt" || exit 20
41+
pod2html --title="CBRAIN Perl API" perl/CbrainAPI.pm > "$PERLAPI_DOC/CbrainPerlAPI.html" || exit 20
42+
test -f pod2htmd.tmp && rm -f pod2htmd.tmp # cleanup of leftover junk
43+
44+
echo "Generating and installing Ruby API doc in HTML ..."
45+
mkdir -p "$RUBYAPI_DOC" || exit 20
46+
rdoc -O --op="$RUBYAPI_DOC" ruby/cbrain_ruby_api.rb
47+
48+
49+
cat <<FINAL
50+
51+
All done. The documentation is located here:
52+
53+
Perl doc, plain text: $PERLAPI_DOC/CbrainPerlAPI.txt
54+
Perl doc, HTML : $PERLAPI_DOC/CbrainPerlAPI.html
55+
Ruby doc, HTML : $RUBYAPI_DOC/CbrainRubyAPI.html
56+
57+
FINAL
58+
59+

node/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
lib/exemple.js
2+
node_modules/*
3+
test.js
4+
tracegl/*

node/Gruntfile.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module.exports = function(grunt) {
2+
3+
// Project configuration.
4+
grunt.initConfig({
5+
6+
pkg: grunt.file.readJSON('package.json'),
7+
8+
docular: {
9+
docular_webapp_target: "docs/docular",
10+
docular_partial_home: "docs/node_api_home.html",
11+
groups: [
12+
{
13+
groupTitle: "Node API",
14+
groupId: "node API",
15+
showSource: false,
16+
sections: [
17+
{
18+
title: "Node API",
19+
id: "node API",
20+
scripts: ["lib/cbrain_api.js"]
21+
},
22+
],
23+
},
24+
],
25+
},
26+
});
27+
28+
// Load the plugin that provides the "docular" tasks.
29+
grunt.loadNpmTasks('grunt-docular');
30+
31+
// Default task(s).
32+
grunt.registerTask('default', ['docular']);
33+
34+
};

node/README.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
The node.js API for CBRAIN is preliminary and incomplete.
3+
4+
Please contact the CBRAIN developers for more information.
5+

node/lib/cbrain_api.js

Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
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+

node/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "cbrain_api",
3+
"version": "0.0.0",
4+
"description": "CbrainAPI ",
5+
"main": "./lib/cbrain_api.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "GNU General Public License v3",
11+
"dependencies": {
12+
"request": "^2.42.0"
13+
},
14+
"devDependencies": {
15+
"grunt": "^0.4.1",
16+
"grunt-docular": "^0.1.2"
17+
}
18+
}

0 commit comments

Comments
 (0)