Beispiel #1
0
        private async Task <string> GetCsvFileFromUrl(CsvDataType csvDataType)
        {
            using HttpClient httpClient = _httpClientFactory.CreateClient();

            HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post,
                                                                       "http://database.ukrcensus.gov.ua/statbank_lviv/Dialog/SaveShow.asp");

            switch (csvDataType)
            {
            case CsvDataType.ByActivityType:
                requestMessage.Content = new StringContent(HttpRequestContentForDataByActivityType, Encoding.UTF8,
                                                           "application/x-www-form-urlencoded");
                break;

            case CsvDataType.ByCity:
                requestMessage.Content = new StringContent(HttpRequestContentForDataByCity, Encoding.UTF8,
                                                           "application/x-www-form-urlencoded");
                break;
            }

            HttpResponseMessage response = await httpClient.SendAsync(requestMessage);

            var responseContentBytes = await response.Content.ReadAsByteArrayAsync();

            Encoding win1251         = Encoding.GetEncoding("windows-1251");
            var      convertedString = win1251.GetString(responseContentBytes);

            return(convertedString);
        }
Beispiel #2
0
        public EnergyUsage ParseCsvData(string inputString, CsvDataType csvDataType)
        {
            List <List <string> > parsedRowValues = ParseRows(inputString);

            switch (csvDataType)
            {
            case CsvDataType.ByActivityType:
                if (!TryParseYears(parsedRowValues, out var years1, out var indexOfYearsRow1) ||
                    !TryParseTerritoryName(parsedRowValues, out var territoryName) ||
                    !TryParseEnergyTypesWithActivities(parsedRowValues.Skip(indexOfYearsRow1 + 1).ToList(), years1, out var energyTypes1))
                {
                    return(null);
                }

                return(new EnergyUsage()
                {
                    TerritoryName = territoryName,
                    EnergyTypes = energyTypes1,
                    Years = years1
                });

            case CsvDataType.ByCity:
                if (!TryParseYears(parsedRowValues, out var years2, out var indexOfYearsRow2) ||
                    !TryParseEnergyTypesWithCities(parsedRowValues.Skip(indexOfYearsRow2 + 1).ToList(), years2, out var energyTypes2))
                {
                    return(null);
                }

                return(new EnergyUsage
                {
                    EnergyTypes = energyTypes2,
                    Years = years2
                });

            default:
                return(null);
            }
        }