示例#1
0
        /// <summary>
        /// Loads the jokes from API
        /// </summary>
        /// <returns> array with data </returns>
        public static async Task <string[]> LoadJokes()
        {
            string[]   jokesData = new string[2];
            JokesModel jokes     = await Jokes.LoadValue()
                                   .ConfigureAwait(false);

            jokesData[0] = jokes.Setup;
            jokesData[1] = jokes.Punchline;

            string output = string.Format(Regex.Unescape(
                                              ConfigUtils.JokesString), jokesData[0],
                                          jokesData[1]);

            Console.WriteLine(output);

            return(jokesData);
        }
示例#2
0
        /// <summary>
        /// Loads specific value
        /// </summary>
        public static async Task <JokesModel> LoadValue()
        {
            // open a call to a client
            // or open a new request of ApiClient as await for response
            using (HttpResponseMessage response = await ApiHelper.ApiClient.
                                                  GetAsync(ConfigUtils.JokesURL))
            {
                if (response.IsSuccessStatusCode)
                {
                    // there We will read the data
                    // convert json to ValueByNameModel with correct properties
                    JokesModel jokesData = await response.Content.
                                           ReadAsAsync <JokesModel>();

                    return(jokesData);
                }
                else
                {
                    throw new Exception(response.ReasonPhrase);
                }
            }
            // there a response is closing and make sure, that Our calls are cleaned up
        }