public IEnumerable <PetOwnerPerson> GetAllPetOwner() { try { string _petOwnerResultStream; bool _isProxyEnabled = Convert.ToBoolean(ConfigurationManager.AppSettings[Constants.UseProxy]); ProxyDetail _proxyDetail = null; if (_isProxyEnabled) { _proxyDetail = new ProxyDetail { Url = ConfigurationManager.AppSettings[Constants.ProxyUrl].ToString(), Port = ConfigurationManager.AppSettings[Constants.ProxyPort].ToString() }; } Dictionary <string, string> _headerDetails = new Dictionary <string, string>(); _headerDetails.Add("User-Agent", ConfigurationManager.AppSettings[Constants.ExtApiUserAgent].ToString()); bool _isMemoryCacheEnabled = Convert.ToBoolean(ConfigurationManager.AppSettings[Constants.MemCacheAppSettings]); //Check in Cache if (_isMemoryCacheEnabled) { if (null == this.cache.Get(Constants.PetOwnerCache, false)) { _petOwnerResultStream = APIHandler.GetAPIResult(ConfigurationManager.AppSettings[Constants.ApiUrl], _headerDetails, _isProxyEnabled, _proxyDetail); //.Result; // httpClient.GetStringAsync(new Uri(ConfigurationManager.AppSettings[Constants.ApiUrl])).Result; cache.Set(Constants.PetOwnerCache, _petOwnerResultStream, false, null); } else { _petOwnerResultStream = Convert.ToString(cache.Get(Constants.PetOwnerCache, false)); } } else { _petOwnerResultStream = APIHandler.GetAPIResult(ConfigurationManager.AppSettings[Constants.ApiUrl], _headerDetails, _isProxyEnabled, _proxyDetail); } JsonSerializerSettings settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore }; return(JsonConvert.DeserializeObject <List <PetOwnerPerson> >(_petOwnerResultStream, settings)); } catch (Exception ex) { Logging.HandleException(ex); throw; } }
public List <PetOwnerPerson> GetAllPetOwnerDetails() { try { string petOwnerResultStream; bool isProxyEnabled = Convert.ToBoolean(ConfigurationManager.AppSettings[Constants.UseProxy]); ProxyDetail proxyDetail = null; if (isProxyEnabled) { proxyDetail = new ProxyDetail { Url = ConfigurationManager.AppSettings[Constants.ProxyUrl], Port = ConfigurationManager.AppSettings[Constants.ProxyPort] }; } var headerDetails = new Dictionary <string, string> { { "User-Agent", ConfigurationManager.AppSettings[Constants.ExtApiUserAgent] } }; bool isMemoryCacheEnabled = Convert.ToBoolean(ConfigurationManager.AppSettings[Constants.MemCacheAppSettings]); if (isMemoryCacheEnabled) { if (_cache.Get(Constants.PetOwnerCache, false) == null) { petOwnerResultStream = ApiHandler.GetApiResult(ConfigurationManager.AppSettings[Constants.ApiUrl], headerDetails, isProxyEnabled, proxyDetail); _cache.Set(Constants.PetOwnerCache, petOwnerResultStream, false, null); } else { petOwnerResultStream = Convert.ToString(_cache.Get(Constants.PetOwnerCache, false)); } } else { petOwnerResultStream = ApiHandler.GetApiResult(ConfigurationManager.AppSettings[Constants.ApiUrl], headerDetails, isProxyEnabled, proxyDetail); } return(JsonConvert.DeserializeObject <List <PetOwnerPerson> >(petOwnerResultStream)); } catch (Exception ex) { Logging.HandleException(ex); throw; } }
public static string GetAPIResult(string apiUri, Dictionary <string, string> headerDetails = null, bool useProxy = false, ProxyDetail proxyDetail = null) { HttpClientHandler _httpClientHandler = new HttpClientHandler(); if (useProxy && null != proxyDetail) { _httpClientHandler.Proxy = new WebProxy(string.Format("{0}:{1}", proxyDetail.Url, proxyDetail.Port), false); _httpClientHandler.UseProxy = true; } using (HttpClient httpClient = new HttpClient(_httpClientHandler)) { httpClient.DefaultRequestHeaders.Clear(); if (null != headerDetails) { foreach (KeyValuePair <string, string> header in headerDetails) { httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); } } httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); return(httpClient.GetStringAsync(new Uri(apiUri)).Result); } }
public static string GetApiResult(string apiUri, Dictionary <string, string> headerDetails = null, bool useProxy = false, ProxyDetail proxyDetail = null) { var httpClientHandler = new HttpClientHandler(); if (useProxy && proxyDetail != null) { httpClientHandler.Proxy = new WebProxy($"{proxyDetail.Url}:{proxyDetail.Port}", false); httpClientHandler.UseProxy = true; } using (var httpClient = new HttpClient(httpClientHandler)) { httpClient.DefaultRequestHeaders.Clear(); if (headerDetails != null) { foreach (KeyValuePair <string, string> header in headerDetails) { httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); } httpClient.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); } return(httpClient.GetStringAsync(new Uri(apiUri)).Result); } }