/// <summary> /// Handles the response from the NASA API. /// </summary> /// <param name="nasaResponse"></param> /// <returns></returns> public ImageFileResponse HandleNasaResponse(NasaPhotoResponse nasaResponse) { var imageFileResponse = new ImageFileResponse { count = nasaResponse.Photos.Count, isSuccessful = false }; if (!nasaResponse.Photos.Any()) { return(imageFileResponse); } // get the date from the first photo and put on the end of the path _path = $"{_path}/{nasaResponse.Photos[0].EarthDate:yyyyMMdd}"; // Create directory if it doesn't already exist if (!System.IO.Directory.Exists(_path)) { System.IO.Directory.CreateDirectory(_path); } // Process each image concurrently Parallel.ForEach(nasaResponse.Photos, photo => SavePhoto(photo)); return(new ImageFileResponse { count = nasaResponse.Photos.Count, isSuccessful = true, location = _path }); }
public void CanSavePhotosFromResponseToDefaultDirectory() { var photoResponse = new NasaPhotoResponse { Photos = new List <NasaPhoto> { new NasaPhoto { EarthDate = DateTime.Now, Source = "https://mars.jpl.nasa.gov/msl-raw-images/proj/msl/redops/ods/surface/sol/01000/opgs/edr/fcam/FLB_486265257EDR_F0481570FHAZ00323M_.JPG" } } }; var imageFileService = new ImageFileService(); var imageFileResponse = imageFileService.HandleNasaResponse(photoResponse); Assert.IsTrue(imageFileResponse.location.StartsWith(ImageFileService.DefaultBasePath)); }