http = require('http')
url = require('url')

remoteCouch = function(params){

  var couch = params;
  console.log('couch on ' + couch.storageAddress );

  function keyToAddress(key) {
    var i = 0;
    while(i < key.length && key[i] =='u') {
      i++;
    }
    if((i < key.length) && (key[i] == '_')) {
      key = 'u'+key;
    }
    return couch.storageAddress + key;
  }

  function doCall(method, key, value, callback) {
    console.log(method + ' request to: ' + keyToAddress(key));
    var httpObj = url.parse(keyToAddress(key));
    httpObj.method = method;
    httpObj.headers= {Authorization: 'Bearer '+ couch.bearerToken};
    var req = http.request(httpObj, function(res) {
      console.log(method +' STATUS: ' + res.statusCode);
      if(res.statusCode == 404) {
        callback(false, null)
      } else {
        res.on('data', function (chunk) {
          console.log(method + 'DATA: ' + chunk);
          callback(false, chunk)
        });
      }
      if(method='PUT') {
        callback(false, null)
      }
    });
    req.on('error', function(e) {
      console.log(method + 'error: ' + e.status);
      // I have no idea if this works. Could not find info about the
      // error in the API
      if(e.status == 404) {
        callback(false, null)
      } else {
        callback(e, null)
      }
    });

    if(method!='GET') {
      req.write(value);
    }
    req.end();
  }
  
  couch.getUserAddress = function() {
    return couch.storageAddress
  }


  couch.get = function(key, callback) {
    console.log('couch.get("'+key+'", callback);');
    doCall('GET', key, null, function(str) {
      var obj = JSON.parse(str);
      couch['_shadowCouchRev_'+key] = obj._rev;
      callback(false, obj.value);
    });
  }

  couch.set = function(key, value, callback) {
    console.log('couch.set("'+key+'", "'+value+'", callback);');
    var revision = couch['_shadowCouchRev_'+key];
    var obj = {
      value: value
    };
    if(revision) {
      obj._rev = revision;
    }
    doCall('PUT', key, JSON.stringify(obj), function(err, str) {
      var obj = JSON.parse(str);
      if(obj.rev) {
        couch['_shadowCouchRev_'+key] = obj.rev;
      }
      callback();
    });
  }
 
  couch.remove = function(key, callback) {
    console.log('couch.remove("'+key+'", callback);');
    doCall('DELETE', key, null, callback);
  } 
  return couch
}

exports.remoteCouch = remoteCouch;

@@ -780,37 +780,36 @@
        callback(
-false
+null
 , null)%0A      %7D 
@@ -889,16 +889,17 @@
 thod + '
+ 
 DATA: ' 
@@ -927,21 +927,20 @@
 allback(
-false
+null
 , chunk)
@@ -964,71 +964,8 @@
   %7D%0A
-      if(method='PUT') %7B%0A        callback(false, null)%0A      %7D%0A
     
@@ -1008,35 +1008,36 @@
 %7B%0A      console.
-log
+warn
 (method + 'error
@@ -1031,21 +1031,21 @@
 thod + '
-error
+ERROR
 : ' + e.
@@ -1057,178 +1057,8 @@
 s);%0A
-      // I have no idea if this works. Could not find info about the%0A      // error in the API%0A      if(e.status == 404) %7B%0A        callback(false, null)%0A      %7D else %7B%0A  
     
@@ -1077,24 +1077,16 @@
 , null)%0A
-      %7D%0A
     %7D);%0A
@@ -1368,23 +1368,126 @@
 unction(
+err, 
 str) %7B%0A
+      if(err != null %7C%7C str == null)%0A      %7B%0A        callback(err, null)%0A        return;%0A      %7D%0A%0A
       va
@@ -1507,24 +1507,31 @@
 parse(str);%0A
+      %0A
       couch%5B
@@ -1930,24 +1930,101 @@
 err, str) %7B%0A
+      if(str == null)%0A      %7B%0A        callback(err);%0A        return;%0A      %7D%0A
       var ob
@@ -2056,16 +2056,31 @@
   if(obj
+ != null && obj
 .rev) %7B%0A
@@ -2151,16 +2151,19 @@
 allback(
+err
 );%0A    %7D

handle null values / 404 correctly