Skip to content

Commit

Permalink
Make ApodClient implement IDisposable
Browse files Browse the repository at this point in the history
Closes #19
  • Loading branch information
MarcusOtter committed Nov 16, 2019
1 parent be7a9a8 commit 8096196
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
28 changes: 27 additions & 1 deletion src/Apod/ApodClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
namespace Apod
{
/// <summary>A client for interfacing with NASA's Astronomy Picture of the Day API.</summary>
public class ApodClient : IApodClient
public class ApodClient : IApodClient, IDisposable
{
private bool _disposed;

private readonly IHttpRequester _httpRequester;
private readonly IHttpResponseParser _httpResponseParser;
private readonly IErrorHandler _errorHandler;
Expand Down Expand Up @@ -39,6 +41,8 @@ public ApodClient(string apiKey, IHttpRequester httpRequester = null, IHttpRespo
/// <summary>Fetch the current Astronomy Picture of the Day.</summary>
public async Task<ApodResponse> FetchApodAsync()
{
ThrowExceptionIfDisposed();

var httpResponse = await _httpRequester.SendHttpRequestAsync();

var responseError = await _errorHandler.ValidateHttpResponseAsync(httpResponse);
Expand All @@ -51,6 +55,8 @@ public async Task<ApodResponse> FetchApodAsync()
/// <param name="dateTime">The date to request the APOD for. Must be between June 16th 1995 and today's date.</param>
public async Task<ApodResponse> FetchApodAsync(DateTime dateTime)
{
ThrowExceptionIfDisposed();

if (dateTime.Date == DateTime.Today) { return await FetchApodAsync(); }

var dateError = _errorHandler.ValidateDate(dateTime);
Expand All @@ -69,6 +75,8 @@ public async Task<ApodResponse> FetchApodAsync(DateTime dateTime)
/// <param name="endDate">The end date. Must be between the <paramref name="startDate"/> and today's date. Defaults to <see cref="DateTime.Today"/>.</param>
public async Task<ApodResponse> FetchApodAsync(DateTime startDate, DateTime endDate = default)
{
ThrowExceptionIfDisposed();

var dateError = _errorHandler.ValidateDateRange(startDate, endDate);
if (dateError.ErrorCode != ApodErrorCode.None) { return dateError.ToApodResponse(); }

Expand All @@ -84,6 +92,8 @@ public async Task<ApodResponse> FetchApodAsync(DateTime startDate, DateTime endD
/// <param name="count">The amount of APODs to fetch. Must be positive and cannot exceed 100.</param>
public async Task<ApodResponse> FetchApodAsync(int count)
{
ThrowExceptionIfDisposed();

var countError = _errorHandler.ValidateCount(count);
if (countError.ErrorCode != ApodErrorCode.None) { return countError.ToApodResponse(); }

Expand All @@ -94,5 +104,21 @@ public async Task<ApodResponse> FetchApodAsync(int count)

return await _httpResponseParser.ParseMultipleApodAsync(httpResponse);
}

private void ThrowExceptionIfDisposed()
{
if (_disposed) { throw new ObjectDisposedException(GetType().FullName); }
}

/// <summary>
/// Releases the unmanaged resources and disposes of the managed resources used by the System.Net.Http.HttpMessageInvoker.
/// </summary>
public void Dispose()
{
if (_disposed) { return; }
_httpRequester.Dispose();
GC.SuppressFinalize(this);
_disposed = true;
}
}
}
2 changes: 1 addition & 1 deletion src/Apod/IApodClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Apod
{
public interface IApodClient
public interface IApodClient : IDisposable
{
Task<ApodResponse> FetchApodAsync();
Task<ApodResponse> FetchApodAsync(DateTime dateTime);
Expand Down
13 changes: 9 additions & 4 deletions src/Apod/Logic/Net/HttpRequester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private HttpClient GetDefaultHttpClient()

public async Task<HttpResponseMessage> SendHttpRequestAsync()
{
if (_disposed) { throw new ObjectDisposedException(GetType().FullName); }
ThrowExceptionIfDisposed();

var uri = _uriBuilder.GetApodUri();

Expand All @@ -34,7 +34,7 @@ public async Task<HttpResponseMessage> SendHttpRequestAsync()

public async Task<HttpResponseMessage> SendHttpRequestAsync(DateTime dateTime)
{
if (_disposed) { throw new ObjectDisposedException(GetType().FullName); }
ThrowExceptionIfDisposed();

var uri = _uriBuilder.GetApodUri(dateTime);
using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, uri))
Expand All @@ -45,7 +45,7 @@ public async Task<HttpResponseMessage> SendHttpRequestAsync(DateTime dateTime)

public async Task<HttpResponseMessage> SendHttpRequestAsync(DateTime startDate, DateTime endDate = default)
{
if (_disposed) { throw new ObjectDisposedException(GetType().FullName); }
ThrowExceptionIfDisposed();

var uri = _uriBuilder.GetApodUri(startDate, endDate);
using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, uri))
Expand All @@ -56,7 +56,7 @@ public async Task<HttpResponseMessage> SendHttpRequestAsync(DateTime startDate,

public async Task<HttpResponseMessage> SendHttpRequestAsync(int count)
{
if (_disposed) { throw new ObjectDisposedException(GetType().FullName); }
ThrowExceptionIfDisposed();

var uri = _uriBuilder.GetApodUri(count);
using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, uri))
Expand All @@ -65,6 +65,11 @@ public async Task<HttpResponseMessage> SendHttpRequestAsync(int count)
}
}

private void ThrowExceptionIfDisposed()
{
if (_disposed) { throw new ObjectDisposedException(GetType().FullName); }
}

public void Dispose()
{
if (_disposed) { return; }
Expand Down
2 changes: 1 addition & 1 deletion src/Apod/Logic/Net/IHttpRequester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Apod.Logic.Net
{
public interface IHttpRequester
public interface IHttpRequester : IDisposable
{
Task<HttpResponseMessage> SendHttpRequestAsync();
Task<HttpResponseMessage> SendHttpRequestAsync(DateTime dateTime);
Expand Down

0 comments on commit 8096196

Please sign in to comment.