fix SendRequestAsync
This commit is contained in:
2
MareAPI
2
MareAPI
Submodule MareAPI updated: 9fea4c30da...2015496ec0
@@ -50,7 +50,7 @@ public partial class ApiController
|
|||||||
|
|
||||||
private async Task<Guid> GetQueueRequest(DownloadFileTransfer downloadFileTransfer, CancellationToken ct)
|
private async Task<Guid> GetQueueRequest(DownloadFileTransfer downloadFileTransfer, CancellationToken ct)
|
||||||
{
|
{
|
||||||
var response = await SendRequestAsync<object>(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 responseString = await response.Content.ReadAsStringAsync(ct).ConfigureAwait(false);
|
||||||
var requestId = Guid.Parse(responseString.Trim('"'));
|
var requestId = Guid.Parse(responseString.Trim('"'));
|
||||||
if (!_downloadReady.ContainsKey(requestId))
|
if (!_downloadReady.ContainsKey(requestId))
|
||||||
@@ -79,7 +79,7 @@ public partial class ApiController
|
|||||||
{
|
{
|
||||||
if (downloadCt.IsCancellationRequested) throw;
|
if (downloadCt.IsCancellationRequested) throw;
|
||||||
|
|
||||||
var req = await SendRequestAsync<object>(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
|
try
|
||||||
{
|
{
|
||||||
req.EnsureSuccessStatusCode();
|
req.EnsureSuccessStatusCode();
|
||||||
@@ -105,7 +105,7 @@ public partial class ApiController
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await SendRequestAsync<object>(HttpMethod.Get, MareFiles.RequestCancelFullPath(downloadFileTransfer.DownloadUri, requestId), downloadCt).ConfigureAwait(false);
|
await SendRequestAsync(HttpMethod.Get, MareFiles.RequestCancelFullPath(downloadFileTransfer.DownloadUri, requestId), downloadCt).ConfigureAwait(false);
|
||||||
alreadyCancelled = true;
|
alreadyCancelled = true;
|
||||||
}
|
}
|
||||||
catch { }
|
catch { }
|
||||||
@@ -118,7 +118,7 @@ public partial class ApiController
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await SendRequestAsync<object>(HttpMethod.Get, MareFiles.RequestCancelFullPath(downloadFileTransfer.DownloadUri, requestId), downloadCt).ConfigureAwait(false);
|
await SendRequestAsync(HttpMethod.Get, MareFiles.RequestCancelFullPath(downloadFileTransfer.DownloadUri, requestId), downloadCt).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
catch { }
|
catch { }
|
||||||
}
|
}
|
||||||
@@ -140,7 +140,7 @@ public partial class ApiController
|
|||||||
Logger.Debug($"Downloading {requestUrl} for file {fileTransfer.Hash}");
|
Logger.Debug($"Downloading {requestUrl} for file {fileTransfer.Hash}");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
response = await SendRequestAsync<object>(HttpMethod.Get, requestUrl, ct: ct).ConfigureAwait(false);
|
response = await SendRequestAsync(HttpMethod.Get, requestUrl, ct).ConfigureAwait(false);
|
||||||
response.EnsureSuccessStatusCode();
|
response.EnsureSuccessStatusCode();
|
||||||
}
|
}
|
||||||
catch (HttpRequestException ex)
|
catch (HttpRequestException ex)
|
||||||
@@ -205,22 +205,23 @@ public partial class ApiController
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<HttpResponseMessage> SendRequestAsync<T>(HttpMethod method, Uri uri, T content = default, CancellationToken? ct = null) where T : class
|
private async Task<HttpResponseMessage> SendRequestAsync(HttpMethod method, Uri uri, CancellationToken? ct = null)
|
||||||
{
|
{
|
||||||
using var requestMessage = new HttpRequestMessage(method, uri);
|
using var requestMessage = new HttpRequestMessage(method, uri);
|
||||||
if (content != default)
|
return await SendRequestInternalAsync(requestMessage, ct).ConfigureAwait(false);
|
||||||
{
|
|
||||||
requestMessage.Content = JsonContent.Create(content);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task<HttpResponseMessage> SendRequestInternalAsync(HttpRequestMessage requestMessage, CancellationToken? ct = null)
|
||||||
|
{
|
||||||
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", this.Authorization);
|
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
|
else
|
||||||
{
|
{
|
||||||
Logger.Debug("Sending " + method + " to " + uri);
|
Logger.Debug("Sending " + requestMessage.Method + " to " + requestMessage.RequestUri);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ct.HasValue)
|
if (ct.HasValue)
|
||||||
@@ -229,6 +230,13 @@ public partial class ApiController
|
|||||||
return await _httpClient.SendAsync(requestMessage).ConfigureAwait(false);
|
return await _httpClient.SendAsync(requestMessage).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task<HttpResponseMessage> SendRequestAsync<T>(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> fileReplacementDto, CancellationToken ct)
|
private async Task DownloadFilesInternal(int currentDownloadId, List<FileReplacementDto> fileReplacementDto, CancellationToken ct)
|
||||||
{
|
{
|
||||||
Logger.Debug("Downloading files (Download ID " + currentDownloadId + ")");
|
Logger.Debug("Downloading files (Download ID " + currentDownloadId + ")");
|
||||||
|
|||||||
Reference in New Issue
Block a user