Skip to content

Commit

Permalink
fix: Do not use BaseAddress property of HttpClient (#364)
Browse files Browse the repository at this point in the history
On .NET Framework, best practice is to re-use HttpClient, so using BaseAddress can cause the incorrect API URL to be used for different clients. Explicitly use the cached base address per-client when generating a HTTP request.

Co-authored-by: Armando Rodríguez <[email protected]>
  • Loading branch information
Antaris and armando-rodriguez-cko authored Feb 29, 2024
1 parent f186f91 commit 13d9602
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/CheckoutSdk/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ public class ApiClient : IApiClient
#endif

private readonly HttpClient _httpClient;
private readonly Uri _baseUri;
private readonly ISerializer _serializer = new JsonSerializer();

public ApiClient(IHttpClientFactory httpClientFactory, Uri baseUri)
{
CheckoutUtils.ValidateParams("httpClientFactory", httpClientFactory, "baseUri", baseUri);
var httpClient = httpClientFactory.CreateClient();
httpClient.BaseAddress = baseUri;
_baseUri = baseUri;
_httpClient = httpClient;
}

Expand Down Expand Up @@ -230,7 +231,9 @@ private async Task<HttpResponseMessage> Invoke(
string idempotencyKey)
{
CheckoutUtils.ValidateParams("httpMethod", httpMethod, "authorization", authorization);
var httpRequest = new HttpRequestMessage(httpMethod, path) { Content = httpContent };

var pathUri = new Uri(_baseUri, path);
var httpRequest = new HttpRequestMessage(httpMethod, pathUri) { Content = httpContent };
#if (NETSTANDARD2_0_OR_GREATER || NETCOREAPP3_1_OR_GREATER)
_log.LogInformation(@"{HttpMethod}: {Path}", httpMethod, path);
#endif
Expand Down

0 comments on commit 13d9602

Please sign in to comment.