Skip to content

Commit

Permalink
Rename BaseUrl to BeeUrl and simplify BeeClient initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm360 committed Oct 27, 2024
1 parent 84769fd commit d5595c6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 48 deletions.
45 changes: 6 additions & 39 deletions src/BeeNet.Client/BeeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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<Dictionary<string, Account>> AccountingAsync(
Expand Down Expand Up @@ -1043,7 +1034,7 @@ public async Task<WebSocket> OpenChunkUploadWebSocketConnectionAsync(
// Build protocol upgrade request.
//url
var urlBuilder = new StringBuilder();
urlBuilder.Append(BaseUrl);
urlBuilder.Append(BeeUrl);
urlBuilder.Append(endpointPath);
var url = urlBuilder.ToString();

Expand Down Expand Up @@ -1462,29 +1453,5 @@ public async Task<string> 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(@"^((?<proto>\w+)://)?(?<host>[^/:]+)",
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);
}
}
}
2 changes: 1 addition & 1 deletion src/BeeNet.Util/IBeeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Etherna.BeeNet
public interface IBeeClient
{
// Properties.
Uri BaseUrl { get; }
Uri BeeUrl { get; }

// Methods.
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SwarmHash> UploadBZZFileAndGetReferenceAsync(string filePath = null)
{
var batch = await beeNodeClient.BuyPostageBatchAsync(500, 32);
Expand Down

0 comments on commit d5595c6

Please sign in to comment.