From 02492b4438bbdae04f13ff624eafe63b2b5298a3 Mon Sep 17 00:00:00 2001 From: Artem Levin <64748654+Leo506@users.noreply.github.com> Date: Tue, 17 Oct 2023 12:42:26 +0500 Subject: [PATCH] Rename INetworkManager to INetworkMonitor (#48) Co-authored-by: Artem Levin --- README.md | 4 ++-- samples/Arbus.Network.Demo/Program.cs | 17 ++++---------- .../Tests/NativeHttpClientTests.cs | 12 +++++----- ...{INetworkManager.cs => INetworkMonitor.cs} | 2 +- src/Arbus.Network/NativeHttpClient.cs | 10 ++++---- src/Arbus.Network/WindowsHttpClient.cs | 4 ++-- src/Arbus.Network/WindowsNetworkManager.cs | 23 ------------------- src/Arbus.Network/WindowsNetworkMonitor.cs | 17 ++++++++++++++ 8 files changed, 38 insertions(+), 51 deletions(-) rename src/Arbus.Network/{INetworkManager.cs => INetworkMonitor.cs} (79%) delete mode 100644 src/Arbus.Network/WindowsNetworkManager.cs create mode 100644 src/Arbus.Network/WindowsNetworkMonitor.cs diff --git a/README.md b/README.md index d549346..b191f01 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,8 @@ Let's build a better world together! ## How to use 1. Provide your own implementation or use default one for interfaces: INetworkManager, INativeHttpClient, IHttpClientContext: ```c# - _networkManager = new WindowsNetworkManager(); - _nativeHttpClient = new WindowsHttpClient(_networkManager); + _networkMonitor = new WindowsNetworkMonitor(); + _nativeHttpClient = new WindowsHttpClient(_networkMonitor); _httpClientContext = new HttpClientContext(_nativeHttpClient); 2. Use ApiEndpoint as base class for your API endpoints: diff --git a/samples/Arbus.Network.Demo/Program.cs b/samples/Arbus.Network.Demo/Program.cs index 779f533..a5dbbae 100644 --- a/samples/Arbus.Network.Demo/Program.cs +++ b/samples/Arbus.Network.Demo/Program.cs @@ -4,23 +4,16 @@ namespace Arbus.Network.Demo; public static class Program { - private static readonly INetworkManager _networkManager; - private static readonly INativeHttpClient _nativeHttpClient; - private static readonly IHttpClientContext _httpClientContext; - - static Program() - { - _networkManager = new WindowsNetworkManager(); - _nativeHttpClient = new WindowsHttpClient(_networkManager); - _httpClientContext = new HttpClientContext(_nativeHttpClient); - } - public static async Task Main() { + var networkMonitor = new WindowsNetworkMonitor(); + var nativeHttpClient = new WindowsHttpClient(networkMonitor); + var httpClientContext = new HttpClientContext(nativeHttpClient); + try { GetAllOrdersApiEndpoint endpoint = new(); - var orders = await _httpClientContext.RunEndpoint(endpoint).ConfigureAwait(false); + await httpClientContext.RunEndpoint(endpoint).ConfigureAwait(false); } catch (NetworkException e) { diff --git a/src/Arbus.Network.UnitTests/Tests/NativeHttpClientTests.cs b/src/Arbus.Network.UnitTests/Tests/NativeHttpClientTests.cs index 353610b..36b1655 100644 --- a/src/Arbus.Network.UnitTests/Tests/NativeHttpClientTests.cs +++ b/src/Arbus.Network.UnitTests/Tests/NativeHttpClientTests.cs @@ -25,10 +25,10 @@ public void EnsureNoTimeout_CancellationNotRequested_NoException() [Test] public void EnsureNetworkAvailable_NetworkNotAvailable_ThrowsNoNetworkConnectionAvailableException() { - var mockNetworkManager = new Mock(); - mockNetworkManager.SetupGet(x => x.IsNetworkAvailable).Returns(false); + var mockNetworkMonitor = new Mock(); + mockNetworkMonitor.SetupGet(x => x.IsNetworkAvailable).Returns(false); - NativeHttpClient nativeHttpClient = new(mockNetworkManager.Object); + NativeHttpClient nativeHttpClient = new(mockNetworkMonitor.Object); Assert.Throws(() => nativeHttpClient.EnsureNetworkAvailable()); } @@ -36,10 +36,10 @@ public void EnsureNetworkAvailable_NetworkNotAvailable_ThrowsNoNetworkConnection [Test] public void EnsureNetworkAvailable_NetworkAvailable_NoException() { - var mockNetworkManager = new Mock(); - mockNetworkManager.SetupGet(x => x.IsNetworkAvailable).Returns(true); + var mockNetworkMonitor = new Mock(); + mockNetworkMonitor.SetupGet(x => x.IsNetworkAvailable).Returns(true); - NativeHttpClient nativeHttpClient = new(mockNetworkManager.Object); + NativeHttpClient nativeHttpClient = new(mockNetworkMonitor.Object); Assert.DoesNotThrow(() => nativeHttpClient.EnsureNetworkAvailable()); } diff --git a/src/Arbus.Network/INetworkManager.cs b/src/Arbus.Network/INetworkMonitor.cs similarity index 79% rename from src/Arbus.Network/INetworkManager.cs rename to src/Arbus.Network/INetworkMonitor.cs index 5e7662a..503826e 100644 --- a/src/Arbus.Network/INetworkManager.cs +++ b/src/Arbus.Network/INetworkMonitor.cs @@ -1,6 +1,6 @@ namespace Arbus.Network; -public interface INetworkManager +public interface INetworkMonitor { bool IsNetworkAvailable { get; } event EventHandler? NetworkAvailabilityChanged; diff --git a/src/Arbus.Network/NativeHttpClient.cs b/src/Arbus.Network/NativeHttpClient.cs index a8832cf..047309c 100644 --- a/src/Arbus.Network/NativeHttpClient.cs +++ b/src/Arbus.Network/NativeHttpClient.cs @@ -10,16 +10,16 @@ public class NativeHttpClient : INativeHttpClient { Timeout = Timeout.InfiniteTimeSpan }; - private readonly INetworkManager _networkManager; + private readonly INetworkMonitor _networkMonitor; - public NativeHttpClient(INetworkManager networkManager, ProductInfoHeaderValue userAgent) : this(networkManager) + public NativeHttpClient(INetworkMonitor networkMonitor, ProductInfoHeaderValue userAgent) : this(networkMonitor) { _httpClient.DefaultRequestHeaders.UserAgent.Add(userAgent); } - public NativeHttpClient(INetworkManager networkManager) + public NativeHttpClient(INetworkMonitor networkMonitor) { - _networkManager = networkManager; + _networkMonitor = networkMonitor; } public virtual async Task SendRequest(HttpRequestMessage request, CancellationToken cancellationToken, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseHeadersRead) @@ -60,7 +60,7 @@ public virtual async Task SendRequest(HttpRequestMessage re public void EnsureNetworkAvailable() { - if (_networkManager.IsNetworkAvailable is false) + if (_networkMonitor.IsNetworkAvailable is false) throw new NoNetworkConnectionException(); } diff --git a/src/Arbus.Network/WindowsHttpClient.cs b/src/Arbus.Network/WindowsHttpClient.cs index 560d4da..4260c6d 100644 --- a/src/Arbus.Network/WindowsHttpClient.cs +++ b/src/Arbus.Network/WindowsHttpClient.cs @@ -16,12 +16,12 @@ static void SetTlsForWindows7() } } - public WindowsHttpClient(INetworkManager networkManager) : base(networkManager) + public WindowsHttpClient(INetworkMonitor networkMonitor) : base(networkMonitor) { } - public WindowsHttpClient(INetworkManager networkManager, ProductInfoHeaderValue userAgent) : base(networkManager, userAgent) + public WindowsHttpClient(INetworkMonitor networkMonitor, ProductInfoHeaderValue userAgent) : base(networkMonitor, userAgent) { } } \ No newline at end of file diff --git a/src/Arbus.Network/WindowsNetworkManager.cs b/src/Arbus.Network/WindowsNetworkManager.cs deleted file mode 100644 index 9addffa..0000000 --- a/src/Arbus.Network/WindowsNetworkManager.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System.Net.NetworkInformation; - -namespace Arbus.Network; - -public class WindowsNetworkManager : INetworkManager -{ - public event EventHandler? NetworkAvailabilityChanged; - - public bool IsNetworkAvailable => GetIsNetworkAvailable(); - - //NetworkInterface.GetIsNetworkAvailable() always returns 'true' for Xamarin.iOS and Xamarin.Android - public static bool GetIsNetworkAvailable() - { - foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces()) - { - if (networkInterface.OperationalStatus == OperationalStatus.Up - && networkInterface.NetworkInterfaceType != NetworkInterfaceType.Tunnel - && networkInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback) - return true; - } - return false; - } -} \ No newline at end of file diff --git a/src/Arbus.Network/WindowsNetworkMonitor.cs b/src/Arbus.Network/WindowsNetworkMonitor.cs new file mode 100644 index 0000000..9c2a277 --- /dev/null +++ b/src/Arbus.Network/WindowsNetworkMonitor.cs @@ -0,0 +1,17 @@ +using System.Net.NetworkInformation; + +namespace Arbus.Network; + +public class WindowsNetworkMonitor : INetworkMonitor +{ + public event EventHandler? NetworkAvailabilityChanged; + + public bool IsNetworkAvailable => NetworkInterface.GetAllNetworkInterfaces().Any(IsNetworkInterfaceAvailable); + + private static bool IsNetworkInterfaceAvailable(NetworkInterface networkInterface) + { + return networkInterface.OperationalStatus == OperationalStatus.Up && + networkInterface.NetworkInterfaceType != NetworkInterfaceType.Tunnel && + networkInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback; + } +} \ No newline at end of file