Skip to content

Commit

Permalink
Add protected virtual methods to MediaDownloader for closer response …
Browse files Browse the repository at this point in the history
…inspection
  • Loading branch information
jskeet committed Dec 13, 2016
1 parent a406f8b commit cbcefc0
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Src/Support/GoogleApis/Apis/[Media]/Download/MediaDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ private async Task<IDownloadProgress> DownloadCoreAsync(string url, Stream strea
throw await MediaApiErrorHandling.ExceptionForResponseAsync(service, response).ConfigureAwait(false);
}

OnResponseReceived(response);

using (var responseStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
{
// We send ChunkSize bytes at a time to the caller, but we keep ChunkSize + 1 bytes
Expand All @@ -282,6 +284,7 @@ private async Task<IDownloadProgress> DownloadCoreAsync(string url, Stream strea

// Send one chunk to the caller's stream.
int bytesToReturn = Math.Min(ChunkSize, buffer.Count);
OnDataReceived(buffer.Data, bytesToReturn);
await stream.WriteAsync(buffer.Data, 0, bytesToReturn, cancellationToken).ConfigureAwait(false);
bytesReturned += bytesToReturn;

Expand All @@ -296,6 +299,7 @@ private async Task<IDownloadProgress> DownloadCoreAsync(string url, Stream strea
UpdateProgress(new DownloadProgress(DownloadStatus.Downloading, bytesReturned));
}
}
OnDownloadCompleted();

var finalProgress = new DownloadProgress(DownloadStatus.Completed, bytesReturned);
UpdateProgress(finalProgress);
Expand All @@ -316,5 +320,38 @@ private async Task<IDownloadProgress> DownloadCoreAsync(string url, Stream strea
return progress;
}
}

/// <summary>
/// Called when a successful HTTP response is received, allowing subclasses to examine headers.
/// </summary>
/// <remarks>
/// For unsuccessful responses, an appropriate exception is thrown immediately, without this method
/// being called.
/// </remarks>
/// <param name="response">HTTP response received.</param>
protected virtual void OnResponseReceived(HttpResponseMessage response)
{
// No-op
}

/// <summary>
/// Called when an HTTP response is received, allowing subclasses to examine data before it's
/// written to the client stream.
/// </summary>
/// <param name="data">Byte array containing the data downloaded.</param>
/// <param name="length">Length of data downloaded in this chunk, in bytes.</param>
protected virtual void OnDataReceived(byte[] data, int length)
{
// No-op
}

/// <summary>
/// Called when a download has completed, allowing subclasses to perform any final validation
/// or transformation.
/// </summary>
protected virtual void OnDownloadCompleted()
{
// No-op
}
}
}

0 comments on commit cbcefc0

Please sign in to comment.