Пример #1
0
        /// <summary>
        /// Generic Get method which returns a JSON object of type T
        /// </summary>
        /// <param name="searchString">The string to be used in the query string</param>
        /// <param name="applicationName">the name of the http client instance</param>
        /// <returns></returns>
        public async Task <T> GetAsync(string searchString, string applicationName)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(searchString))
                {
                    _url = _url.Replace("{searchString}", searchString);
                    using (var client = _clientFactory.CreateClient(applicationName))
                    {
                        var response = await client.GetStringAsync(_url);

                        response = response.Replace("jsonFlickrApi", "").Replace("(", "").Replace(")", "");

                        T objectValue = JsonConvert.DeserializeObject <T>(response);
                        _logger.LogToFile(response, _path);
                        return(objectValue);
                    }
                }
                else
                {
                    throw new NullReferenceException();
                }
            }
            catch (Exception ex)
            {
                _logger.LogToFile(ex.Message, _path);
                return(null);
            }
        }
Пример #2
0
        public async Task <Root> GetAsync(string searchString, string applicationName)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(searchString))
                {
                    var url = _configuration.GetSection("ApiCallSettings").GetSection("url").Value;
                    url = url.Replace("{searchString}", searchString);
                    using (var client = _clientFactory.CreateClient(applicationName))
                    {
                        var result = await client.GetStringAsync(url);

                        result = result.Replace("jsonFlickrApi", "").Replace("(", "").Replace(")", "");


                        Root value = JsonConvert.DeserializeObject <Root>(result);
                        _logger.LogToFile(result);
                        return(value);
                    }
                }
                else
                {
                    throw new ArgumentNullException();
                }
            }
            catch (Exception ex)
            {
                _logger.LogToFile(ex.Message);
                return(null);;
            }
        }