Propogate CancellationToken to HTTP requests

This commit is contained in:
whimsical-c4lic0 2026-03-01 03:18:45 -06:00
parent 0654c9ab09
commit 523eb9b8f1

View File

@ -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<PurchasedDtos.PurchasedDto>(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<PurchasedDtos.PurchasedDto>(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<PostDtos.PostDto>(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<PostDtos.PostDto>(loopbody, s_mJsonSerializerSettings);
PostEntities.Post newposts = PostMapper.FromDto(newPostsDto);
@ -1044,7 +1051,8 @@ public class ApiService(IAuthService authService, IConfigService configService,
SinglePostCollection singlePostCollection = new();
Dictionary<string, string> 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<PostDtos.SinglePostDto>(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<StreamsDtos.StreamsDto>(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<StreamsDtos.StreamsDto>(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<MessageDtos.MessagesDto>(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<MessageDtos.MessagesDto>(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<MessageDtos.SingleMessageDto>(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<PurchasedDtos.PurchasedDto>(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<string, string> 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<PurchasedDtos.PurchasedDto>(body, s_mJsonSerializerSettings);
PurchasedEntities.Purchased purchased = PurchasedMapper.FromDto(purchasedDto);
@ -2789,12 +2809,12 @@ public class ApiService(IAuthService authService, IConfigService configService,
private async Task<string?> BuildHeaderAndExecuteRequests(Dictionary<string, string> 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();