Пример #1
0
        /// <summary>
        /// Call Mars Rover Photos API Request for the specified date
        /// </summary>
        /// <param name="photoDate"></param>
        /// <returns></returns>
        public async Task <HTTPResponseDetails> GetNASAPhotosAsync(string photoDate)
        {
            HTTPResponseDetails responseDetails = new HTTPResponseDetails();
            string response;
            Stream dataStream;
            string method   = "GET";
            string endpoint = $"{_uri}?earth_date={photoDate}&api_key={_apiKey}"; //?earth_date=2020-12-11&api_key=DEMO_KEY

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endpoint);
                request.Method = method;
                request.Date   = DateTime.Now;

                using (HttpWebResponse webResponse = (HttpWebResponse)await request.GetResponseAsync())
                {
                    // Get content received after request
                    dataStream = webResponse.GetResponseStream();

                    StreamReader reader = new StreamReader(dataStream);

                    // Read to the end of the stream
                    response = reader.ReadToEnd();

                    // Save the response and the response code into the object returned by this method
                    responseDetails.ResponseResult = response;
                    responseDetails.ResponseStatus = webResponse.StatusCode;
                }
            }
            catch (WebException ex)
            {
                if (ex?.Response != null)
                {
                    using (WebResponse exResponse = ex.Response)
                    {
                        HttpWebResponse exWebResponse = (HttpWebResponse)exResponse;
                        using (Stream reader = exResponse.GetResponseStream())
                        {
                            string responseStream = new StreamReader(reader).ReadToEnd();
                            responseDetails.ResponseResult = responseStream;
                        }
                        responseDetails.ResponseStatus = exWebResponse.StatusCode;
                    }
                }
                else
                {
                    responseDetails.ResponseResult = ex.ToString();
                }
            }
            catch (Exception ex)
            {
                //Do some logging
            }
            return(responseDetails);
        }
Пример #2
0
        /// <summary>
        /// Download NASA files for each date in Dates.txt
        /// </summary>
        /// <returns></returns>
        public async Task DownLoadNASAPhotos()
        {
            try
            {
                // Create the required folder structure
                _fileService.CreateRequiredFolders();

                // Retrieve list of dates from Dates.txt
                List <string> dateList = _fileService.ReadDatesFile();

                // Process each date
                foreach (string ds in dateList)
                {
                    // Convert each date to format "yyyy-MM-dd" i.e. 2020-12-01
                    string dateString = ConvertDateString(ds);

                    // Check for date in the correct format
                    if (dateString != null)
                    {
                        // Call Mars Rover Photos API Request for the specified date
                        HTTPResponseDetails responseDetails = await _apiService.GetNASAPhotosAsync(dateString);

                        // If the response is ok, deserialize to NASA_ResponseObject model
                        if (responseDetails.ResponseStatus == System.Net.HttpStatusCode.OK)
                        {
                            // Deserialize the response
                            NASA_ResponseObject nasaResponseObject = JsonConvert.DeserializeObject <NASA_ResponseObject>(responseDetails.ResponseResult);

                            // Loop through the List<Photo>
                            foreach (Photo p in nasaResponseObject.photos)
                            {
                                // Get the file name part of the file name
                                string filename = Path.GetFileName(p.img_src);

                                // Save the photo to a local folder
                                await _fileService.SaveNASAPhoto(p.img_src, $"{_outputFilePath}{filename}");
                            }
                        }
                        else
                        {
                            // Do some logging
                            throw new Exception();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //Do Some logging, rethrow
                throw;
            }
        }