-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from mdsol/develop
Release of v2.2.0
- Loading branch information
Showing
17 changed files
with
278 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/Medidata.MAuth.Core/Exceptions/RetriedRequestException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
|
||
namespace Medidata.MAuth.Core | ||
{ | ||
/// <summary> | ||
/// The exception that is thrown when a number of request attempts to the MAuth server fails. | ||
/// </summary> | ||
public class RetriedRequestException: Exception | ||
{ | ||
/// <summary> | ||
/// Determines the responses for each request attempts made. | ||
/// </summary> | ||
public ICollection<HttpResponseMessage> Responses { get; } = new List<HttpResponseMessage>(); | ||
|
||
/// <summary> | ||
/// Determines the request message of the first request attempt to the MAuth server. | ||
/// </summary> | ||
public HttpRequestMessage Request { get; set; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="RetriedRequestException"/> class with the specified message. | ||
/// </summary> | ||
/// <param name="message">A message that describes the request failure.</param> | ||
public RetriedRequestException(string message): base(message) { } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="RetriedRequestException"/> class with the specified message | ||
/// and inner exception. | ||
/// </summary> | ||
/// <param name="message">A message that describes the request failure.</param> | ||
/// <param name="innerException">An exception that is the cause of the current exception.</param> | ||
public RetriedRequestException(string message, Exception innerException): base(message, innerException) { } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
#if !NETSTANDARD1_4 | ||
using System.Net.Cache; | ||
#endif | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
|
||
namespace Medidata.MAuth.Core | ||
{ | ||
internal class MAuthRequestRetrier | ||
{ | ||
private readonly HttpClient client; | ||
private RetriedRequestException exception; | ||
|
||
public MAuthRequestRetrier(MAuthOptionsBase options) | ||
{ | ||
var signingHandler = new MAuthSigningHandler(options: new MAuthSigningOptions() | ||
{ | ||
ApplicationUuid = options.ApplicationUuid, | ||
PrivateKey = options.PrivateKey | ||
}, | ||
innerHandler: options.MAuthServerHandler ?? | ||
#if NETSTANDARD1_4 | ||
new HttpClientHandler() | ||
#else | ||
new WebRequestHandler() | ||
{ | ||
CachePolicy = new RequestCachePolicy(RequestCacheLevel.Default) | ||
} | ||
#endif | ||
); | ||
|
||
client = new HttpClient(signingHandler); | ||
client.Timeout = TimeSpan.FromSeconds(options.AuthenticateRequestTimeoutSeconds); | ||
} | ||
|
||
public async Task<HttpResponseMessage> GetSuccessfulResponse(Guid applicationUuid, | ||
Func<Guid, HttpRequestMessage> requestFactory, int remainingAttempts) | ||
{ | ||
var request = requestFactory?.Invoke(applicationUuid); | ||
|
||
if (request == null) | ||
throw new ArgumentNullException( | ||
nameof(requestFactory), | ||
"No request function provided or the provided request function resulted null request." | ||
); | ||
|
||
exception = exception ?? new RetriedRequestException( | ||
$"Could not get a successful response from the MAuth Service after {remainingAttempts} attempts. " + | ||
"Please see the responses for each attempt in the exception's Responses field.") | ||
{ | ||
Request = request | ||
}; | ||
|
||
if (remainingAttempts == 0) | ||
throw exception; | ||
|
||
var result = await client.SendAsync(request).ConfigureAwait(continueOnCapturedContext: false); | ||
|
||
exception.Responses.Add(result); | ||
|
||
return result.IsSuccessStatusCode ? | ||
result : await GetSuccessfulResponse(applicationUuid, requestFactory, remainingAttempts - 1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
namespace Medidata.MAuth.Core | ||
{ | ||
/// <summary> | ||
/// Determines the retrying policy with the MAuth server communication. | ||
/// </summary> | ||
public enum MAuthServiceRetryPolicy | ||
{ | ||
/// <summary> | ||
/// No attempt to retry the service request. The authentication will fail upon the first attempt if it is | ||
/// not successful. | ||
/// </summary> | ||
NoRetry = 0, | ||
/// <summary> | ||
/// 1 more attempt to send a request to the MAuth service if the first attempt fails. | ||
/// </summary> | ||
RetryOnce = 1, | ||
/// <summary> | ||
/// 2 more attempts to send a request to the MAuth service if the first attempt fails. | ||
/// </summary> | ||
RetryTwice = 2, | ||
/// <summary> | ||
/// 9 more attempts to send a request to the MAuth service if the first attempt fails. This setting is not | ||
/// recommended in production use as it can put more load the the MAuth service. | ||
/// </summary> | ||
Agressive = 9 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.