first commit -push
This commit is contained in:
36
RobotNet.Clients/HttpClientExtensions.cs
Normal file
36
RobotNet.Clients/HttpClientExtensions.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Net.Http.Json;
|
||||
|
||||
namespace RobotNet.Clients;
|
||||
|
||||
public static class HttpClientExtensions
|
||||
{
|
||||
public static async Task<TValue?> PostFromJsonAsync<TValue>(this HttpClient client, string requestUri, object value)
|
||||
{
|
||||
var response = await client.PostAsJsonAsync(requestUri, value);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return await response.Content.ReadFromJsonAsync<TValue>();
|
||||
}
|
||||
return default;
|
||||
}
|
||||
|
||||
public static async Task<TValue?> PutFromJsonAsync<TValue>(this HttpClient client, string requestUri, object value)
|
||||
{
|
||||
var response = await client.PutAsJsonAsync(requestUri, value);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return await response.Content.ReadFromJsonAsync<TValue>();
|
||||
}
|
||||
return default;
|
||||
}
|
||||
|
||||
public static async Task<TValue?> PatchFromJsonAsync<TValue>(this HttpClient client, string requestUri, object value)
|
||||
{
|
||||
var response = await client.PatchAsJsonAsync(requestUri, value);
|
||||
if (response.IsSuccessStatusCode)
|
||||
{
|
||||
return await response.Content.ReadFromJsonAsync<TValue>();
|
||||
}
|
||||
return default;
|
||||
}
|
||||
}
|
||||
60
RobotNet.Clients/HubClient.cs
Normal file
60
RobotNet.Clients/HubClient.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using Microsoft.AspNetCore.SignalR.Client;
|
||||
|
||||
namespace RobotNet.Clients;
|
||||
|
||||
public abstract class HubClient
|
||||
{
|
||||
public event Action<HubConnectionState>? ConnectionStateChanged;
|
||||
public bool IsConnected => Connection.State == HubConnectionState.Connected;
|
||||
protected HubConnection Connection { get; }
|
||||
|
||||
protected HubClient(Uri url, Func<Task<string?>> accessTokenProvider)
|
||||
{
|
||||
Connection = new HubConnectionBuilder()
|
||||
.WithUrl(url, options =>
|
||||
{
|
||||
options.AccessTokenProvider = accessTokenProvider;
|
||||
})
|
||||
.WithAutomaticReconnect(new HubClientRepeatRetryPolicy(TimeSpan.FromSeconds(3)))
|
||||
.Build();
|
||||
|
||||
Connection.Closed += Connection_Closed;
|
||||
Connection.Reconnected += Connection_Reconnected;
|
||||
}
|
||||
|
||||
private Task Connection_Closed(Exception? arg)
|
||||
{
|
||||
ConnectionStateChanged?.Invoke(Connection.State);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
private Task Connection_Reconnected(string? arg)
|
||||
{
|
||||
ConnectionStateChanged?.Invoke(Connection.State);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public virtual async Task StartAsync()
|
||||
{
|
||||
if (Connection.State == HubConnectionState.Disconnected)
|
||||
{
|
||||
await Connection.StartAsync();
|
||||
ConnectionStateChanged?.Invoke(Connection.State);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task StopAsync()
|
||||
{
|
||||
if (Connection.State != HubConnectionState.Disconnected)
|
||||
{
|
||||
await Connection.StopAsync();
|
||||
ConnectionStateChanged?.Invoke(Connection.State);
|
||||
}
|
||||
}
|
||||
|
||||
public class HubClientRepeatRetryPolicy(TimeSpan repeatSpan) : IRetryPolicy
|
||||
{
|
||||
private readonly TimeSpan RepeatTimeSpan = repeatSpan;
|
||||
|
||||
public TimeSpan? NextRetryDelay(RetryContext retryContext) => RepeatTimeSpan;
|
||||
}
|
||||
}
|
||||
13
RobotNet.Clients/RobotNet.Clients.csproj
Normal file
13
RobotNet.Clients/RobotNet.Clients.csproj
Normal file
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="9.0.8" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user