Index: Src/GoogleApis/Apis/Http/ConfigurableMessageHandler.cs
===================================================================
--- a/Src/GoogleApis/Apis/Http/ConfigurableMessageHandler.cs
+++ b/Src/GoogleApis/Apis/Http/ConfigurableMessageHandler.cs
@@ -29,54 +29,54 @@
namespace Google.Apis.Http
{
///
- /// This message handler contains the main logic of our Http requests. It contains a list of
+ /// This message handler contains the main logic of our HTTP requests. It contains a list of
/// s for handling abnormal responses, a list of
/// s for handling exception in a request and a list of
/// s for intercepting a request before it has been sent to the server.
- /// It also contains important properties like num of retires, follow redirect, etc.
+ /// It also contains important properties like number of tires, follow redirect, etc.
///
public class ConfigurableMessageHandler : DelegatingHandler
{
- /// The class logger.
+ /// The class logger.
private static readonly ILogger Logger = ApplicationContext.Logger.ForType();
- /// Maximum allowed number of tries.
+ /// Maximum allowed number of tries.
[VisibleForTestOnly]
internal const int MaxAllowedNumTries = 20;
- /// The current API version of this client library.
+ /// The current API version of this client library.
private static readonly string ApiVersion = Google.Apis.Util.Utilities.GetLibraryVersion();
- /// The User-Agent suffix header which contains the .
+ /// The User-Agent suffix header which contains the .
private static readonly string UserAgentSuffix = "google-api-dotnet-client/" + ApiVersion + " (gzip)";
#region IHttpUnsuccessfulResponseHandler, IHttpExceptionHandler and IHttpExecuteInterceptor lists
- /// A list of .
+ /// A list of .
private readonly IList unsuccessfulResponseHandlers =
new List();
- /// A list of .
+ /// A list of .
private readonly IList exceptionHandlers =
new List();
- /// A list of .
+ /// A list of .
private readonly IList executeInterceptors =
new List();
- /// Gets a list of .
+ /// Gets a list of .
public IList UnsuccessfulResponseHandlers
{
get { return unsuccessfulResponseHandlers; }
}
- /// Gets a list of .
+ /// Gets a list of .
public IList ExceptionHandlers
{
get { return exceptionHandlers; }
}
- /// Gets a list of .
+ /// Gets a list of .
public IList ExecuteInterceptors
{
get { return executeInterceptors; }
@@ -84,13 +84,13 @@
#endregion
- /// Number of tries. Default is 3.
+ /// Number of tries. Default is 3.
private int numTries = 3;
- ///
+ ///
/// Gets or sets the number of tries that will be allowed to execute. Retries occur as a result of either
/// or which handles the
- /// abnormal Http response or exception, before being terminated.
+ /// abnormal HTTP response or exception, before being terminated.
/// Set 1 for not retrying requests. The default value is 3".
///
public int NumTries
@@ -106,19 +106,19 @@
}
}
- ///
+ ///
/// Gets or sets whether the handler should follow a redirect when a redirect response is received. Default
/// value is true.
///
public bool FollowRedirect { get; set; }
- /// Gets or sets whether logging is enabled. Default value is true.
+ /// Gets or sets whether logging is enabled. Default value is true.
public bool IsLoggingEnabled { get; set; }
- /// Gets or sets the application name which will be used on the User-Agent header.
+ /// Gets or sets the application name which will be used on the User-Agent header.
public string ApplicationName { get; set; }
- /// Constructs a new configurable message handler
+ /// Constructs a new configurable message handler
public ConfigurableMessageHandler(HttpMessageHandler httpMessageHandler)
: base(httpMessageHandler)
{
@@ -140,14 +140,14 @@
int triesRemaining = NumTries;
Exception lastException = null;
- // set User-Agent header
+ // Set User-Agent header.
var userAgent = (ApplicationName == null ? "" : ApplicationName + " ") + UserAgentSuffix;
// TODO: setting the User-Agent won't work on Silverlight. We may need to create a special callback here to
- // set it correctly. Also check what happen in WP?
+ // set it correctly.
request.Headers.Add("User-Agent", userAgent);
HttpResponseMessage response = null;
- do // while (triesRemaining > 0)
+ do // While (triesRemaining > 0)
{
cancellationToken.ThrowIfCancellationRequested();
@@ -158,17 +158,15 @@
}
lastException = null;
- // intercept the request
- // TODO(peleyal): rethink if each execute interceptor should be implemented as DelegateHandler, and
- // then remove this execute interceptor!
+ // Intercept the request.
foreach (var interceptor in executeInterceptors)
{
- interceptor.Intercept(request);
+ await interceptor.InterceptAsync(request, cancellationToken).ConfigureAwait(false);
}
try
{
- // send the request!
+ // Send the request!
response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
@@ -176,31 +174,31 @@
lastException = ex;
}
- // decrease the number of retries
+ // Decrease the number of retries.
triesRemaining--;
- // exception was thrown , try to handle it
+ // Exception was thrown, try to handle it.
if (response == null)
{
var exceptionHandled = false;
- // try to handle the exception with each handler
+ // Try to handle the exception with each handler.
foreach (var handler in exceptionHandlers)
{
- exceptionHandled |= handler.HandleException(new HandleExceptionArgs
+ exceptionHandled |= await handler.HandleExceptionAsync(new HandleExceptionArgs
{
Request = request,
Exception = lastException,
TotalTries = NumTries,
CurrentFailedTry = NumTries - triesRemaining,
CancellationToken = cancellationToken
- });
+ }).ConfigureAwait(false);
}
if (!exceptionHandled)
{
Logger.Error(lastException,
- "Exception was thrown while executing an Http request and it wasn't handled");
+ "Exception was thrown while executing a HTTP request and it wasn't handled");
throw lastException;
}
else if (loggable)
@@ -213,24 +211,24 @@
{
if (response.IsSuccessStatusCode)
{
- // no need to retry, the response was successful
+ // No need to retry, the response was successful.
triesRemaining = 0;
}
else
{
bool errorHandled = false;
- // try to handle the abnormal Http response with each handler
+ // Try to handle the abnormal HTTP response with each handler.
foreach (var handler in unsuccessfulResponseHandlers)
{
- errorHandled |= handler.HandleResponse(new HandleUnsuccessfulResponseArgs
+ errorHandled |= await handler.HandleResponseAsync(new HandleUnsuccessfulResponseArgs
{
Request = request,
Response = response,
TotalTries = NumTries,
CurrentFailedTry = NumTries - triesRemaining,
CancellationToken = cancellationToken
- });
+ }).ConfigureAwait(false);
}
if (!errorHandled)
@@ -252,7 +250,7 @@
response.StatusCode);
}
- // no need to retry, because no handler handled the abnormal response
+ // No need to retry, because no handler handled the abnormal response.
triesRemaining = 0;
}
}
@@ -263,12 +261,12 @@
}
}
}
- } while (triesRemaining > 0); // not a success status code but it was handled
+ } while (triesRemaining > 0); // Not a successful status code but it was handled.
- // if the response is null, we should throw the last exception
+ // If the response is null, we should throw the last exception.
if (response == null)
{
- Logger.Error(lastException, "Exception was thrown while executing an Http request");
+ Logger.Error(lastException, "Exception was thrown while executing a HTTP request");
throw lastException;
}
else if (!response.IsSuccessStatusCode)
@@ -302,7 +300,7 @@
{
request.Method = HttpMethod.Get;
}
- // clear Authorization and If-* headers
+ // Clear Authorization and If-* headers.
request.Headers.Remove("Authorization");
request.Headers.IfMatch.Clear();
request.Headers.IfNoneMatch.Clear();