Пример #1
0
        /// <summary>
        /// Loads specific value
        /// </summary>
        public static async Task <AgeByNameModel> LoadValue(string text)
        {
            string url;

            if (text != ConfigUtils.DefaultName)
            {
                url = $"{ConfigUtils.AgeByNameURL}{ text } ";
            }
            else
            {
                url = $"{ConfigUtils.AgeByNameURL}{ ConfigUtils.DefaultName }";
            }

            // open a call to a client
            // or open a new request of ApiClient as await for response
            using (HttpResponseMessage response = await ApiHelper.ApiClient.
                                                  GetAsync(url))
            {
                if (response.IsSuccessStatusCode)
                {
                    // there We will read the data
                    // convert json to ValueByNameModel with correct properties
                    AgeByNameModel age = await response.Content.ReadAsAsync
                                         <AgeByNameModel>();

                    return(age);
                }
                else
                {
                    throw new Exception(response.ReasonPhrase);
                }
            }
            // there a response is closing and make sure, that Our calls are cleaned up
        }
Пример #2
0
        /// <summary>
        /// Loads the name age from API
        /// </summary>
        /// <param name="name">Name of person</param>
        /// <returns> age </returns>
        public static async Task <int> LoadNameAge(string name)
        {
            AgeByNameModel age = await AgeByName.
                                 LoadValue(name).ConfigureAwait(false);

            return(age.Age);
        }