first commit -push

This commit is contained in:
dungtt
2025-10-15 15:15:53 +07:00
parent 674ae395be
commit a9577c5756
885 changed files with 74595 additions and 0 deletions

View 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;
}
}

View 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;
}
}

View 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>