Ejemplo n.º 1
0
        public Result <JObject> TriggerReplication(ReplicationOptions aReplicationOptions, Result <JObject> aResult)
        {
            if (aReplicationOptions == null)
            {
                throw new ArgumentNullException("aReplicationOptions");
            }
            if (aResult == null)
            {
                throw new ArgumentNullException("aResult");
            }

            Plug p = BasePlug.At(Constants.REPLICATE);

            string json = aReplicationOptions.ToString();

            p.Post(DreamMessage.Ok(MimeType.JSON, json), new Result <DreamMessage>()).WhenDone(
                a =>
            {
                if ((a.Status == DreamStatus.Accepted) ||
                    (a.Status == DreamStatus.Ok))
                {
                    aResult.Return(JObject.Parse(a.ToText()));
                }
                else
                {
                    aResult.Throw(new CouchException(a));
                }
            },
                aResult.Throw
                );

            return(aResult);
        }
Ejemplo n.º 2
0
        public Result DeleteConfigValue(string aSection, string aKeyName, Result aResult)
        {
            if (String.IsNullOrEmpty(aSection))
            {
                throw new ArgumentException("aSection cannot be null nor empty");
            }
            if (String.IsNullOrEmpty(aKeyName))
            {
                throw new ArgumentException("aKeyName cannot be null nor empty");
            }
            if (aResult == null)
            {
                throw new ArgumentNullException("aResult");
            }

            BasePlug.At(Constants.CONFIG, XUri.EncodeFragment(aSection), XUri.EncodeFragment(aKeyName)).Delete(DreamMessage.Ok(), new Result <DreamMessage>()).WhenDone(
                a =>
            {
                if (a.Status == DreamStatus.Ok)
                {
                    aResult.Return();                            // remove " and "\n
                }
                else
                {
                    aResult.Throw(new CouchException(a));
                }
            },
                aResult.Throw
                );
            return(aResult);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Retrieve list of available databases on the server
        /// </summary>
        /// <param name="aResult"></param>
        /// <returns></returns>
        public Result <IEnumerable <string> > GetAllDatabases(Result <IEnumerable <string> > aResult)
        {
            if (aResult == null)
            {
                throw new ArgumentNullException("aResult");
            }

            BasePlug.At("_all_dbs").Get(new Result <DreamMessage>()).WhenDone(
                a =>
            {
                if (a.Status == DreamStatus.Ok)
                {
                    var d = JArray.Parse(a.ToText());
                    aResult.Return(d.Values <string>());
                }
                else
                {
                    aResult.Throw(new CouchException(a));
                }
            },
                aResult.Throw
                );

            return(aResult);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Deletes the specified database
        /// </summary>
        /// <param name="aDatabaseName">Database to delete</param>
        /// <param name="aResult"></param>
        /// <returns></returns>
        public Result <JObject> DeleteDatabase(string aDatabaseName, Result <JObject> aResult)
        {
            if (String.IsNullOrEmpty(aDatabaseName))
            {
                throw new ArgumentException("DatabaseName cannot be null nor empty");
            }
            if (aResult == null)
            {
                throw new ArgumentNullException("aResult");
            }

            BasePlug.At(XUri.EncodeFragment(aDatabaseName)).Delete(new Result <DreamMessage>()).WhenDone(
                a =>
            {
                if (a.Status == DreamStatus.Ok)
                {
                    aResult.Return(JObject.Parse(a.ToText()));
                }
                else
                {
                    aResult.Throw(new CouchException(a));
                }
            },
                aResult.Throw
                );

            return(aResult);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Get's the user.
        /// </summary>
        /// <param name="aUserId"></param>
        /// <returns></returns>
        public JDocument GetUser(string aUserId)
        {
            if (String.IsNullOrEmpty(aUserId))
            {
                throw new ArgumentException("aUser cannot be null nor empty");
            }

            var db = new CouchDatabase(BasePlug.At("_users"));

            aUserId = "org.couchdb.user:" + HttpUtility.UrlEncode(aUserId);
            return(db.GetDocument(aUserId, new Result <JDocument>()).Wait());
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Returns a bool indicating whether or not the database exists.
        /// </summary>
        /// <param name="aDatabaseName"></param>
        /// <param name="aResult"></param>
        /// <returns></returns>
        public Result <bool> HasDatabase(string aDatabaseName, Result <bool> aResult)
        {
            if (String.IsNullOrEmpty(aDatabaseName))
            {
                throw new ArgumentException("DatabaseName cannot be null nor empty");
            }
            if (aResult == null)
            {
                throw new ArgumentNullException("aResult");
            }

            BasePlug.At(XUri.EncodeFragment(aDatabaseName)).Head(new Result <DreamMessage>()).WhenDone(
                a => aResult.Return(a.Status == DreamStatus.Ok),
                aResult.Throw
                );

            return(aResult);
        }
Ejemplo n.º 7
0
        public void CreateAdminUser(string aUserName, string aPassword)
        {
            if (String.IsNullOrEmpty(aUserName))
            {
                throw new ArgumentException("aUserName cannot be null nor empty");
            }
            if (String.IsNullOrEmpty(aPassword))
            {
                throw new ArgumentException("aPassword cannot be null nor empty");
            }

            SetConfigValue("admins", aUserName, aPassword, new Result()).Wait();
            BasePlug.WithCredentials(aUserName, aPassword);            // Logon(username, password, new Result<bool>()).Wait();
            CouchUser user = new CouchUser {
                Name = aUserName
            };

            ObjectSerializer <CouchUser> serializer = new ObjectSerializer <CouchUser>();

            BasePlug.At("_users", HttpUtility.UrlEncode("org.couchdb.user:" + aUserName)).Put(DreamMessage.Ok(MimeType.JSON, serializer.Serialize(user)), new Result <DreamMessage>()).Wait();
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Restarts the CouchDB instance. You must be authenticated as a user with administration privileges for this to work.
        /// </summary>
        /// <param name="aResult"></param>
        /// <returns></returns>
        public Result RestartServer(Result aResult)
        {
            if (aResult == null)
            {
                throw new ArgumentNullException("aResult");
            }

            BasePlug.At(Constants.RESTART).Post(DreamMessage.Ok(MimeType.JSON, String.Empty), new Result <DreamMessage>()).WhenDone(
                a => {
                if (a.Status == DreamStatus.Ok)
                {
                    aResult.Return();
                }
                else
                {
                    aResult.Throw(new CouchException(a));
                }
            },
                aResult.Throw
                );
            return(aResult);
        }
Ejemplo n.º 9
0
        public Result <string> GetConfigValue(string aSection, string aKeyName, Result <string> aResult)
        {
            if (String.IsNullOrEmpty(aSection))
            {
                throw new ArgumentException("aSection cannot be null nor empty");
            }
            if (String.IsNullOrEmpty(aKeyName))
            {
                throw new ArgumentException("aKeyName cannot be null nor empty");
            }
            if (aResult == null)
            {
                throw new ArgumentNullException("aResult");
            }


            BasePlug.At(Constants.CONFIG, XUri.EncodeFragment(aSection), XUri.EncodeFragment(aKeyName)).Get(DreamMessage.Ok(), new Result <DreamMessage>()).WhenDone(
                a =>
            {
                string value = a.ToText();
                switch (a.Status)
                {
                case DreamStatus.Ok:
                    aResult.Return(value.Substring(1, value.Length - 3));                                            // remove " and "\n
                    break;

                case DreamStatus.NotFound:
                    aResult.Return((string)null);
                    break;

                default:
                    aResult.Throw(new CouchException(a));
                    break;
                }
            },
                aResult.Throw
                );
            return(aResult);
        }
Ejemplo n.º 10
0
        /// <summary>
        ///  Gets a Database
        /// </summary>
        /// <param name="aDatabaseName">Name of the database</param>
        /// <param name="createIfNotExists">Flag specifying if the database must be created if not found</param>
        /// <param name="aResult"></param>
        /// <returns></returns>
        public Result <CouchDatabase> GetDatabase(string aDatabaseName, bool createIfNotExists, Result <CouchDatabase> aResult)
        {
            if (String.IsNullOrEmpty(aDatabaseName))
            {
                throw new ArgumentException("DatabaseName cannot be null nor empty");
            }
            if (aResult == null)
            {
                throw new ArgumentNullException("aResult");
            }


            HasDatabase(aDatabaseName, new Result <bool>()).WhenDone(
                exists =>
            {
                if (exists)
                {
                    aResult.Return(new CouchDatabase(BasePlug.At(XUri.EncodeFragment(aDatabaseName))));
                }
                else
                {
                    if (createIfNotExists)
                    {
                        CreateDatabase(aDatabaseName, new Result <JObject>()).WhenDone(
                            a => aResult.Return(new CouchDatabase(BasePlug.At(XUri.EncodeFragment(aDatabaseName)))),
                            aResult.Throw
                            );
                    }
                    else
                    {
                        aResult.Return((CouchDatabase)null);
                    }
                }
            },
                aResult.Throw
                );

            return(aResult);
        }
Ejemplo n.º 11
0
        public Result <Dictionary <string, Dictionary <string, string> > > GetConfig(Result <Dictionary <string, Dictionary <string, string> > > aResult)
        {
            if (aResult == null)
            {
                throw new ArgumentNullException("aResult");
            }

            BasePlug.At(Constants.CONFIG).Get(DreamMessage.Ok(), new Result <DreamMessage>()).WhenDone(
                a =>
            {
                if (a.Status == DreamStatus.Ok)
                {
                    aResult.Return(JsonConvert.DeserializeObject <Dictionary <string, Dictionary <string, string> > >(a.ToText()));
                }
                else
                {
                    aResult.Throw(new CouchException(a));
                }
            },
                aResult.Throw
                );
            return(aResult);
        }
Ejemplo n.º 12
0
        public Result <Dictionary <string, string> > GetConfigSection(string aSection, Result <Dictionary <string, string> > aResult)
        {
            if (aSection == null)
            {
                throw new ArgumentNullException("aSection");
            }
            if (aResult == null)
            {
                throw new ArgumentNullException("aResult");
            }
            if (String.IsNullOrEmpty(aSection))
            {
                throw new ArgumentException("Section cannot be empty");
            }

            BasePlug.At(Constants.CONFIG, XUri.EncodeFragment(aSection)).Get(DreamMessage.Ok(), new Result <DreamMessage>()).WhenDone(
                a =>
            {
                switch (a.Status)
                {
                case DreamStatus.Ok:
                    aResult.Return(JsonConvert.DeserializeObject <Dictionary <string, string> >(a.ToText()));
                    break;

                case DreamStatus.NotFound:
                    aResult.Return(new Dictionary <string, string>());
                    break;

                default:
                    aResult.Throw(new CouchException(a));
                    break;
                }
            },
                aResult.Throw
                );
            return(aResult);
        }