public void WriteToJsonFileTest() { string path = "D:\\Weather Info\\Source"; var directoryInfo = FileIOHelper.CreateDestinationFolder(path); var now = DateTime.Now; var yearName = now.ToString("yyyy"); var monthName = now.ToString("MMMM"); var dayName = now.ToString("dd-MM-yyyy"); string filepath = $"{path}\\{yearName}\\{monthName}\\{dayName}"; City city = new City() { Cityname = "London", CityId = "2643741" }; IWeatherFetcher wf = new WeatherFetcher("http://api.openweathermap.org", "aa69195559bd4f88d79f9aadeb77a8f6"); var currentWeather = wf.GetCurrentWeather(city.CityId); var destinationFilepath = $"{filepath}\\{city.Cityname}_{city.CityId}.txt"; FileIOHelper.WriteToJsonFile <CurrentWeather>(destinationFilepath, currentWeather, append: false); var DoesFileExist = File.Exists(destinationFilepath); Assert.IsTrue(DoesFileExist); }
public async Task <IActionResult> Index(IFormFile file) { if (file == null || file.Length == 0) { return(Content("file not selected")); } var destPath = _configuration["DestinationDir"]; using (var stream = new FileStream(file.FileName, FileMode.Create)) { await file.CopyToAsync(stream); } IEnumerable <City> cities = FileIOHelper.ReadFromFile(file.FileName); Parallel.ForEach(cities, (currentCity) => { IWeatherFetcher wf = new WeatherFetcher(_configuration["WeatherAPIUrl"], _configuration["WeatherAPIKey"]); var currentWeather = wf.GetCurrentWeather(currentCity.CityId); var dirInfo = FileIOHelper.CreateDestinationFolder(destPath); FileIOHelper.WriteToJsonFile <CurrentWeather>($"{dirInfo.FullName}\\{currentCity.Cityname}_{currentCity.CityId} .txt", currentWeather, append: false); }); ViewBag.Message = string.Format("Report generated at specified destination path.\\nCurrent Date and Time: {0}", DateTime.Now.ToString()); return(View()); }
static void Main(string[] args) { var path = ConfigurationManager.AppSettings["SourceFilePath"]; IEnumerable <City> cities = UtilityHelper.ReadFromFile(path + "\\" + "CityList.txt"); Parallel.ForEach(cities, (currentcity) => { IWeatherFetcher wf = new WeatherFetcher(); var currentWeather = wf.GetCurrentWeather(currentcity.ZipCode); var dirInfo = UtilityHelper.CreateDestinationFolder(ConfigurationManager.AppSettings["DestinationPath"]); UtilityHelper.WriteToJsonFile <CurrentWeather>($"{dirInfo.FullName} \\ {currentcity.Cityname}_{currentcity.ZipCode} .txt", currentWeather, append: false); } ); Console.ReadLine(); }