Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: be629f659b
Fetching contributors…

Cannot retrieve contributors at this time

file 184 lines (170 sloc) 5.51 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
using System;
using System.Web;
using LoveSeat.Support;
using Newtonsoft.Json.Linq;

namespace LoveSeat
{
/// <summary>
/// Used as the starting point for any communication with CouchDB
/// </summary>
public class CouchClient : CouchBase
{
/// <summary>
/// This is only intended for use if your CouchDb is in Admin Party
/// </summary>
public CouchClient()
: this("localhost", 5984, null, null)
{
}
/// <summary>
///
/// </summary>
/// <param name="username"></param>
/// <param name="password"></param>
public CouchClient(string username, string password)
: this("localhost", 5984, username, password)
{
}

/// <summary>
/// Constructs the CouchClient and gets an authentication cookie (10 min)
/// </summary>
/// <param name="host">The hostname of the CouchDB instance</param>
/// <param name="port">The port of the CouchDB instance</param>
/// <param name="username">The username of the CouchDB instance</param>
/// <param name="password">The password of the CouchDB instance</param>
public CouchClient(string host, int port, string username, string password)
: base(username, password)
{
baseUri = "http://" + host + ":" + port + "/";
}

/// <summary>
/// Triggers one way replication from the source to target. If bidirection is needed call this method twice with the source and target args reversed.
/// </summary>
/// <param name="source">Uri or database name of database to replicate from</param>
/// <param name="target">Uri or database name of database to replicate to</param>
/// <param name="continuous">Whether or not CouchDB should continue to replicate going forward on it's own</param>
/// <returns></returns>
public JObject TriggerReplication(string source, string target, bool continuous)
{
var request = GetRequest(baseUri + "_replicate");

var options = new ReplicationOptions(source, target, continuous);
var response = request.Post()
.Data(options.ToString())
.GetResponse();

return response.GetCouchDocument();
}



public JObject TriggerReplication(string source, string target)
{
return TriggerReplication(source, target, false);
}

/// <summary>
/// Creates a database
/// </summary>
/// <param name="databaseName">Name of new database</param>
/// <returns></returns>
public JObject CreateDatabase(string databaseName)
{
return GetRequest(baseUri + databaseName).Put().GetResponse().GetCouchDocument();
}
/// <summary>
/// Deletes the specified database
/// </summary>
/// <param name="databaseName">Database to delete</param>
/// <returns></returns>
public JObject DeleteDatabase(string databaseName)
{
return GetRequest(baseUri + databaseName).Delete().GetResponse().GetCouchDocument();
}

/// <summary>
/// Gets a Database object
/// </summary>
/// <param name="databaseName">Name of database to fetch</param>
/// <returns></returns>
public CouchDatabase GetDatabase(string databaseName)
{
return new CouchDatabase(baseUri, databaseName, username, password);
}

public JObject CreateAdminUser(string usernameToCreate, string passwordToCreate)
{
try
{
//Creates the user in the local.ini
var iniResult = GetRequest(baseUri + "_config/admins/" + HttpUtility.UrlEncode(usernameToCreate))
.Put().Json().Data("\"" + passwordToCreate + "\"").GetResponse();
}
catch (CouchException ce)
{
//fail silently
}
var user = @"{ ""name"": ""%name%"",
""_id"": ""org.couchdb.user:%name%"", ""type"": ""user"", ""roles"": [],
}".Replace("%name%", usernameToCreate).Replace("\r\n", "");
var docResult = GetRequest(baseUri + "_users/org.couchdb.user:" + HttpUtility.UrlEncode(usernameToCreate))
.Put().Json().Data(user).GetResponse().GetCouchDocument();
return docResult;

}
/// <summary>
/// Deletes user (if you have permission)
/// </summary>
/// <param name="userToDelete"></param>
public void DeleteAdminUser(string userToDelete)
{
try
{
var iniResult = GetRequest(baseUri + "_config/admins/" + HttpUtility.UrlEncode(userToDelete))
.Delete().Json().GetResponse();
}
catch (CouchException ce)
{
//fail silently
}

var userDb = this.GetDatabase("_users");
var userId = "org.couchdb.user:" + HttpUtility.UrlEncode(userToDelete);
var userDoc = userDb.GetDocument(userId);
if (userDoc != null)
{
userDb.DeleteDocument(userDoc.Id, userDoc.Rev);
}
}
/// <summary>
/// Returns a bool indicating whether or not the database exists.
/// </summary>
/// <param name="databaseName"></param>
/// <returns></returns>
public bool HasDatabase(string databaseName)
{
try
{
var result = GetRequest(baseUri + databaseName)
.Get()
.GetResponse()
.GetCouchDocument()["ok"];
return true;
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// Returns true/false depending on whether or not the user is contained in the _users database
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public bool HasUser(string userId)
{
return GetUser(userId) != null;
}

/// <summary>
/// Get's the user.
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public CouchDocument GetUser(string userId)
{
var db = new CouchDatabase(baseUri, "_users", username, password);
userId = "org.couchdb.user:" + HttpUtility.UrlEncode(userId);
return db.GetDocument(userId);
}
}
}
Something went wrong with that request. Please try again.