Beispiel #1
0
        public async Task <NetworkStatus <List <CovidCountryViewModel> > > LoadCountryCovidCases()
        {
            _covidCountryData.Clear();
            var result = new NetworkStatus <List <CovidCountryViewModel> >();

            var pageSourceResult = await _httpClientMgr.GetAsync(_covidCasesNowUrl);

            if (!pageSourceResult.Succeeded)
            {
                result.ErrorMessage = "Error loading content from " + _covidCasesNowUrl;
                result.ErrorCode    = pageSourceResult.ErrorCode;
                return(result);
            }

            var parseSuccess = TryParseWorldometersSiteData(pageSourceResult.Result);

            if (!parseSuccess)
            {
                result.ErrorMessage = "Error parsing content from " + _covidCasesNowUrl;
            }
            else
            {
                result.Result = new List <CovidCountryViewModel>();
                foreach (var c in _covidCountryData)
                {
                    result.Result.Add(CovidCountryViewModel.Create(c));
                }
                result.Succeeded = true;
            }
            return(result);
        }
        public async Task <NetworkStatus <string> > GetAsync(string url)
        {
            var result = new NetworkStatus <string>()
            {
                Succeeded = false
            };
            bool done        = false;
            int  retryCounts = 0;

            while (!done)
            {
                done = true;
                try
                {
                    var response = await Client.GetAsync(url).ConfigureAwait(false);

                    response.EnsureSuccessStatusCode();
                    result.Succeeded = true;
                    result.Result    = await response.Content.ReadAsStringAsync();
                }
                catch (Exception ex)
                {
                    result.ErrorCode    = NetworkReturnUtils.ExceptionToReturnCode(ex);
                    result.ErrorMessage = ex.Message;
                    result.Ex           = ex;
                    if (result.ErrorCode == NetworkErrorCode.NetworkTimeout)
                    {
                        retryCounts++;
                        if (retryCounts < _maxRetryIfNetworkTimeout)
                        {
                            done = false;
                        }
                    }
                }
            }
            return(result);
        }