HttpClient tweaks

This commit is contained in:
Casper Sparre 2025-03-08 15:10:17 +01:00
parent 459f9745d8
commit 90ae6a6fed

View File

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