public async Task<string> Index()
        {
            try
            {
                string username = ConfigurationManager.AppSettings["Weee.WarmUpUserUsername"];
                string password = ConfigurationManager.AppSettings["Weee.WarmUpUserPassword"];

                if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
                {
                    return "Skipping warm-up - No user details provided.";
                }

                Stopwatch stopwatch = Stopwatch.StartNew();

                string baseUrl = ConfigurationManager.AppSettings["Weee.SiteRoot"];
                string clientId = ConfigurationManager.AppSettings["Weee.ApiClientID"];
                string clientSecret = ConfigurationManager.AppSettings["Weee.ApiSecret"];

                // Fetch an access token for the warm-up user.
                IOAuthClient client = new OAuthClient(baseUrl, clientId, clientSecret);
                var tokenResponse = await client.GetAccessTokenAsync(username, password);

                if (tokenResponse.AccessToken == null)
                {
                    if (tokenResponse.IsHttpError)
                    {
                        throw new Exception(string.Format("Request for access token returned HttpError: Status - {0}, Reason - {1}, Error - {2}",
                            tokenResponse.HttpErrorStatusCode, tokenResponse.HttpErrorReason, tokenResponse.Error));
                    }
                    else if (tokenResponse.IsError)
                    {
                        throw new Exception(string.Format("Request for access token returned Error: {0}", tokenResponse.Error));
                    }
                    else
                    {
                        throw new Exception("Request for access token returned null.");
                    }
                }

                // Fetch the user info for the warm-up user.
                IUserInfoClient userInfoClient = new UserInfoClient(baseUrl);
                var userInfo = await userInfoClient.GetUserInfoAsync(tokenResponse.AccessToken);

                return string.Format("Warm-up complete in {0} seconds.", stopwatch.Elapsed.TotalSeconds);
            }
            catch (Exception ex)
            {
                throw new Exception("API warm-up failed.", ex);
            }
        }
Example #2
0
        public async Task <string> Index()
        {
            try
            {
                string username = ConfigurationManager.AppSettings["Iws.WarmUpUserUsername"];
                string password = ConfigurationManager.AppSettings["Iws.WarmUpUserPassword"];

                if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
                {
                    return("Skipping warm-up - No user details provided.");
                }

                Stopwatch stopwatch = Stopwatch.StartNew();

                string baseUrl      = ConfigurationManager.AppSettings["Iws.SiteRoot"];
                string clientId     = ConfigurationManager.AppSettings["Iws.ApiClientID"];
                string clientSecret = ConfigurationManager.AppSettings["Iws.ApiSecret"];

                // Fetch an access token for the warm-up user.
                IOAuthClient client        = new OAuthClient(baseUrl, clientId, clientSecret);
                var          tokenResponse = await client.GetAccessTokenAsync(username, password);

                if (tokenResponse.AccessToken == null)
                {
                    if (tokenResponse.IsHttpError)
                    {
                        throw new Exception(string.Format("Request for access token returned HttpError: Status - {0}, Reason - {1}, Error - {2}",
                                                          tokenResponse.HttpErrorStatusCode, tokenResponse.HttpErrorReason, tokenResponse.Error));
                    }
                    else if (tokenResponse.IsError)
                    {
                        throw new Exception(string.Format("Request for access token returned Error: {0}", tokenResponse.Error));
                    }
                    else
                    {
                        throw new Exception("Request for access token returned null.");
                    }
                }

                // Fetch the user info for the warm-up user.
                IUserInfoClient userInfoClient = new UserInfoClient(baseUrl);
                var             userInfo       = await userInfoClient.GetUserInfoAsync(tokenResponse.AccessToken);

                return(string.Format("Warm-up complete in {0} seconds.", stopwatch.Elapsed.TotalSeconds));
            }
            catch (Exception ex)
            {
                throw new Exception("API warm-up failed.", ex);
            }
        }
Example #3
0
        private async Task ProcessRandomJokesAsync()
        {
            var useRandomName = Tuple.Create(false, (UserInfo)null);
            var useCategory   = Tuple.Create(false, (string)null);

            Output.Write("Want to use a random name? y/n");
            var input = Input.ReadKey();

            if (input == 'y')
            {
                var userNamesTask = UserInfoClient.GetUserInfoAsync();
                await AwaitTasksWithIndicator(userNamesTask);

                useRandomName = Tuple.Create(true, await userNamesTask);
            }

            Output.Write("Want to specify a category? y/n");
            input = Input.ReadKey();
            if (input == 'y')
            {
                var categoriesTask = ChuckNorrisClient.GetCategoriesAsync();
                await AwaitTasksWithIndicator(categoriesTask);

                PrintResults("Enter a category ", (await categoriesTask).ToList());
                var category = Input.ReadString();
                useCategory = Tuple.Create(true, category);
            }

            int inputNumber = 0;

            while (1 > inputNumber || inputNumber > 9)
            {
                Output.Write("How many jokes do you want? (1-9)");
                inputNumber = Input.ReadNumber();

                if (1 > inputNumber || inputNumber > 9)
                {
                    Output.Write("You input is wrong number. Please try again.");
                }
            }

            if (inputNumber > 1)
            {
                Output.Write("Want to process in parallel? y/n");
                input = Input.ReadKey();
            }

            Stopwatch sw = new Stopwatch();

            sw.Start();

            var  results   = new List <string>();
            var  jokeTasks = new Task <Joke> [inputNumber];
            Joke joke      = null;

            if (inputNumber > 1 && input == 'y')
            {
                for (int i = 0; i < inputNumber; i++)
                {
                    var jokeTask = useCategory.Item1 ?
                                   ChuckNorrisClient.GetJokeByCategoryNameAsync(useCategory.Item2) :
                                   ChuckNorrisClient.GetRandonJokeAsync();

                    jokeTasks[i] = jokeTask;
                }

                try
                {
                    await AwaitTasksWithIndicator(jokeTasks);

                    foreach (var jokeTask in jokeTasks)
                    {
                        joke = await jokeTask;

                        if (useRandomName.Item1)
                        {
                            joke = joke.ReplaceWith(useRandomName.Item2);
                        }

                        results.Add(joke.Value);
                    }
                }
                catch (HttpRequestException e)
                {
                    Output.Write(e.Message);
                    Output.Write("Hmm. Why did you do that?");
                }
            }
            else
            {
                try
                {
                    for (int i = 0; i < inputNumber; i++)
                    {
                        var jokeTask = useCategory.Item1 ?
                                       ChuckNorrisClient.GetJokeByCategoryNameAsync(useCategory.Item2) :
                                       ChuckNorrisClient.GetRandonJokeAsync();

                        await AwaitTasksWithIndicator(jokeTask);

                        joke = await jokeTask;

                        if (useRandomName.Item1)
                        {
                            joke = joke.ReplaceWith(useRandomName.Item2);
                        }

                        results.Add(joke.Value);
                    }
                }
                catch (HttpRequestException e)
                {
                    Output.Write(e.Message);
                    Output.Write("Hmm. Why did you do that?");
                }
            }

            sw.Stop();

            PrintResults(string.Empty, results);
            Output.WriteFormat("Done in {0} milliseconds.", sw.ElapsedMilliseconds);
        }