Move `SendAndRetryAsync` from `TorHttpClientExtensions` to `ITorHttpClient`
Remove `expectedCode` parameter.
MODIFY
WalletWasabi.Tests/RegressionTests/BackendTests.cs
WalletWasabi.Tests/RegressionTests/BackendTests.cs
@@ -194,7 +194,7 @@ namespace WalletWasabi.Tests.RegressionTests
}
using var client = new WasabiClient(new Uri(RegTestFixture.BackendEndPoint), null);
- var response = await client.TorClient.SendAndRetryAsync(HttpMethod.Get, HttpStatusCode.OK, request);
+ var response = await client.TorClient.SendAndRetryAsync(HttpMethod.Get, request);
using (HttpContent content = response.Content)
{
var resp = await content.ReadAsJsonAsync<StatusResponse>();
@@ -207,7 +207,7 @@ namespace WalletWasabi.Tests.RegressionTests
await rpc.GenerateAsync(1);
- response = await client.TorClient.SendAndRetryAsync(HttpMethod.Get, HttpStatusCode.OK, request);
+ response = await client.TorClient.SendAndRetryAsync(HttpMethod.Get, request);
using (HttpContent content = response.Content)
{
var resp = await content.ReadAsJsonAsync<StatusResponse>();
@@ -222,7 +222,7 @@ namespace WalletWasabi.Tests.RegressionTests
// Set back the time to trigger timeout in BlockchainController.GetStatusAsync.
global.IndexBuilderService.LastFilterBuildTime = DateTimeOffset.UtcNow - BlockchainController.FilterTimeout;
- response = await client.TorClient.SendAndRetryAsync(HttpMethod.Get, HttpStatusCode.OK, request);
+ response = await client.TorClient.SendAndRetryAsync(HttpMethod.Get, request);
using (HttpContent content = response.Content)
{
var resp = await content.ReadAsJsonAsync<StatusResponse>();

DELETE
WalletWasabi/Tor/Http/Extensions/TorHttpClientExtensions.cs
-using System;
-using System.Net;
-using System.Net.Http;
-using System.Threading;
-using System.Threading.Tasks;
-using WalletWasabi.Tor.Http.Interfaces;
-
-namespace WalletWasabi.Tor.Http.Extensions
-{
- public static class TorHttpClientExtensions
- {
- /// <remarks>
- /// Throws OperationCancelledException if <paramref name="cancel"/> is set.
- /// </remarks>
- public static async Task<HttpResponseMessage> SendAndRetryAsync(this ITorHttpClient client, HttpMethod method, HttpStatusCode expectedCode, string relativeUri, int retry = 2, HttpContent content = null, CancellationToken cancel = default)
- {
- HttpResponseMessage response = null;
- while (retry-- > 0)
- {
- response?.Dispose();
- cancel.ThrowIfCancellationRequested();
- response = await client.SendAsync(method, relativeUri, content, cancel: cancel);
- if (response.StatusCode == expectedCode)
- {
- break;
- }
- try
- {
- await Task.Delay(1000, cancel);
- }
- catch (TaskCanceledException ex)
- {
- throw new OperationCanceledException(ex.Message, ex, cancel);
- }
- }
- return response;
- }
- }
-}

MODIFY
WalletWasabi/Tor/Http/Interfaces/ITorHttpClient.cs
WalletWasabi/Tor/Http/Interfaces/ITorHttpClient.cs
@@ -14,8 +14,40 @@ namespace WalletWasabi.Tor.Http.Interfaces
bool IsTorUsed { get; }
- Task<HttpResponseMessage> SendAsync(HttpMethod method, string relativeUri, HttpContent content = null, CancellationToken cancel = default);
+ Task<HttpResponseMessage> SendAsync(HttpMethod method, string relativeUri, HttpContent? content = null, CancellationToken cancel = default);
Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancel = default);
+
+ /// <summary>
+ /// Sends HTTP request (up to <paramref name="retry"/> times) to get HTTP response.
+ /// </summary>
+ /// <param name="retry">Maximum number of attempts to do to get HTTP response.</param>
+ /// <exception cref="OperationCanceledException">When <paramref name="token"/> is used to cancel operation.</exception>
+ public async Task<HttpResponseMessage?> SendAndRetryAsync(HttpMethod method, string relativeUri, int retry = 2, HttpContent? content = null, CancellationToken token = default)
+ {
+ HttpResponseMessage? response = null;
+
+ while (retry-- > 0)
+ {
+ response?.Dispose();
+ token.ThrowIfCancellationRequested();
+ response = await SendAsync(method, relativeUri, content, cancel: token).ConfigureAwait(false);
+
+ if (response.StatusCode == HttpStatusCode.OK)
+ {
+ break;
+ }
+
+ try
+ {
+ await Task.Delay(1000, token).ConfigureAwait(false);
+ }
+ catch (TaskCanceledException ex)
+ {
+ throw new OperationCanceledException(ex.Message, ex, token);
+ }
+ }
+ return response;
+ }
}
}

MODIFY
WalletWasabi/WebClients/Wasabi/WasabiClient.cs
WalletWasabi/WebClients/Wasabi/WasabiClient.cs
@@ -52,7 +52,7 @@ namespace WalletWasabi.WebClients.Wasabi
relativeUri = $"{relativeUri}&estimateSmartFeeMode={estimateMode}";
}
- using var response = await TorClient.SendAndRetryAsync(HttpMethod.Get, HttpStatusCode.OK, relativeUri, cancel: cancel).ConfigureAwait(false);
+ using var response = await TorClient.SendAndRetryAsync(HttpMethod.Get, relativeUri, token: cancel).ConfigureAwait(false);
if (response.StatusCode != HttpStatusCode.OK)
{
@@ -75,9 +75,8 @@ namespace WalletWasabi.WebClients.Wasabi
{
using var response = await TorClient.SendAndRetryAsync(
HttpMethod.Get,
- HttpStatusCode.OK,
$"/api/v{ApiVersion}/btc/blockchain/filters?bestKnownBlockHash={bestKnownBlockHash}&count={count}",
- cancel: cancel).ConfigureAwait(false);
+ token: cancel).ConfigureAwait(false);
if (response.StatusCode == HttpStatusCode.NoContent)
{
@@ -110,9 +109,8 @@ namespace WalletWasabi.WebClients.Wasabi
using var response = await TorClient.SendAndRetryAsync(
HttpMethod.Get,
- HttpStatusCode.OK,
$"/api/v{ApiVersion}/btc/blockchain/transaction-hexes?&transactionIds={string.Join("&transactionIds=", chunk.Select(x => x.ToString()))}",
- cancel: cancel).ConfigureAwait(false);
+ token: cancel).ConfigureAwait(false);
if (response.StatusCode != HttpStatusCode.OK)
{
@@ -169,9 +167,8 @@ namespace WalletWasabi.WebClients.Wasabi
{
using var response = await TorClient.SendAndRetryAsync(
HttpMethod.Get,
- HttpStatusCode.OK,
$"/api/v{ApiVersion}/btc/blockchain/mempool-hashes",
- cancel: cancel).ConfigureAwait(false);
+ token: cancel).ConfigureAwait(false);
if (response.StatusCode != HttpStatusCode.OK)
{
@@ -192,9 +189,8 @@ namespace WalletWasabi.WebClients.Wasabi
{
using var response = await TorClient.SendAndRetryAsync(
HttpMethod.Get,
- HttpStatusCode.OK,
$"/api/v{ApiVersion}/btc/blockchain/mempool-hashes?compactness={compactness}",
- cancel: cancel).ConfigureAwait(false);
+ token: cancel).ConfigureAwait(false);
if (response.StatusCode != HttpStatusCode.OK)
{
@@ -212,7 +208,7 @@ namespace WalletWasabi.WebClients.Wasabi
public async Task<IEnumerable<ExchangeRate>> GetExchangeRatesAsync()
{
- using var response = await TorClient.SendAndRetryAsync(HttpMethod.Get, HttpStatusCode.OK, $"/api/v{ApiVersion}/btc/offchain/exchange-rates").ConfigureAwait(false);
+ using var response = await TorClient.SendAndRetryAsync(HttpMethod.Get, $"/api/v{ApiVersion}/btc/offchain/exchange-rates").ConfigureAwait(false);
if (response.StatusCode != HttpStatusCode.OK)
{
@@ -230,7 +226,7 @@ namespace WalletWasabi.WebClients.Wasabi
public async Task<(Version ClientVersion, ushort BackendMajorVersion, Version LegalDocumentsVersion)> GetVersionsAsync(CancellationToken cancel)
{
- using var response = await TorClient.SendAndRetryAsync(HttpMethod.Get, HttpStatusCode.OK, "/api/software/versions", cancel: cancel).ConfigureAwait(false);
+ using var response = await TorClient.SendAndRetryAsync(HttpMethod.Get, "/api/software/versions", token: cancel).ConfigureAwait(false);
if (response.StatusCode != HttpStatusCode.OK)
{
@@ -267,9 +263,8 @@ namespace WalletWasabi.WebClients.Wasabi
{
using var response = await TorClient.SendAndRetryAsync(
HttpMethod.Get,
- HttpStatusCode.OK,
$"/api/v{ApiVersion}/wasabi/legaldocuments",
- cancel: cancel).ConfigureAwait(false);
+ token: cancel).ConfigureAwait(false);
if (response.StatusCode != HttpStatusCode.OK)
{

Move `SendAndRetryAsync` from `TorHttpClientExtensions` to `ITorHttpClient`

Remove `expectedCode` parameter.