HttpClient tweaks

This commit is contained in:
Casper Sparre 2025-03-08 15:10:17 +01:00
parent fed6fc77a8
commit 3ac8360a09

View File

@ -29,6 +29,8 @@ public class APIHelper : IAPIHelper
private readonly IDBHelper m_DBHelper;
private readonly IDownloadConfig downloadConfig;
private readonly Auth auth;
private HttpClient httpClient = new();
private static DateTime? cachedDynamicRulesExpiration;
private static DynamicRules? cachedDynamicRules;
@ -164,18 +166,16 @@ public class APIHelper : IAPIHelper
return input.All(char.IsDigit);
}
private static HttpClient GetHttpClient(IDownloadConfig? config = null)
private HttpClient GetHttpClient(IDownloadConfig? config = null)
{
var client = new HttpClient();
httpClient ??= new HttpClient();
if (config?.Timeout != null && config.Timeout > 0)
{
client.Timeout = TimeSpan.FromSeconds(config.Timeout.Value);
httpClient.Timeout = TimeSpan.FromSeconds(config.Timeout.Value);
}
return client;
return httpClient;
}
/// <summary>
/// this one is used during initialization only
/// if the config option is not available then no modificatiotns will be done on the getParams
@ -302,7 +302,7 @@ public class APIHelper : IAPIHelper
Log.Debug("Calling GetAllSubscrptions");
string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, new HttpClient());
string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, httpClient);
subscriptions = JsonConvert.DeserializeObject<Subscriptions>(body);
if (subscriptions != null && subscriptions.hasMore)
@ -312,7 +312,7 @@ public class APIHelper : IAPIHelper
while (true)
{
Subscriptions newSubscriptions = new();
string? loopbody = await BuildHeaderAndExecuteRequests(getParams, endpoint, new HttpClient());
string? loopbody = await BuildHeaderAndExecuteRequests(getParams, endpoint, httpClient);
if (!string.IsNullOrEmpty(loopbody) && (!loopbody.Contains("[]") || loopbody.Trim() != "[]"))
{
@ -405,7 +405,7 @@ public class APIHelper : IAPIHelper
Dictionary<string, int> lists = new();
while (true)
{
string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, new HttpClient());
string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient(config));
if (body == null)
{
@ -470,7 +470,7 @@ public class APIHelper : IAPIHelper
while (true)
{
var body = await BuildHeaderAndExecuteRequests(getParams, endpoint, new HttpClient());
var body = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient(config));
if (body == null)
{
break;
@ -553,7 +553,7 @@ public class APIHelper : IAPIHelper
break;
}
var body = await BuildHeaderAndExecuteRequests(getParams, endpoint, new HttpClient());
var body = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient(config));
if (mediatype == MediaType.Stories)
@ -932,7 +932,7 @@ public class APIHelper : IAPIHelper
ref getParams,
downloadAsOf);
var body = await BuildHeaderAndExecuteRequests(getParams, endpoint, new HttpClient());
var body = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient(config));
posts = JsonConvert.DeserializeObject<Post>(body, m_JsonSerializerSettings);
ctx.Status($"[red]Getting Posts (this may take a long time, depending on the number of Posts the creator has)\n[/] [red]Found {posts.list.Count}[/]");
ctx.Spinner(Spinner.Known.Dots);
@ -1090,7 +1090,7 @@ public class APIHelper : IAPIHelper
{ "skip_users", "all" }
};
var body = await BuildHeaderAndExecuteRequests(getParams, endpoint, new HttpClient());
var body = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient(config));
singlePost = JsonConvert.DeserializeObject<SinglePost>(body, m_JsonSerializerSettings);
if (singlePost != null)
@ -1251,7 +1251,7 @@ public class APIHelper : IAPIHelper
ref getParams,
config.CustomDate);
var body = await BuildHeaderAndExecuteRequests(getParams, endpoint, new HttpClient());
var body = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient(config));
streams = JsonConvert.DeserializeObject<Streams>(body, m_JsonSerializerSettings);
ctx.Status($"[red]Getting Streams\n[/] [red]Found {streams.list.Count}[/]");
ctx.Spinner(Spinner.Known.Dots);