From d5595c6ee8766140ff99fa1664a4608dff2cba08 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Sun, 27 Oct 2024 15:29:56 +0100 Subject: [PATCH] Rename `BaseUrl` to `BeeUrl` and simplify BeeClient initialization --- src/BeeNet.Client/BeeClient.cs | 45 +++---------------- src/BeeNet.Util/IBeeClient.cs | 2 +- .../v1_13_2/BaseTest_Gateway_v5_0_0.cs | 10 +---- 3 files changed, 9 insertions(+), 48 deletions(-) diff --git a/src/BeeNet.Client/BeeClient.cs b/src/BeeNet.Client/BeeClient.cs index b609ddc..b5f8914 100644 --- a/src/BeeNet.Client/BeeClient.cs +++ b/src/BeeNet.Client/BeeClient.cs @@ -31,7 +31,6 @@ using System.Net.WebSockets; using System.Security.Cryptography; using System.Text; -using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using FileResponse = Etherna.BeeNet.Models.FileResponse; @@ -61,22 +60,15 @@ public class BeeClient : IBeeClient, IDisposable private bool disposed; - // Constructors. + // Constructor. public BeeClient( - string baseUrl = "http://localhost/", - int port = DefaultPort, - HttpClient? httpClient = null) - : this(BuildBaseUrl(baseUrl, port), httpClient) - { } - - public BeeClient( - Uri baseUrl, + Uri beeUrl, HttpClient? httpClient = null) { this.httpClient = httpClient ?? new HttpClient { Timeout = DefaultTimeout }; - BaseUrl = baseUrl; - generatedClient = new BeeGeneratedClient(this.httpClient) { BaseUrl = BaseUrl.ToString() }; + BeeUrl = beeUrl; + generatedClient = new BeeGeneratedClient(this.httpClient) { BaseUrl = BeeUrl.ToString() }; } // Dispose. @@ -97,9 +89,8 @@ protected virtual void Dispose(bool disposing) disposed = true; } - // Properties. - public Uri BaseUrl { get; } + public Uri BeeUrl { get; } // Methods. public async Task> AccountingAsync( @@ -1043,7 +1034,7 @@ public async Task OpenChunkUploadWebSocketConnectionAsync( // Build protocol upgrade request. //url var urlBuilder = new StringBuilder(); - urlBuilder.Append(BaseUrl); + urlBuilder.Append(BeeUrl); urlBuilder.Append(endpointPath); var url = urlBuilder.ToString(); @@ -1462,29 +1453,5 @@ public async Task WithdrawFromChequebookAsync( amount.ToPlurLong(), gasPrice?.ToWeiLong(), cancellationToken).ConfigureAwait(false)).TransactionHash; - - // Helpers. - private static Uri BuildBaseUrl(string url, int port) - { - var normalizedUrl = url; - if (normalizedUrl.Last() != '/') - normalizedUrl += '/'; - - var baseUrl = ""; - - var urlRegex = new Regex(@"^((?\w+)://)?(?[^/:]+)", - RegexOptions.None, TimeSpan.FromMilliseconds(150)); - var urlMatch = urlRegex.Match(normalizedUrl); - - if (!urlMatch.Success) - throw new ArgumentException("Url is not valid", nameof(url)); - - if (!string.IsNullOrEmpty(urlMatch.Groups["proto"].Value)) - baseUrl += urlMatch.Groups["proto"].Value + "://"; - - baseUrl += $"{urlMatch.Groups["host"].Value}:{port}"; - - return new Uri(baseUrl); - } } } \ No newline at end of file diff --git a/src/BeeNet.Util/IBeeClient.cs b/src/BeeNet.Util/IBeeClient.cs index e01e185..a81b086 100644 --- a/src/BeeNet.Util/IBeeClient.cs +++ b/src/BeeNet.Util/IBeeClient.cs @@ -28,7 +28,7 @@ namespace Etherna.BeeNet public interface IBeeClient { // Properties. - Uri BaseUrl { get; } + Uri BeeUrl { get; } // Methods. /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. diff --git a/test/BeeNet.Client.IntegrationTest/BeeVersions/v1_13_2/BaseTest_Gateway_v5_0_0.cs b/test/BeeNet.Client.IntegrationTest/BeeVersions/v1_13_2/BaseTest_Gateway_v5_0_0.cs index 174a709..8ceba8e 100644 --- a/test/BeeNet.Client.IntegrationTest/BeeVersions/v1_13_2/BaseTest_Gateway_v5_0_0.cs +++ b/test/BeeNet.Client.IntegrationTest/BeeVersions/v1_13_2/BaseTest_Gateway_v5_0_0.cs @@ -22,17 +22,11 @@ namespace BeeNet.IntegrationTest.BeeVersions.v1_13_2 { public abstract class BaseTest_Gateway_v5_0_0 { - protected BeeClient beeNodeClient; + protected BeeClient beeNodeClient = new( + new Uri(Environment.GetEnvironmentVariable("BeeNet_IT_NodeEndPoint") ?? "http://127.0.0.1:1633/")); protected string pathTestFileForUpload = "Data/TestFileForUpload_Gateway.txt"; protected const string version = "4.0.0"; - public BaseTest_Gateway_v5_0_0() - { - beeNodeClient = new BeeClient( - Environment.GetEnvironmentVariable("BeeNet_IT_NodeEndPoint") ?? "http://127.0.0.1/", - 1633); - } - protected async Task UploadBZZFileAndGetReferenceAsync(string filePath = null) { var batch = await beeNodeClient.BuyPostageBatchAsync(500, 32);