|
| 1 | +// ------------------------------------------------------------------------------ |
| 2 | +// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. |
| 3 | +// ------------------------------------------------------------------------------ |
| 4 | + |
| 5 | +namespace Microsoft.Graph |
| 6 | +{ |
| 7 | + using System; |
| 8 | + using System.Net.Http; |
| 9 | + |
| 10 | + /// <summary> |
| 11 | + /// Extension methods for <see cref="BaseRequest"/> |
| 12 | + /// </summary> |
| 13 | + public static class BaseRequestExtensions |
| 14 | + { |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Sets the default authentication provider to the default Authentication Middleware Handler for this request. |
| 18 | + /// This only works with the default authentication handler. |
| 19 | + /// If you use a custom authentication handler, you have to handle it's retreival in your implementation. |
| 20 | + /// </summary> |
| 21 | + /// <typeparam name="T"></typeparam> |
| 22 | + /// <param name="baseRequest">The <see cref="BaseRequest"/> for the request.</param> |
| 23 | + /// <returns></returns> |
| 24 | + internal static T WithDefaultAuthProvider<T>(this T baseRequest) where T : IBaseRequest |
| 25 | + { |
| 26 | + string authOptionKey = typeof(AuthenticationHandlerOption).ToString(); |
| 27 | + if (baseRequest.MiddlewareOptions.ContainsKey(authOptionKey)) |
| 28 | + { |
| 29 | + (baseRequest.MiddlewareOptions[authOptionKey] as AuthenticationHandlerOption).AuthenticationProvider = baseRequest.Client.AuthenticationProvider; |
| 30 | + } |
| 31 | + else |
| 32 | + { |
| 33 | + baseRequest.MiddlewareOptions.Add(authOptionKey, new AuthenticationHandlerOption { AuthenticationProvider = baseRequest.Client.AuthenticationProvider }); |
| 34 | + } |
| 35 | + return baseRequest; |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Sets the PerRequestAuthProvider delegate handler to the default Authentication Middleware Handler to authenticate a single request. |
| 40 | + /// The PerRequestAuthProvider delegate handler must be set to the GraphServiceClient instance before using this extension method otherwise, it defaults to the default authentication provider. |
| 41 | + /// This only works with the default authentication handler. |
| 42 | + /// If you use a custom authentication handler, you have to handle it's retreival in your implementation. |
| 43 | + /// </summary> |
| 44 | + /// <typeparam name="T"></typeparam> |
| 45 | + /// <param name="baseRequest">The <see cref="BaseRequest"/> for the request.</param> |
| 46 | + /// <returns></returns> |
| 47 | + public static T WithPerRequestAuthProvider<T>(this T baseRequest) where T : IBaseRequest |
| 48 | + { |
| 49 | + if (baseRequest.Client.PerRequestAuthProvider != null) |
| 50 | + { |
| 51 | + string authOptionKey = typeof(AuthenticationHandlerOption).ToString(); |
| 52 | + if (baseRequest.MiddlewareOptions.ContainsKey(authOptionKey)) |
| 53 | + { |
| 54 | + (baseRequest.MiddlewareOptions[authOptionKey] as AuthenticationHandlerOption).AuthenticationProvider = baseRequest.Client.PerRequestAuthProvider(); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + baseRequest.MiddlewareOptions.Add(authOptionKey, new AuthenticationHandlerOption { AuthenticationProvider = baseRequest.Client.PerRequestAuthProvider() }); |
| 59 | + } |
| 60 | + } |
| 61 | + return baseRequest; |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Sets a ShouldRetry <see cref="Func{HttpResponseMessage, Boolean}"/> delegate to the default Retry Middleware Handler for this request. |
| 66 | + /// This only works with the default Retry Middleware Handler. |
| 67 | + /// If you use a custom Retry Middleware Handler, you have to handle it's retreival in your implementation. |
| 68 | + /// </summary> |
| 69 | + /// <typeparam name="T"></typeparam> |
| 70 | + /// <param name="baseRequest">The <see cref="BaseRequest"/> for the request.</param> |
| 71 | + /// <param name="shouldRetry">A <see cref="Func{HttpResponseMessage, Boolean}"/> for the request.</param> |
| 72 | + /// <returns></returns> |
| 73 | + public static T WithShouldRetry<T>(this T baseRequest, Func<HttpResponseMessage, bool> shouldRetry) where T : IBaseRequest |
| 74 | + { |
| 75 | + string retryOptionKey = typeof(RetryHandlerOption).ToString(); |
| 76 | + if (baseRequest.MiddlewareOptions.ContainsKey(retryOptionKey)) |
| 77 | + { |
| 78 | + (baseRequest.MiddlewareOptions[retryOptionKey] as RetryHandlerOption).ShouldRetry = shouldRetry; |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + baseRequest.MiddlewareOptions.Add(retryOptionKey, new RetryHandlerOption { ShouldRetry = shouldRetry }); |
| 83 | + } |
| 84 | + return baseRequest; |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Sets the maximum number of retries to the default Retry Middleware Handler for this request. |
| 89 | + /// This only works with the default Retry Middleware Handler. |
| 90 | + /// If you use a custom Retry Middleware Handler, you have to handle it's retreival in your implementation. |
| 91 | + /// </summary> |
| 92 | + /// <typeparam name="T"></typeparam> |
| 93 | + /// <param name="baseRequest">The <see cref="BaseRequest"/> for the request.</param> |
| 94 | + /// <param name="maxRetry">The maxRetry for the request.</param> |
| 95 | + /// <returns></returns> |
| 96 | + public static T WithMaxRetry<T>(this T baseRequest, int maxRetry) where T : IBaseRequest |
| 97 | + { |
| 98 | + string retryOptionKey = typeof(RetryHandlerOption).ToString(); |
| 99 | + if (baseRequest.MiddlewareOptions.ContainsKey(retryOptionKey)) |
| 100 | + { |
| 101 | + (baseRequest.MiddlewareOptions[retryOptionKey] as RetryHandlerOption).MaxRetry = maxRetry; |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + baseRequest.MiddlewareOptions.Add(retryOptionKey, new RetryHandlerOption { MaxRetry = maxRetry }); |
| 106 | + } |
| 107 | + return baseRequest; |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Sets the maximum number of redirects to the default Redirect Middleware Handler for this request. |
| 112 | + /// This only works with the default Redirect Middleware Handler. |
| 113 | + /// If you use a custom Redirect Middleware Handler, you have to handle it's retreival in your implementation. |
| 114 | + /// </summary> |
| 115 | + /// <typeparam name="T"></typeparam> |
| 116 | + /// <param name="baseRequest">The <see cref="BaseRequest"/> for the request.</param> |
| 117 | + /// <param name="maxRedirects">Maximum number of redirects allowed for the request</param> |
| 118 | + /// <returns></returns> |
| 119 | + public static T WithMaxRedirects<T>(this T baseRequest, int maxRedirects) where T : IBaseRequest |
| 120 | + { |
| 121 | + string redirectOptionKey = typeof(RedirectHandlerOption).ToString(); |
| 122 | + if (baseRequest.MiddlewareOptions.ContainsKey(redirectOptionKey)) |
| 123 | + { |
| 124 | + (baseRequest.MiddlewareOptions[redirectOptionKey] as RedirectHandlerOption).MaxRedirects = maxRedirects; |
| 125 | + } |
| 126 | + else |
| 127 | + { |
| 128 | + baseRequest.MiddlewareOptions.Add(redirectOptionKey, new RedirectHandlerOption { MaxRedirects = maxRedirects }); |
| 129 | + } |
| 130 | + return baseRequest; |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments