-
Notifications
You must be signed in to change notification settings - Fork 34
Allow Using HttpClient BaseAddres for Authority in DiscoveryCache #257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ public class DiscoveryCache : IDiscoveryCache | |
|
||
private readonly DiscoveryPolicy _policy; | ||
private readonly Func<HttpMessageInvoker> _getHttpClient; | ||
private readonly string _authority; | ||
private readonly string? _authority; | ||
|
||
/// <summary> | ||
/// Initialize instance of DiscoveryCache with passed authority. | ||
|
@@ -42,6 +42,18 @@ public DiscoveryCache(string authority, Func<HttpMessageInvoker> httpClientFunc, | |
_getHttpClient = httpClientFunc ?? throw new ArgumentNullException(nameof(httpClientFunc)); | ||
} | ||
|
||
/// <summary> | ||
/// Initialize instance of DiscoveryCache without authority - the HttpClient used must have a BaseAddress configured. | ||
/// </summary> | ||
/// <param name="httpClientFunc">The HTTP client function which must have a BaseAddress configured.</param> | ||
/// <param name="policy">The policy.</param> | ||
public DiscoveryCache(Func<HttpMessageInvoker> httpClientFunc, DiscoveryPolicy? policy = null) | ||
{ | ||
_getHttpClient = httpClientFunc ?? throw new ArgumentNullException(nameof(httpClientFunc)); | ||
_policy = policy ?? new DiscoveryPolicy(); | ||
_authority = null; | ||
} | ||
bhazen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// <summary> | ||
/// Frequency to refresh discovery document. Defaults to 24 hours. | ||
/// </summary> | ||
|
@@ -68,9 +80,17 @@ public Task<DiscoveryDocumentResponse> GetAsync() | |
|
||
private async Task<DiscoveryDocumentResponse> GetResponseAsync() | ||
{ | ||
var result = await _getHttpClient().GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest | ||
var client = _getHttpClient(); | ||
var httpClient = client as HttpClient; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was not thrilled with needing to do this, but the alternative I saw was changing the type of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe to be defensive we should detect if the cast fails and throw an exception to say that you have to either use an http client or set the authority? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that's probably better than relying on exception from |
||
var address = _authority ?? httpClient?.BaseAddress?.AbsoluteUri; | ||
if (address.IsMissing()) | ||
{ | ||
throw new InvalidOperationException("DiscoveryCache cannot determine the authority. Either pass the authority in the constructor or pass httpClientFunc which returns an instance of HttpClient with a BaseAddress."); | ||
} | ||
|
||
bhazen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var result = await client.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest | ||
{ | ||
Address = _authority, | ||
Address = address, | ||
bhazen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Policy = _policy | ||
}).ConfigureAwait(); | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.