-
Notifications
You must be signed in to change notification settings - Fork 10
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
Allow Customization of Base URI #10
Comments
The problem is that Therefore one viable alternative would be to use the following public class BaseAddressStore
{
public Uri? BaseAddress { get; set; } // Consider Optional
public BaseAddressStore(Uri? baseAddress = null)
{
this.BaseAddress = baseAddress;
}
}
public class BaseAddressOverrideHandler : System.Net.Http.DelegatingHandler
{
private readonly Uri _originalBaseAddress;
private readonly BaseAddressStore _baseAddressStore;
public BaseAddressOverrideHandler(Uri orignalBaseAddress, BaseAddressStore baseAddressStore)
{
_originalBaseAddress = originalBaseAddress;
_baseAddressStore = baseAddressStore;
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var baseAddress = _baseAddressStore.BaseAddress;
if (baseAddress != null && request.RequestUri != null)
{
if (_originalBaseAddress.IsBaseOf(request.RequestUri))
{
var relativeUri = _originalBaseAddress.MakeRelativeUri(request.RequestUri);
request.RequestUri = new Uri(baseAddress, relativeUri);
}
}
return base.SendAsync(request, cancellationToken);
}
} |
I'm not sure if that is a problem, since a new HttpClient is requested from the factory on each request. |
HttpClientFactory doesn't allow for modifying the base address configuration, likely due to some issues with how it handles the lifetime (and possible reuse) of existing clients. There is no tracking mechanism for which clients which use which base address since it's assumed that this configuration will not change. I think @MazeXP's suggestion is likely the most practical at this point, though I'm doing some digging to see if there are other options (besides implementing our own HttpClientFactory). |
Description
Some REST APIs will specify a specific API server instance after authenticating and will expect all API requests to use that server.
For instance, authentication may take place at
auth.example.com
but the upon successful authentication, you are directed to send API queries toapi-na1.example.com
.The ability to customize the base address should be added.
Why This is Needed
Without the ability to customize the base address, it places a requirement on the user to fully qualify all endpoint calls, which becomes difficult when the directed API base address changes between calls.
Alternatives Considered
One option currently is for the user to do something like this, where
_cache
is anIMemoryCache
andIAuthenticationResponse.ResourceServerBaseUri
is the API base address I was directed to use. However, for obvious reasons, this is less than ideal.The text was updated successfully, but these errors were encountered: