Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsonrpc-calls-96 #97

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 59 additions & 11 deletions fluidinfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ var fluidinfo = function(options) {
}
}
}
if (options.access_token != undefined){
if (options.access_token !== undefined){
OAuthAccessToken = options.access_token;
}
if ((options.username != undefined) && (options.password != undefined)) {
if ((options.username !== undefined) && (options.password !== undefined)) {
authorizationBase64Fragment = Base64.encode(
options.username + ":" + options.password);
// Makes sure the logged in user's username is available via the
Expand Down Expand Up @@ -168,7 +168,7 @@ var fluidinfo = function(options) {
* @return {string} The appropriately encoded URL.
*/
function encodeURL(path) {
var result = "";
var i, result = "";
for (i = 0; i < path.length; i++) {
result += "/" + encodeURIComponent(path[i]);
}
Expand Down Expand Up @@ -201,7 +201,6 @@ var fluidinfo = function(options) {
// check for an array (potential set) and validate it only contains
// strings (currently multi-type arrays are not allowed)
if (isArray(value)) {
var i;
for (i = 0; i < value.length; i++) {
var memberType = typeof(value[i]);
if (memberType !== "string") {
Expand Down Expand Up @@ -229,8 +228,8 @@ var fluidinfo = function(options) {
// then check if the value is a Fluidinfo primitive type. Otherwise
// complain.
var result = null;
if (options.type==="PUT" && (options.path.match("^objects\/") ||
options.path.match("^about\/"))) {
if (options.type === "PUT" && (options.path.match("^objects\/") ||
options.path.match("^about\/"))) {
if (options.contentType){
result = options.contentType;
} else if (isPrimitive(options.data)) {
Expand Down Expand Up @@ -464,7 +463,7 @@ var fluidinfo = function(options) {
} else {
xhr.onreadystatechange = function() {
// Old skool handling of XHR for older browsers.
if (xhr.readyState != 4) return;
if (xhr.readyState !== 4) return;
handleResult();
};
}
Expand Down Expand Up @@ -538,6 +537,55 @@ var fluidinfo = function(options) {
return sendRequest(options);
};

/**
* Make a JSON RPC call (via HTTP POST) to the Fluidinfo API.
* See http://www.simple-is-better.org/json-rpc/jsonrpc20.html for
* the JSON RPC specification.
*/
api.jsonrpc = function(opts){
/*
* param opts: a JS object containing attributes:
* method: the (string) name of the remote method.
* onError: (optional) the error callback function.
* onSuccess: (optional) the success callback function.
* params: the parameters to pass to the remote method,
* either a JS object or array.
*/
var options = clone(opts);
var originalOnSuccess = options.onSuccess;
var originalOnError = options.onError;

options.onSuccess = function(result){
if (result.data.hasOwnProperty('result')){
// Success.
if (originalOnSuccess){
result.data = result.data.result;
originalOnSuccess(result);
}
}
else {
// Error.
if (originalOnError){
// We could overwrite result.status and result.statusText
// here, but I think it's better to just pass the error
// object (which contains a 'code' and 'message' and
// possibly a 'data'.
result.data = result.data.error;
originalOnError(result);
}
}
};
options.type = 'POST';
options.path = 'jsonrpc';
options.data = {
id: 1, // Just send a constant request id for now.
jsonrpc: '2.0',
method: options.method,
params: options.params
};
return sendRequest(options);
};

session.api = api;

/**
Expand Down Expand Up @@ -573,7 +621,7 @@ var fluidinfo = function(options) {
var objectID;
for (objectID in data.id){
if (typeof data.id[objectID] !== "function") {
var obj = new Object();
var tag, obj = new Object();
obj["id"] = objectID;
for (tag in data.id[objectID]) {
if (typeof data.id[objectID][tag] !== "function") {
Expand Down Expand Up @@ -633,7 +681,7 @@ var fluidinfo = function(options) {
var queries = [];
var updateSpecification = [];
updateSpecification[0] = options.where;
var valueSpec = new Object();
var val, valueSpec = new Object();
for (val in options.values){
if (typeof options.values[val] !== "function") {
if(options.values[val] === undefined) {
Expand Down Expand Up @@ -780,8 +828,8 @@ var fluidinfo = function(options) {
*/
session.recent = function(opts) {
var options = clone(opts);
if (options == undefined) {
var options = {};
if (options === undefined) {
options = {};
}

if (options.about) {
Expand Down
Loading