From 8357eb350ea0259a008a61076e5a593d741aa59b Mon Sep 17 00:00:00 2001 From: Stanley Dimant Date: Wed, 18 Jan 2023 15:06:20 +0100 Subject: [PATCH] fix SendRequestAsync --- MareAPI | 2 +- .../WebAPI/ApIController.Functions.Files.cs | 34 ++++++++++++------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/MareAPI b/MareAPI index 9fea4c3..2015496 160000 --- a/MareAPI +++ b/MareAPI @@ -1 +1 @@ -Subproject commit 9fea4c30da56d842a10a209a9945fd7f3cb98f4b +Subproject commit 2015496ec0bbda566a0ac7e88ce56d7714fd8c49 diff --git a/MareSynchronos/WebAPI/ApIController.Functions.Files.cs b/MareSynchronos/WebAPI/ApIController.Functions.Files.cs index ac3c776..4006552 100644 --- a/MareSynchronos/WebAPI/ApIController.Functions.Files.cs +++ b/MareSynchronos/WebAPI/ApIController.Functions.Files.cs @@ -50,7 +50,7 @@ public partial class ApiController private async Task GetQueueRequest(DownloadFileTransfer downloadFileTransfer, CancellationToken ct) { - var response = await SendRequestAsync(HttpMethod.Get, MareFiles.RequestRequestFileFullPath(downloadFileTransfer.DownloadUri, downloadFileTransfer.Hash), ct: ct).ConfigureAwait(false); + var response = await SendRequestAsync(HttpMethod.Get, MareFiles.RequestRequestFileFullPath(downloadFileTransfer.DownloadUri, downloadFileTransfer.Hash), ct).ConfigureAwait(false); var responseString = await response.Content.ReadAsStringAsync(ct).ConfigureAwait(false); var requestId = Guid.Parse(responseString.Trim('"')); if (!_downloadReady.ContainsKey(requestId)) @@ -79,7 +79,7 @@ public partial class ApiController { if (downloadCt.IsCancellationRequested) throw; - var req = await SendRequestAsync(HttpMethod.Get, MareFiles.RequestCheckQueueFullPath(downloadFileTransfer.DownloadUri, requestId, downloadFileTransfer.Hash), downloadCt).ConfigureAwait(false); + var req = await SendRequestAsync(HttpMethod.Get, MareFiles.RequestCheckQueueFullPath(downloadFileTransfer.DownloadUri, requestId, downloadFileTransfer.Hash), downloadCt).ConfigureAwait(false); try { req.EnsureSuccessStatusCode(); @@ -105,7 +105,7 @@ public partial class ApiController { try { - await SendRequestAsync(HttpMethod.Get, MareFiles.RequestCancelFullPath(downloadFileTransfer.DownloadUri, requestId), downloadCt).ConfigureAwait(false); + await SendRequestAsync(HttpMethod.Get, MareFiles.RequestCancelFullPath(downloadFileTransfer.DownloadUri, requestId), downloadCt).ConfigureAwait(false); alreadyCancelled = true; } catch { } @@ -118,7 +118,7 @@ public partial class ApiController { try { - await SendRequestAsync(HttpMethod.Get, MareFiles.RequestCancelFullPath(downloadFileTransfer.DownloadUri, requestId), downloadCt).ConfigureAwait(false); + await SendRequestAsync(HttpMethod.Get, MareFiles.RequestCancelFullPath(downloadFileTransfer.DownloadUri, requestId), downloadCt).ConfigureAwait(false); } catch { } } @@ -140,7 +140,7 @@ public partial class ApiController Logger.Debug($"Downloading {requestUrl} for file {fileTransfer.Hash}"); try { - response = await SendRequestAsync(HttpMethod.Get, requestUrl, ct: ct).ConfigureAwait(false); + response = await SendRequestAsync(HttpMethod.Get, requestUrl, ct).ConfigureAwait(false); response.EnsureSuccessStatusCode(); } catch (HttpRequestException ex) @@ -205,22 +205,23 @@ public partial class ApiController } } - private async Task SendRequestAsync(HttpMethod method, Uri uri, T content = default, CancellationToken? ct = null) where T : class + private async Task SendRequestAsync(HttpMethod method, Uri uri, CancellationToken? ct = null) { using var requestMessage = new HttpRequestMessage(method, uri); - if (content != default) - { - requestMessage.Content = JsonContent.Create(content); - } + return await SendRequestInternalAsync(requestMessage, ct).ConfigureAwait(false); + } + + private async Task SendRequestInternalAsync(HttpRequestMessage requestMessage, CancellationToken? ct = null) + { requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", this.Authorization); - if (content != default) + if (requestMessage.Content != null) { - Logger.Debug("Sending " + method + " to " + uri + " (Content: " + await (((JsonContent)requestMessage.Content).ReadAsStringAsync()) + ")"); + Logger.Debug("Sending " + requestMessage.Method + " to " + requestMessage.RequestUri + " (Content: " + await (((JsonContent)requestMessage.Content).ReadAsStringAsync()) + ")"); } else { - Logger.Debug("Sending " + method + " to " + uri); + Logger.Debug("Sending " + requestMessage.Method + " to " + requestMessage.RequestUri); } if (ct.HasValue) @@ -229,6 +230,13 @@ public partial class ApiController return await _httpClient.SendAsync(requestMessage).ConfigureAwait(false); } + private async Task SendRequestAsync(HttpMethod method, Uri uri, T content, CancellationToken? ct = null) where T : class + { + using var requestMessage = new HttpRequestMessage(method, uri); + requestMessage.Content = JsonContent.Create(content); + return await SendRequestInternalAsync(requestMessage, ct).ConfigureAwait(false); + } + private async Task DownloadFilesInternal(int currentDownloadId, List fileReplacementDto, CancellationToken ct) { Logger.Debug("Downloading files (Download ID " + currentDownloadId + ")");