// ------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
// ------------------------------------------------------------------------------
namespace Microsoft.Graph
{
using System;
using System.Net.Http;
///
/// Extension methods for
///
public static class BaseRequestExtensions
{
///
/// Sets the default authentication provider to the default Authentication Middleware Handler for this request.
/// This only works with the default authentication handler.
/// If you use a custom authentication handler, you have to handle it's retrieval in your implementation.
///
///
/// The for the request.
///
internal static T WithDefaultAuthProvider(this T baseRequest) where T : IBaseRequest
{
string authOptionKey = typeof(AuthenticationHandlerOption).ToString();
if (baseRequest.MiddlewareOptions.ContainsKey(authOptionKey))
{
(baseRequest.MiddlewareOptions[authOptionKey] as AuthenticationHandlerOption).AuthenticationProvider = baseRequest.Client.AuthenticationProvider;
}
else
{
baseRequest.MiddlewareOptions.Add(authOptionKey, new AuthenticationHandlerOption { AuthenticationProvider = baseRequest.Client.AuthenticationProvider });
}
return baseRequest;
}
///
/// Sets the PerRequestAuthProvider delegate handler to the default Authentication Middleware Handler to authenticate a single request.
/// The PerRequestAuthProvider delegate handler must be set to the GraphServiceClient instance before using this extension method otherwise, it defaults to the default authentication provider.
/// This only works with the default authentication handler.
/// If you use a custom authentication handler, you have to handle it's retrieval in your implementation.
///
///
/// The for the request.
///
public static T WithPerRequestAuthProvider(this T baseRequest) where T : IBaseRequest
{
if (baseRequest.Client.PerRequestAuthProvider != null)
{
string authOptionKey = typeof(AuthenticationHandlerOption).ToString();
if (baseRequest.MiddlewareOptions.ContainsKey(authOptionKey))
{
(baseRequest.MiddlewareOptions[authOptionKey] as AuthenticationHandlerOption).AuthenticationProvider = baseRequest.Client.PerRequestAuthProvider();
}
else
{
baseRequest.MiddlewareOptions.Add(authOptionKey, new AuthenticationHandlerOption { AuthenticationProvider = baseRequest.Client.PerRequestAuthProvider() });
}
}
return baseRequest;
}
///
/// Sets a ShouldRetry delegate to the default Retry Middleware Handler for this request.
/// This only works with the default Retry Middleware Handler.
/// If you use a custom Retry Middleware Handler, you have to handle it's retrieval in your implementation.
///
///
/// The for the request.
/// A for the request.
///
public static T WithShouldRetry(this T baseRequest, Func shouldRetry) where T : IBaseRequest
{
string retryOptionKey = typeof(RetryHandlerOption).ToString();
if (baseRequest.MiddlewareOptions.ContainsKey(retryOptionKey))
{
(baseRequest.MiddlewareOptions[retryOptionKey] as RetryHandlerOption).ShouldRetry = shouldRetry;
}
else
{
baseRequest.MiddlewareOptions.Add(retryOptionKey, new RetryHandlerOption { ShouldRetry = shouldRetry });
}
return baseRequest;
}
///
/// Sets the maximum number of retries to the default Retry Middleware Handler for this request.
/// This only works with the default Retry Middleware Handler.
/// If you use a custom Retry Middleware Handler, you have to handle it's retrieval in your implementation.
///
///
/// The for the request.
/// The maxRetry for the request.
///
public static T WithMaxRetry(this T baseRequest, int maxRetry) where T : IBaseRequest
{
string retryOptionKey = typeof(RetryHandlerOption).ToString();
if (baseRequest.MiddlewareOptions.ContainsKey(retryOptionKey))
{
(baseRequest.MiddlewareOptions[retryOptionKey] as RetryHandlerOption).MaxRetry = maxRetry;
}
else
{
baseRequest.MiddlewareOptions.Add(retryOptionKey, new RetryHandlerOption { MaxRetry = maxRetry });
}
return baseRequest;
}
///
/// Sets the maximum time for request retries to the default Retry Middleware Handler for this request.
/// This only works with the default Retry Middleware Handler.
/// If you use a custom Retry Middleware Handler, you have to handle it's retrieval in your implementation.
///
///
/// The for the request.
/// The retriestimelimit for the request in seconds.
///
public static T WithMaxRetry(this T baseRequest, TimeSpan retriesTimeLimit) where T : IBaseRequest
{
string retryOptionKey = typeof(RetryHandlerOption).ToString();
if (baseRequest.MiddlewareOptions.ContainsKey(retryOptionKey))
{
(baseRequest.MiddlewareOptions[retryOptionKey] as RetryHandlerOption).RetriesTimeLimit = retriesTimeLimit;
}
else
{
baseRequest.MiddlewareOptions.Add(retryOptionKey, new RetryHandlerOption { RetriesTimeLimit = retriesTimeLimit });
}
return baseRequest;
}
///
/// Sets the maximum number of redirects to the default Redirect Middleware Handler for this request.
/// This only works with the default Redirect Middleware Handler.
/// If you use a custom Redirect Middleware Handler, you have to handle it's retrieval in your implementation.
///
///
/// The for the request.
/// Maximum number of redirects allowed for the request
///
public static T WithMaxRedirects(this T baseRequest, int maxRedirects) where T : IBaseRequest
{
string redirectOptionKey = typeof(RedirectHandlerOption).ToString();
if (baseRequest.MiddlewareOptions.ContainsKey(redirectOptionKey))
{
(baseRequest.MiddlewareOptions[redirectOptionKey] as RedirectHandlerOption).MaxRedirect = maxRedirects;
}
else
{
baseRequest.MiddlewareOptions.Add(redirectOptionKey, new RedirectHandlerOption { MaxRedirect = maxRedirects });
}
return baseRequest;
}
///
/// Replaces the default response handler with a custom response handler for this request.
///
///
/// The for the request.
/// The for the request.
///
/// If responseHandler is null.
public static T WithResponseHandler(this T baseRequest, IResponseHandler responseHandler) where T : BaseRequest
{
baseRequest.ResponseHandler = responseHandler ?? throw new ArgumentNullException(nameof(responseHandler));
return baseRequest;
}
}
}