Beispiel #1
0
        public async Task <MarsRoverPhotos> GetMarsRoverPhotosByDateAsync(string datevalue)
        {
            try
            {
                MarsRoverPhotos objMarsRoverPhotos = new MarsRoverPhotos();
                DateTime        currentdate        = new DateTime();
                string          result;
                CultureInfo     provider = CultureInfo.InvariantCulture;

                if (DateTime.TryParse(datevalue, out currentdate))
                {
                    result = Convert.ToDateTime(datevalue).ToString("yyyy-MM-dd");
                    string baseURL  = _configuration.GetValue <string>("ConfigurationSettings:MarsRoverPhotosAPI");
                    string apiKey   = "IVSY73JJjs28z7GYrbYdBCm7O5ewzc5KlqsJD87a";
                    string finalURL = baseURL + "earth_date=" + result + "&api_key=" + apiKey;

                    //Configure httpclient for call

                    // Add an Accept header for JSON format.
                    nasaclient.DefaultRequestHeaders.Accept.Add(
                        new MediaTypeWithQualityHeaderValue("application/json"));

                    //send request to provenir
                    var response = await nasaclient.GetAsync(finalURL);

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        //deserialize to object again
                        objMarsRoverPhotos         = JsonConvert.DeserializeObject <MarsRoverPhotos>(response.Content.ReadAsStringAsync().Result);
                        objMarsRoverPhotos.message = "Success";
                    }
                    else
                    {
                        objMarsRoverPhotos.message = "Error calling Nasa Mars API";
                        _logger.LogCritical("Error occured while performing call to NasaMarsRoverPhotos API. Date: " + datevalue.ToString() + " Exception: " + response.Content.ReadAsStringAsync().Result);
                        return(objMarsRoverPhotos);
                    }

                    //deserialize to object again
                    return(objMarsRoverPhotos);
                }

                else
                {
                    objMarsRoverPhotos.message = "Provided Datetime Not Valid";
                    return(objMarsRoverPhotos);
                }
            }
            catch (Exception ex)
            {
                _logger.LogCritical("Error occured while performing call to GetMarsRoverPhotosByDateAsync. Date: " + datevalue.ToString() + " Exception: " + ex.ToString() + " Exception Trace: " + ex.StackTrace);
                MarsRoverPhotos objMarsRoverPhotos = new MarsRoverPhotos();
                objMarsRoverPhotos.message = "Error occured while processing your request. Please contact your administrator.";
                return(objMarsRoverPhotos);
            }
        }
Beispiel #2
0
        public async Task<IActionResult> GetMarsRoverPhotosByDate(string datevalue)
        {
            try
            {
                MarsRoverPhotos objMarsRoverPhotos = new MarsRoverPhotos();
                DateTime currentdate = new DateTime();
                string result;
                CultureInfo provider = CultureInfo.InvariantCulture;
                if (DateTime.TryParse(datevalue, out currentdate))
                {
                    result = Convert.ToDateTime(datevalue).ToString("yyyy-MM-dd");
                    objMarsRoverPhotos = await _nasaMarsapiService.GetMarsRoverPhotosByDateAsync(result.ToString());

                    if (!(Directory.Exists(_configuration.GetValue<string>("ConfigurationSettings:Path"))))
                    {
                        Directory.CreateDirectory(_configuration.GetValue<string>("ConfigurationSettings:Path"));
                    }

                    foreach (Photo photo in objMarsRoverPhotos.photos)
                    {
                        using (WebClient client = new WebClient())
                        {
                            string fileName = Path.GetFileName(photo.img_src);
                            client.DownloadFileTaskAsync(new Uri(photo.img_src), _configuration.GetValue<string>("ConfigurationSettings:Path") + datevalue + "-" + fileName).Wait();
                        }
                    }
                }
                else
                {
                    objMarsRoverPhotos.message = "Provided Datetime Not Valid";
                }

                return View ("~/Views/MarsRoverImages.cshtml", objMarsRoverPhotos);
            }
            catch (Exception ex)
            {
                MarsRoverPhotos objMarsRoverPhotos = new MarsRoverPhotos();
                objMarsRoverPhotos.message = "Error occured while processing your request. Please contact your administrator.";
                _logger.LogCritical("Error occured while performing call to GetMarsRoverPhotosByDate. Date: " + datevalue.ToString() + " Exception: " + ex.ToString() + " Exception Trace: " + ex.StackTrace);
                return View("~/Views/MarsRoverImages.cshtml", objMarsRoverPhotos);
            }
        }