protected async Task <string> SaveMarsPhotoImageAsync(NasaMarsPhoto nasaMarsPhoto)
        {
            string imagePath = null;

            _logger.LogInformation($"SaveMarsPhotoImageAsync Id: {nasaMarsPhoto.Id} ImgSrc: {nasaMarsPhoto.ImgSrc}");

            try
            {
                byte[] image = await _nasaApiClient.GetRoverPhotoAsync(nasaMarsPhoto.ImgSrc);

                var uri           = new Uri(nasaMarsPhoto.ImgSrc);
                var imageFileName = uri.Segments.LastOrDefault();

                if (imageFileName != null)
                {
                    imagePath = await _imageService.SaveImageAsync(_imagePaths.MarsRoverImagePath, imageFileName, image);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to save image Id: {nasaMarsPhoto.Id} ImgSrc: {nasaMarsPhoto.ImgSrc} Error: {ex.Message}");
            }

            return(imagePath);
        }
Esempio n. 2
0
        public void Test_MarsPhotoMapping()
        {
            var config = new MapperConfiguration(cfg => cfg.AddProfile <MappingProfile>());
            var mapper = config.CreateMapper();

            config.AssertConfigurationIsValid();

            NasaMarsPhoto nasaMarsPhoto = new NasaMarsPhoto()
            {
                Id     = 617458,
                Sol    = 1622,
                Camera = new NasaCamera()
                {
                    Id       = 20,
                    Name     = "FHAZ",
                    RoverId  = 5,
                    FullName = "Front Hazard Avoidance Camera"
                },
                ImgSrc    = "http://mars.jpl.nasa.gov/msl-raw-images/proj/msl/redops/ods/surface/sol/01622/opgs/edr/fcam/FLB_541484941EDR_F0611140FHAZ00341M_.JPG",
                EarthDate = "2017-02-27",
                Rover     = new NasaRover()
                {
                    Id          = 5,
                    Name        = "Curiosity",
                    LandingDate = "2012-08-06",
                    LaunchDate  = "2011-11-26",
                    Status      = "active"
                }
            };

            var marsPhoto = mapper.Map <MarsPhoto>(nasaMarsPhoto);

            Assert.Equal(nasaMarsPhoto.Id, marsPhoto.Id);
            Assert.Equal(nasaMarsPhoto.ImgSrc, marsPhoto.ImgSrc);
            Assert.Equal(nasaMarsPhoto.EarthDate, marsPhoto.EarthDate);
        }