Skip to content

Commit

Permalink
Rename INetworkManager to INetworkMonitor (#48)
Browse files Browse the repository at this point in the history
Co-authored-by: Artem Levin <[email protected]>
  • Loading branch information
Leo506 and Artem Levin authored Oct 17, 2023
1 parent 0f62cce commit 02492b4
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 51 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 5 additions & 12 deletions samples/Arbus.Network.Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
12 changes: 6 additions & 6 deletions src/Arbus.Network.UnitTests/Tests/NativeHttpClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ public void EnsureNoTimeout_CancellationNotRequested_NoException()
[Test]
public void EnsureNetworkAvailable_NetworkNotAvailable_ThrowsNoNetworkConnectionAvailableException()
{
var mockNetworkManager = new Mock<INetworkManager>();
mockNetworkManager.SetupGet(x => x.IsNetworkAvailable).Returns(false);
var mockNetworkMonitor = new Mock<INetworkMonitor>();
mockNetworkMonitor.SetupGet(x => x.IsNetworkAvailable).Returns(false);

NativeHttpClient nativeHttpClient = new(mockNetworkManager.Object);
NativeHttpClient nativeHttpClient = new(mockNetworkMonitor.Object);

Assert.Throws<NoNetworkConnectionException>(() => nativeHttpClient.EnsureNetworkAvailable());
}

[Test]
public void EnsureNetworkAvailable_NetworkAvailable_NoException()
{
var mockNetworkManager = new Mock<INetworkManager>();
mockNetworkManager.SetupGet(x => x.IsNetworkAvailable).Returns(true);
var mockNetworkMonitor = new Mock<INetworkMonitor>();
mockNetworkMonitor.SetupGet(x => x.IsNetworkAvailable).Returns(true);

NativeHttpClient nativeHttpClient = new(mockNetworkManager.Object);
NativeHttpClient nativeHttpClient = new(mockNetworkMonitor.Object);

Assert.DoesNotThrow(() => nativeHttpClient.EnsureNetworkAvailable());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Arbus.Network;

public interface INetworkManager
public interface INetworkMonitor
{
bool IsNetworkAvailable { get; }
event EventHandler<bool>? NetworkAvailabilityChanged;
Expand Down
10 changes: 5 additions & 5 deletions src/Arbus.Network/NativeHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpResponseMessage> SendRequest(HttpRequestMessage request, CancellationToken cancellationToken, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseHeadersRead)
Expand Down Expand Up @@ -60,7 +60,7 @@ public virtual async Task<HttpResponseMessage> SendRequest(HttpRequestMessage re

public void EnsureNetworkAvailable()
{
if (_networkManager.IsNetworkAvailable is false)
if (_networkMonitor.IsNetworkAvailable is false)
throw new NoNetworkConnectionException();
}

Expand Down
4 changes: 2 additions & 2 deletions src/Arbus.Network/WindowsHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}
}
23 changes: 0 additions & 23 deletions src/Arbus.Network/WindowsNetworkManager.cs

This file was deleted.

17 changes: 17 additions & 0 deletions src/Arbus.Network/WindowsNetworkMonitor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Net.NetworkInformation;

namespace Arbus.Network;

public class WindowsNetworkMonitor : INetworkMonitor
{
public event EventHandler<bool>? 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;
}
}

0 comments on commit 02492b4

Please sign in to comment.