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