Ejemplo n.º 1
0
        private async Task <T> GetDataAsync <T>(string cacheKey, string url) where T : class
        {
            T   result    = null;
            var cacheData = _cacheDataProvider.Get(cacheKey);

            try
            {
                if (string.IsNullOrEmpty(cacheData))
                {
                    if (_networkProvider.IsConnected())
                    {
                        var requestUrl = url.Contains(Constants.ApiAutoKey)
                     ? $"{Constants.ApiAutoBaseUrl}{url}"
                     : $"{Constants.ApiAutoBaseUrl}{url}?api_key={Constants.ApiAutoKey}";

                        var response = await _httpClient.GetAsync(requestUrl);

                        if (response.IsSuccessStatusCode)
                        {
                            cacheData = await response.Content.ReadAsStringAsync();

                            _cacheDataProvider.Set(cacheKey, cacheData);

                            result = await _jsonProvider.DeserializeAsync <T>(cacheData);
                        }
                        else
                        {
                            _statisticDataProvider.SendAsync(Projects.AutoLogs, response);
                        }
                    }
                }
                else
                {
                    result = await _jsonProvider.DeserializeAsync <T>(cacheData);
                }
            }
            catch (Exception ex)
            {
                _statisticDataProvider.SendAsync(Projects.AutoLogs, ex);
            }

            return(result);
        }