2012-12-23 22:36:59 +00:00
|
|
|
/**
|
2013-01-20 19:13:20 +00:00
|
|
|
* @fileoverview Connection to the blob server and API for the RPCs it
|
|
|
|
* provides. All blob index UI code should use this connection to contact
|
|
|
|
* the server.
|
2012-12-23 22:36:59 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
goog.provide('camlistore.ServerConnection');
|
|
|
|
|
2012-12-23 23:18:09 +00:00
|
|
|
goog.require('goog.net.XhrIo');
|
|
|
|
goog.require('goog.uri.utils');
|
2012-12-23 22:36:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {camlistore.ServerType.DiscoveryDocument} config Discovery document
|
|
|
|
* for the current server.
|
2012-12-23 23:18:09 +00:00
|
|
|
* @param {Function=} opt_sendXhr Function for sending XHRs for testing.
|
2012-12-23 22:36:59 +00:00
|
|
|
* @constructor
|
|
|
|
*/
|
2012-12-23 23:18:09 +00:00
|
|
|
camlistore.ServerConnection = function(config, opt_sendXhr) {
|
2012-12-23 22:36:59 +00:00
|
|
|
/**
|
|
|
|
* @type {camlistore.ServerType.DiscoveryDocument}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.config_ = config;
|
2012-12-23 23:18:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {function}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.sendXhr_ = opt_sendXhr || goog.net.XhrIo.send;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
camlistore.ServerConnection.prototype.getRecentlyUpdatedPermanodes =
|
|
|
|
function(success, opt_thumbnailSize, opt_fail) {
|
|
|
|
|
|
|
|
var path = goog.uri.utils.appendPath(
|
|
|
|
this.config_.searchRoot, 'camli/search/recent');
|
|
|
|
if (!!opt_thumbnailSize) {
|
|
|
|
path = goog.uri.utils.appendParam(path, 'thumbnails', opt_thumbnailSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.sendXhr_(
|
|
|
|
path,
|
|
|
|
goog.bind(this.getRecentlyUpdatedPermanodesDone_, this,
|
|
|
|
success, opt_fail));
|
2012-12-23 22:36:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-12-23 23:18:09 +00:00
|
|
|
/**
|
|
|
|
* @param {Function} success Success callback.
|
|
|
|
* @param {Function?} fail Optional fail callback.
|
|
|
|
* @param {goog.events.Event} e Event that triggered this
|
|
|
|
*/
|
|
|
|
camlistore.ServerConnection.prototype.getRecentlyUpdatedPermanodesDone_ =
|
|
|
|
function(success, fail, e) {
|
|
|
|
var xhr = e.target;
|
|
|
|
var error = !xhr.isSuccess();
|
|
|
|
var result = null;
|
|
|
|
if (!error) {
|
|
|
|
result = xhr.getResponseJson();
|
|
|
|
error = !result;
|
|
|
|
}
|
|
|
|
if (error) {
|
|
|
|
if (fail) {
|
|
|
|
fail()
|
|
|
|
} else {
|
|
|
|
// TODO(bslatkin): Add a default failure event handler to this class.
|
|
|
|
console.log('Failed XHR in ServerConnection');
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
success(result);
|
|
|
|
};
|
2012-12-24 01:52:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {function(string)} success Success callback, called with permanode blobref.
|
|
|
|
* @param {Function=} opt_fail Optional fail callback.
|
|
|
|
*/
|
|
|
|
camlistore.ServerConnection.prototype.createPermanode = function(success, opt_fail) {
|
2013-01-20 19:13:20 +00:00
|
|
|
// TODO(bradfitz): stop depending on camli.js. For now, cheating:
|
2012-12-24 01:52:23 +00:00
|
|
|
camliCreateNewPermanode({
|
|
|
|
success: success,
|
|
|
|
fail: opt_fail
|
|
|
|
});
|
|
|
|
};
|