From 3ac8360a09651851af5cab5f888bec2e2f2b98b7 Mon Sep 17 00:00:00 2001 From: Casper Sparre Date: Sat, 8 Mar 2025 15:10:17 +0100 Subject: [PATCH] HttpClient tweaks --- OF DL/Helpers/APIHelper.cs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/OF DL/Helpers/APIHelper.cs b/OF DL/Helpers/APIHelper.cs index ba45eae..ea73ba8 100644 --- a/OF DL/Helpers/APIHelper.cs +++ b/OF DL/Helpers/APIHelper.cs @@ -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; } - /// /// 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(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 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(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(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(body, m_JsonSerializerSettings); ctx.Status($"[red]Getting Streams\n[/] [red]Found {streams.list.Count}[/]"); ctx.Spinner(Spinner.Known.Dots);