|
23 | 23 |
|
24 | 24 | namespace Autodesk.Forge.Core
|
25 | 25 | {
|
| 26 | + /// <summary> |
| 27 | + /// Represents a handler for Forge API requests. |
| 28 | + /// </summary> |
26 | 29 | public class ForgeHandler : DelegatingHandler
|
27 | 30 | {
|
28 | 31 | private static SemaphoreSlim semaphore = new SemaphoreSlim(1, 1);
|
29 | 32 | private readonly Random rand = new Random();
|
30 | 33 | private readonly IAsyncPolicy<HttpResponseMessage> resiliencyPolicies;
|
31 | 34 |
|
| 35 | + /// <summary> |
| 36 | + /// Gets or sets the Forge configuration options. |
| 37 | + /// </summary> |
32 | 38 | protected readonly IOptions<ForgeConfiguration> configuration;
|
33 | 39 |
|
| 40 | + /// <summary> |
| 41 | + /// Gets or sets the token cache. |
| 42 | + /// </summary> |
34 | 43 | protected ITokenCache TokenCache { get; private set; }
|
35 | 44 |
|
36 | 45 | private bool IsDefaultClient(string user) => string.IsNullOrEmpty(user) || user == ForgeAgentHandler.defaultAgentName;
|
37 | 46 |
|
| 47 | + /// <summary> |
| 48 | + /// Initializes a new instance of the <see cref="ForgeHandler"/> class. |
| 49 | + /// </summary> |
| 50 | + /// <param name="configuration">The Forge configuration options.</param> |
38 | 51 | public ForgeHandler(IOptions<ForgeConfiguration> configuration)
|
39 | 52 | {
|
40 | 53 | this.configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
|
41 | 54 | this.TokenCache = new TokenCache();
|
42 | 55 | this.resiliencyPolicies = GetResiliencyPolicies(GetDefaultTimeout());
|
43 | 56 | }
|
| 57 | + /// <summary> |
| 58 | + /// Gets the default timeout value. |
| 59 | + /// </summary> |
| 60 | + /// <returns>The default timeout value.</returns> |
44 | 61 | protected virtual TimeSpan GetDefaultTimeout()
|
45 | 62 | {
|
46 |
| - // use timeout greater than the forge gateways (10s), we handle the GatewayTimeout response |
47 |
| - return TimeSpan.FromSeconds(15); |
| 63 | + // use timeout greater than the forge gateways (10s), we handle the GatewayTimeout response |
| 64 | + return TimeSpan.FromSeconds(15); |
48 | 65 | }
|
49 |
| - protected virtual (int baseDelayInMs, int multiplier ) GetRetryParameters() |
| 66 | + /// <summary> |
| 67 | + /// Gets the retry parameters for resiliency policies. |
| 68 | + /// </summary> |
| 69 | + /// <returns>A tuple containing the base delay in milliseconds and the multiplier.</returns> |
| 70 | + protected virtual (int baseDelayInMs, int multiplier) GetRetryParameters() |
50 | 71 | {
|
51 | 72 | return (500, 1000);
|
52 | 73 | }
|
| 74 | + /// <summary> |
| 75 | + /// Sends an HTTP request asynchronously. |
| 76 | + /// </summary> |
| 77 | + /// <param name="request">The HTTP request message.</param> |
| 78 | + /// <param name="cancellationToken">The cancellation token.</param> |
| 79 | + /// <returns>The task representing the asynchronous operation.</returns> |
| 80 | + /// <exception cref="ArgumentNullException">Thrown when the request URI is null.</exception> |
53 | 81 | protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
54 | 82 | {
|
55 | 83 | if (request.RequestUri == null)
|
@@ -80,18 +108,25 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
|
80 | 108 | }
|
81 | 109 | return await policies.ExecuteAsync(async (ct) => await base.SendAsync(request, ct), cancellationToken);
|
82 | 110 | }
|
83 |
| - |
| 111 | + /// <summary> |
| 112 | + /// Gets the token refresh policy. |
| 113 | + /// A policy that attempts to retry exactly once when a 401 error is received after obtaining a new token. |
| 114 | + /// </summary> |
| 115 | + /// <returns>The token refresh policy.</returns> |
84 | 116 | protected virtual IAsyncPolicy<HttpResponseMessage> GetTokenRefreshPolicy()
|
85 | 117 | {
|
86 |
| - // A policy that attempts to retry exactly once when 401 error is received after obtaining a new token |
87 | 118 | return Policy
|
88 | 119 | .HandleResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.Unauthorized)
|
89 | 120 | .RetryAsync(
|
90 | 121 | retryCount: 1,
|
91 | 122 | onRetryAsync: async (outcome, retryNumber, context) => await RefreshTokenAsync(outcome.Result.RequestMessage, true, CancellationToken.None)
|
92 | 123 | );
|
93 | 124 | }
|
94 |
| - |
| 125 | + /// <summary> |
| 126 | + /// Gets the resiliency policies for handling HTTP requests. |
| 127 | + /// </summary> |
| 128 | + /// <param name="timeoutValue">The timeout value for the policies.</param> |
| 129 | + /// <returns>The resiliency policies.</returns> |
95 | 130 | protected virtual IAsyncPolicy<HttpResponseMessage> GetResiliencyPolicies(TimeSpan timeoutValue)
|
96 | 131 | {
|
97 | 132 | // Retry when HttpRequestException is thrown (low level network error) or
|
@@ -149,6 +184,13 @@ protected virtual IAsyncPolicy<HttpResponseMessage> GetResiliencyPolicies(TimeSp
|
149 | 184 | return Policy.WrapAsync<HttpResponseMessage>(breaker, retry, timeout);
|
150 | 185 | }
|
151 | 186 |
|
| 187 | + /// <summary> |
| 188 | + /// Refreshes the token asynchronously. |
| 189 | + /// </summary> |
| 190 | + /// <param name="request">The HTTP request message.</param> |
| 191 | + /// <param name="ignoreCache">A flag indicating whether to ignore the cache and always refresh the token.</param> |
| 192 | + /// <param name="cancellationToken">The cancellation token.</param> |
| 193 | + /// <returns>The task representing the asynchronous operation.</returns> |
152 | 194 | protected virtual async Task RefreshTokenAsync(HttpRequestMessage request, bool ignoreCache, CancellationToken cancellationToken)
|
153 | 195 | {
|
154 | 196 | if (request.Options.TryGetValue(ForgeConfiguration.ScopeKey, out var scope))
|
@@ -176,6 +218,14 @@ protected virtual async Task RefreshTokenAsync(HttpRequestMessage request, bool
|
176 | 218 | }
|
177 | 219 | }
|
178 | 220 | }
|
| 221 | + |
| 222 | + /// <summary> |
| 223 | + /// Gets a 2-legged token asynchronously. |
| 224 | + /// </summary> |
| 225 | + /// <param name="user">The user.</param> |
| 226 | + /// <param name="scope">The scope.</param> |
| 227 | + /// <param name="cancellationToken">The cancellation token.</param> |
| 228 | + /// <returns>A tuple containing the token and its expiry time.</returns> |
179 | 229 | protected virtual async Task<(string, TimeSpan)> Get2LeggedTokenAsync(string user, string scope, CancellationToken cancellationToken)
|
180 | 230 | {
|
181 | 231 | using (var request = new HttpRequestMessage())
|
|
0 commit comments