Thanks Jens.
Here is the response from GET /
"vendor" :
"name" : "Couchbase Lite (Objective C)",
"version" : "1.0 (unofficial)",
"couchdb" : "Welcome",
"CouchbaseLite" : "Welcome",
"version" : "1.0 (unofficial)"
Here is the code that I am using (in snippets to minimize reading on your part):
Get the URL
// Now safe to use the PhoneGap API
log("onDeviceReady() called, getting Couchbase Lite URL")
if (window.cblite) {
window.cblite.getURL(function(err, url) {
if (err) {
log("error launching Couchbase Lite: " + err)
} else {
log("Couchbase Lite running at: " + url);
var database = new Database( url );
database.initialize();
}
});
} else {
log("error, Couchbase Lite plugin not found.")
}
Initialize the database. Note that all of these calls work perfectly.
this.initialize = function() {
send( "GET", this.baseURL, null, function( status, response ) {
log( "GET / status - " + status );
log( "GET / response - " + response );
});
// First, we need to check to see if the database is set up with our specific scoremoredb.
send( "GET", this.baseURL + DATABASE_NAME, null, function( status, response ) {
if( status === 200 ) {
// Database is initialized already. Good to go!
log( "Database already created. Good to go" );
triggerSync();
} else if( status === 404 ) {
log( "Database doesn't exist yet. Create it" );
// Database was not found. This is a brand new device so we need to replicate it.
// First, let's create our database locally.
send( "PUT", database.getBaseURL() + DATABASE_NAME, null, function( status, response ) {
if( status === 201 ) {
// Database has been created!
log( "Database created!" );
triggerSync();
} else {
// Error. But why?
log( "Database creation error: " + status + "||" + response );
// TODO
}
});
}
});
};
}
Trigger Synchronization (replication)
function triggerSync()
{
log( "In triggerSync" );
var pull = {
create_target : true,
target : "scoremoredb",
source : "https://my-server.com:4755/scoremoredb/",
continuous : true
};
log( "pull - " + JSON.stringify( pull ));
log( database.getBaseURL() + "_replicate" );
send( "POST", database.getBaseURL() + "_replicate", pull, function(status, response) {
log( "trigger status - " + status );
log( "trigger response - " + response );
// TODO!
});
};
Finally, the send method itself
function send( method, url, json, callback ) {
if( method === null || method == undefined ) {
throw "Invalid argument - method must be defined";
}
if( url === null || url == undefined ) {
throw "Invalid argument - url must be defined";
}
if( callback === null || typeof callback !== "function" ) {
throw "Invalid argument - callback must be a valid function";
}
var xhr = new XMLHttpRequest();
log( method + " URL: " + url );
xhr.open( method, url, true );
if( json !== null ) {
xhr.setRequestHeader("Content-Type", "application/json");
}
xhr.onreadystatechange = function() {
if( xhr.readyState === 4 ) {
// stopSpinner();
log( "Status: " + xhr.status );
log( "Response: " + xhr.response );
callback( xhr.status, xhr.response );
}
};
try {
if( json !== null ) {
xhr.send( json );
} else {
xhr.send();
}
} catch(exception) {
// Fail silently
callback( undefined, undefined );
}
}
Everything works as expected except for the POST to _replicate which is giving me a 400 bad request.