Skip to content

Commit

Permalink
Fix HttpClient leakage by using a default static HttpClient instance
Browse files Browse the repository at this point in the history
  • Loading branch information
flagbug committed Jan 20, 2025
1 parent 7ae1faa commit abc7225
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/AppStoreServerApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public class AppStoreServerApiClient(
HttpClient? httpClient = null
)
{
private readonly HttpClient httpClient = httpClient ?? new HttpClient();
private static readonly Lazy<HttpClient> DefaultHttpClient = new(() => new HttpClient());
private readonly HttpClient httpClient = httpClient ?? DefaultHttpClient.Value;

/// <summary>
/// Get the statuses for all of a customer’s auto-renewable subscriptions in your app.
Expand Down Expand Up @@ -75,7 +76,10 @@ public Task<SubscriptionStatusResponse> GetAllSubscriptionStatuses(string transa
/// Get a customer’s in-app purchase transaction history for your app.
/// </summary>
/// <returns>A list of transactions associated with the provided Transaction Id</returns>
public Task<TransactionHistoryResponse?> GetTransactionHistory(string transactionId, string revisionToken = "")
public Task<TransactionHistoryResponse?> GetTransactionHistory(
string transactionId,
string revisionToken = ""
)
{
//Call to https://developer.apple.com/documentation/appstoreserverapi/get_transaction_history
Dictionary<string, string> queryParameters = new();
Expand All @@ -102,10 +106,21 @@ public Task SendConsumptionData(string transactionId, ConsumptionRequest consump
{
string path = $"v1/transactions/consumption/{transactionId}";

return this.MakeRequest<object?>(path, HttpMethod.Put, null, consumptionRequest, fetchResponse: false);
return this.MakeRequest<object?>(
path,
HttpMethod.Put,
null,
consumptionRequest,
fetchResponse: false
);
}

private static string CreateBearerToken(string keyId, string issuerId, string signingKey, string bundleId)
private static string CreateBearerToken(
string keyId,
string issuerId,
string signingKey,
string bundleId
)
{
var prvKey = ECDsa.Create();
prvKey.ImportFromPem(signingKey);
Expand Down Expand Up @@ -156,7 +171,10 @@ private static string CreateBearerToken(string keyId, string issuerId, string si
builder.Query = query.ToString();
}

var jsonOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
var jsonOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};

try
{
Expand Down Expand Up @@ -199,7 +217,9 @@ private static string CreateBearerToken(string keyId, string issuerId, string si

if (httpResponse.IsSuccessStatusCode)
{
return fetchResponse ? JsonSerializer.Deserialize<TReturn>(responseContent, jsonOptions) : null;
return fetchResponse
? JsonSerializer.Deserialize<TReturn>(responseContent, jsonOptions)
: null;
}

var error = JsonSerializer.Deserialize<ErrorResponse>(responseContent, jsonOptions);
Expand Down

0 comments on commit abc7225

Please sign in to comment.