From 523eb9b8f1f9c0cbf2b88f409f4a470ae3c3a3c3 Mon Sep 17 00:00:00 2001 From: whimsical-c4lic0 Date: Sun, 1 Mar 2026 03:18:45 -0600 Subject: [PATCH] Propogate CancellationToken to HTTP requests --- OF DL.Core/Services/ApiService.cs | 62 ++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/OF DL.Core/Services/ApiService.cs b/OF DL.Core/Services/ApiService.cs index 2e6b2a5..120c870 100644 --- a/OF DL.Core/Services/ApiService.cs +++ b/OF DL.Core/Services/ApiService.cs @@ -461,7 +461,8 @@ public class ApiService(IAuthService authService, IConfigService configService, break; } - string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, new HttpClient()); + string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, new HttpClient(), + cancellationToken); if (string.IsNullOrWhiteSpace(body)) { @@ -540,7 +541,8 @@ public class ApiService(IAuthService authService, IConfigService configService, { Log.Debug("Media Highlights - " + endpoint); - string? loopbody = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient()); + string? loopbody = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient(), + cancellationToken); if (string.IsNullOrWhiteSpace(loopbody)) { Log.Warning("Received empty body from API"); @@ -585,7 +587,8 @@ public class ApiService(IAuthService authService, IConfigService configService, highlightRequest.Headers.Add(keyValuePair.Key, keyValuePair.Value); } - using HttpResponseMessage highlightResponse = await highlightClient.SendAsync(highlightRequest); + using HttpResponseMessage highlightResponse = + await highlightClient.SendAsync(highlightRequest, cancellationToken); highlightResponse.EnsureSuccessStatusCode(); string highlightBody = await highlightResponse.Content.ReadAsStringAsync(); HighlightDtos.HighlightMediaDto? highlightMediaDto = @@ -680,7 +683,8 @@ public class ApiService(IAuthService authService, IConfigService configService, { "author", username } }; - string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient()); + string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient(), + cancellationToken); PurchasedDtos.PurchasedDto? paidPostsDto = DeserializeJson(body, s_mJsonSerializerSettings); PurchasedEntities.Purchased paidPosts = PurchasedMapper.FromDto(paidPostsDto); @@ -690,7 +694,8 @@ public class ApiService(IAuthService authService, IConfigService configService, getParams["offset"] = paidPosts.List.Count.ToString(); while (true) { - string? loopbody = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient()); + string? loopbody = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient(), + cancellationToken); PurchasedDtos.PurchasedDto? newPaidPostsDto = DeserializeJson(loopbody, s_mJsonSerializerSettings); PurchasedEntities.Purchased newPaidPosts = PurchasedMapper.FromDto(newPaidPostsDto); @@ -884,7 +889,8 @@ public class ApiService(IAuthService authService, IConfigService configService, ref getParams, downloadAsOf); - string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, new HttpClient()); + string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, new HttpClient(), + cancellationToken); PostDtos.PostDto? postsDto = DeserializeJson(body, s_mJsonSerializerSettings); PostEntities.Post posts = PostMapper.FromDto(postsDto); @@ -898,7 +904,8 @@ public class ApiService(IAuthService authService, IConfigService configService, while (true) { - string? loopbody = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient()); + string? loopbody = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient(), + cancellationToken); PostDtos.PostDto? newPostsDto = DeserializeJson(loopbody, s_mJsonSerializerSettings); PostEntities.Post newposts = PostMapper.FromDto(newPostsDto); @@ -1044,7 +1051,8 @@ public class ApiService(IAuthService authService, IConfigService configService, SinglePostCollection singlePostCollection = new(); Dictionary getParams = new() { { "skip_users", "all" } }; - string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, new HttpClient()); + string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, new HttpClient(), + cancellationToken); PostDtos.SinglePostDto? singlePostDto = DeserializeJson(body, s_mJsonSerializerSettings); PostEntities.SinglePost singlePost = PostMapper.FromDto(singlePostDto); @@ -1212,7 +1220,8 @@ public class ApiService(IAuthService authService, IConfigService configService, ref getParams, configService.CurrentConfig.CustomDate); - string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, new HttpClient()); + string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, new HttpClient(), + cancellationToken); StreamsDtos.StreamsDto? streamsDto = DeserializeJson(body, s_mJsonSerializerSettings); StreamEntities.Streams streams = StreamsMapper.FromDto(streamsDto); @@ -1226,7 +1235,8 @@ public class ApiService(IAuthService authService, IConfigService configService, while (true) { - string? loopbody = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient()); + string? loopbody = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient(), + cancellationToken); StreamsDtos.StreamsDto? newStreamsDto = DeserializeJson(loopbody, s_mJsonSerializerSettings); StreamEntities.Streams newstreams = StreamsMapper.FromDto(newStreamsDto); @@ -1366,7 +1376,8 @@ public class ApiService(IAuthService authService, IConfigService configService, ref getParams, configService.CurrentConfig.CustomDate); - string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient()); + string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient(), + cancellationToken); if (body == null) { throw new Exception("Failed to retrieve archived posts. Received null response."); @@ -1384,7 +1395,8 @@ public class ApiService(IAuthService authService, IConfigService configService, archived.TailMarker); while (true) { - string? loopbody = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient()); + string? loopbody = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient(), + cancellationToken); if (loopbody == null) { throw new Exception("Failed to retrieve archived posts. Received null response."); @@ -1506,7 +1518,8 @@ public class ApiService(IAuthService authService, IConfigService configService, }; int currentUserId = GetCurrentUserIdOrDefault(); - string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient()); + string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient(), + cancellationToken); MessageDtos.MessagesDto? messagesDto = DeserializeJson(body, s_mJsonSerializerSettings); MessageEntities.Messages messages = MessagesMapper.FromDto(messagesDto); @@ -1516,7 +1529,8 @@ public class ApiService(IAuthService authService, IConfigService configService, getParams["id"] = messages.List[^1].Id.ToString(); while (true) { - string? loopbody = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient()); + string? loopbody = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient(), + cancellationToken); MessageDtos.MessagesDto? newMessagesDto = DeserializeJson(loopbody, s_mJsonSerializerSettings); MessageEntities.Messages newMessages = MessagesMapper.FromDto(newMessagesDto); @@ -1693,7 +1707,8 @@ public class ApiService(IAuthService authService, IConfigService configService, new() { { "limit", Constants.ApiPageSize.ToString() }, { "order", "desc" } }; int currentUserId = GetCurrentUserIdOrDefault(); - string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient()); + string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient(), + cancellationToken); MessageDtos.SingleMessageDto? messageDto = DeserializeJson(body, s_mJsonSerializerSettings); MessageEntities.SingleMessage message = MessagesMapper.FromDto(messageDto); @@ -1848,7 +1863,8 @@ public class ApiService(IAuthService authService, IConfigService configService, }; int currentUserId = GetCurrentUserIdOrDefault(); - string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient()); + string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient(), + cancellationToken); PurchasedDtos.PurchasedDto? paidMessagesDto = DeserializeJson(body, s_mJsonSerializerSettings); PurchasedEntities.Purchased paidMessages = PurchasedMapper.FromDto(paidMessagesDto); @@ -1858,6 +1874,7 @@ public class ApiService(IAuthService authService, IConfigService configService, getParams["offset"] = paidMessages.List.Count.ToString(); while (true) { + cancellationToken.ThrowIfCancellationRequested(); string loopqueryParams = "?" + string.Join("&", getParams.Select(kvp => $"{kvp.Key}={kvp.Value}")); PurchasedEntities.Purchased newpaidMessages; Dictionary loopheaders = GetDynamicHeaders("/api2/v2" + endpoint, loopqueryParams); @@ -1871,7 +1888,8 @@ public class ApiService(IAuthService authService, IConfigService configService, looprequest.Headers.Add(keyValuePair.Key, keyValuePair.Value); } - using (HttpResponseMessage loopresponse = await loopclient.SendAsync(looprequest)) + using (HttpResponseMessage loopresponse = + await loopclient.SendAsync(looprequest, cancellationToken)) { loopresponse.EnsureSuccessStatusCode(); string loopbody = await loopresponse.Content.ReadAsStringAsync(); @@ -2070,7 +2088,8 @@ public class ApiService(IAuthService authService, IConfigService configService, }; cancellationToken.ThrowIfCancellationRequested(); - string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient()); + string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient(), + cancellationToken); if (body == null) { throw new Exception("Failed to get purchased tab users. null body returned."); @@ -2245,7 +2264,8 @@ public class ApiService(IAuthService authService, IConfigService configService, }; cancellationToken.ThrowIfCancellationRequested(); - string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient()); + string? body = await BuildHeaderAndExecuteRequests(getParams, endpoint, GetHttpClient(), + cancellationToken); PurchasedDtos.PurchasedDto? purchasedDto = DeserializeJson(body, s_mJsonSerializerSettings); PurchasedEntities.Purchased purchased = PurchasedMapper.FromDto(purchasedDto); @@ -2789,12 +2809,12 @@ public class ApiService(IAuthService authService, IConfigService configService, private async Task BuildHeaderAndExecuteRequests(Dictionary getParams, string endpoint, - HttpClient client) + HttpClient client, CancellationToken cancellationToken = default) { Log.Debug("Calling BuildHeaderAndExecuteRequests"); HttpRequestMessage request = await BuildHttpRequestMessage(getParams, endpoint); - using HttpResponseMessage response = await client.SendAsync(request); + using HttpResponseMessage response = await client.SendAsync(request, cancellationToken); response.EnsureSuccessStatusCode(); string body = await response.Content.ReadAsStringAsync();