Exemple #1
0
        /// <summary>
        /// Fetch all the Astronomy Pictures of the Day between two dates (inclusive).
        /// </summary>
        /// <remarks>
        /// <para>
        /// You do not need to consider time zone differences between your application and NASA's API.
        /// If <paramref name="endDate"/> has today's date (according to your local time zone), the method will take care of it accordingly.
        /// </para>
        /// <para>
        /// Note that this means that if you are ahead of the Eastern Time Zone and ask for all APODs between yesterday and today just after midnight (locally),
        /// you would only get one APOD with (for you) yesterday's date, since a picture for your "today" doesn't exist yet.
        /// </para>
        /// <para>
        /// It is therefore not possible to safely assume the amount of APODs this method will return,
        /// since it depends on time zone differences and the current time.
        /// </para>
        /// </remarks>
        /// <example>
        /// <code language="csharp">
        /// using var client = new ApodClient();
        ///
        /// var startDate = new DateTime(2008, 10, 29);
        /// var endDate = new DateTime(2008, 11, 02);
        /// var response = await client.FetchApodAsync(startDate, endDate);
        ///
        /// if (response.StatusCode != ApodStatusCode.OK) { return; }
        ///
        /// foreach (var apod in response.AllContent)
        /// {
        ///     Console.WriteLine($"{apod.Date}: {apod.Title}");
        /// }
        /// </code>
        /// </example>
        /// <exception cref="ObjectDisposedException">Thrown when the client has been disposed.</exception>
        /// <param name="startDate">The start date. Must be between June 16th 1995 and today's date (inclusive).</param>
        /// <param name="endDate">The end date. Must be between the <paramref name="startDate"/> and today's date (inclusive). Defaults to today's date.</param>
        /// <returns>The response.</returns>
        public async ValueTask <ApodResponse> FetchApodAsync(DateTime startDate, DateTime endDate = default)
        {
            ThrowExceptionIfDisposed();

            if (endDate.Date == DateTime.Today)
            {
                endDate = default;
            }

            var dateError = _errorHandler.ValidateDateRange(startDate, endDate);

            if (dateError.ErrorCode != ApodErrorCode.None)
            {
                return(dateError.ToApodResponse());
            }

            var httpResponse = await _httpRequester.SendHttpRequestAsync(startDate, endDate).ConfigureAwait(false);

            var responseError = await _errorHandler.ValidateHttpResponseAsync(httpResponse).ConfigureAwait(false);

            if (responseError.ErrorCode != ApodErrorCode.None)
            {
                return(responseError.ToApodResponse());
            }

            return(await _httpResponseParser.ParseMultipleApodsAsync(httpResponse).ConfigureAwait(false));
        }