public Service Connect(Namespace ns)
        {
            var service = new Service(Scheme.Https, this.command.Host, this.command.Port, ns);
            service.LoginAsync(this.command.Username, this.command.Password).Wait();

            return service;
        }
        /// <summary>
        /// Runs the specified service.
        /// </summary>
        /// <param name="service">The service.</param>
        /// <returns>a task</returns>
        private static async Task Run(Service service)
        {
            try
            {
                await service.GetConfigurationsAsync();
            }
            catch (AuthenticationFailureException e)
            {
                Console.WriteLine("Can't get service configuration without log in");
            }

            Console.WriteLine("Login as admin");
            string username = "******";
            string password = "******";
            await service.LoginAsync(username, password);

            Console.WriteLine("List all configurations of the Splunk service:");
            ConfigurationCollection configs = service.GetConfigurationsAsync().Result;
            foreach (Configuration config in configs)
            {
                Console.WriteLine(config.Id);
            }

            Console.WriteLine("Log off");
            await service.LogoffAsync();
        }
        /// <summary>
        /// The main program
        /// </summary>
        /// <param name="argv">The command line arguments</param>
        static async Task Run(Service service)
        {
            Console.WriteLine("Login as admin");
            string username = "******";
            string password = "******";
            await service.LoginAsync(username, password);

            Console.WriteLine("create a  index");
            string indexName = "user-index";
            string source = "*\\splunkd.log";
            string sourceType = "splunkd";

            if (service.GetIndexesAsync().Result.Any(a => a.Name == indexName))
            {
                await service.RemoveIndexAsync(indexName);
            }

            Index index = await service.CreateIndexAsync(indexName);
            try
            {
                await index.EnableAsync();

                Receiver receiver = service.Receiver;
                ReceiverArgs args = new ReceiverArgs()
                {
                    Index = indexName,
                    Source = source,
                    SourceType = sourceType,
                };

                await receiver.SendAsync("Hello World.", args);
                await receiver.SendAsync("Goodbye world.", args);

                SearchResults results = service.SearchOneshotAsync(
                    string.Format(
                        "search index={0}",// source={2} sourcetype={3}",
                        indexName
                        //source,
                        //sourceType
                        )).Result;

                Console.WriteLine(results);              
            }
            finally
            {
                service.RemoveIndexAsync(indexName).Wait();
            }
        }
        public void JobRefreshTest()
        {
            var cli = Command.Splunk("search");
            cli.AddRule("search", typeof(string), "search string");
            cli.Opts["search"] = "search index=_internal * | head 10 ";

            var service = new Service(Scheme.Https, "localhost", 8089);
            service.LoginAsync("admin", "changeme").Wait();
            var job = service.CreateJobAsync((string)cli.Opts["search"]).Result;

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            TimeSpan max = new TimeSpan(0, 0, 0, 10);

            while (!job.IsDone)
            {
                Thread.Sleep(1000);

                //has to call this to get the job.IsCompleted
                job.GetAsync().Wait();
                Console.WriteLine("jobUpdated={0}", job.Updated);

                if (stopwatch.Elapsed > max)
                {
                    Assert.False(true, string.Format("The job is not finished within expected time {0} seconds", max.TotalSeconds));
                }
            }

            job.CancelAsync().Wait();
        }
        public void ServiceLogin()
        {
            //ResponseMessage response;

            Service service = new Service(Scheme.Https, this.SetUp().Host, this.SetUp().Port);
            ConfigurationCollection config;

            // Not logged in, should fail with 401
            try
            {
                //response = service.Get("/services/authentication/users");
                config = service.GetConfigurationsAsync().Result;
                Assert.True(false, "Expected HttpException");
            }
            catch (WebException ex)
            {
                Assert.Equal(401, ((HttpWebResponse)ex.Response).StatusCode.GetHashCode());
            }
            catch (Exception e)
            {
                //TODO, if dev fix the aggregate exception issue, should only catch the above exception
                Assert.True(e.InnerException.Message.Contains("401"));
            }

            // Logged in, request should succeed
            service.LoginAsync(this.SetUp().Username, this.SetUp().Password).Wait();
            //response = service.Get("/services/authentication/users");
            //this.CheckResponse(response);
            config = service.GetConfigurationsAsync().Result;
            Assert.NotNull(config);

            //// Logout, the request should fail with a 401
            service.LogoffAsync().Wait();
            try
            {
                //response = service.Get("/services/authentication/users");
                config = service.GetConfigurationsAsync().Result;    
                Assert.True(false, "Expected HttpException");
            }
            catch (WebException ex)
            {
                Assert.Equal(401, ((HttpWebResponse)ex.Response).StatusCode.GetHashCode());
            }
            catch (Exception e)
            {
                //TODO, if dev fix the aggregate exception issue, should only catch the above exception
                Assert.True(e.InnerException.Message.Contains("401"));
            }
        }