93 lines
3.5 KiB
C#
93 lines
3.5 KiB
C#
using DigitalData.Core.Legacy.Client;
|
|
using System;
|
|
using System.Net.Http;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace WindreamHub.Legacy.Client.Models
|
|
{
|
|
public static class ModelExtensions
|
|
{
|
|
public static async Task<SimplifiedResponse<TData, TError>> Simplify<TData, TError>(this HttpResponseMessage message)
|
|
{
|
|
TData data = message.IsSuccessStatusCode ? await message.Json<TData>() : default;
|
|
TError err = !message.IsSuccessStatusCode ? await message.Json<TError>() : default;
|
|
|
|
return new SimplifiedResponse<TData, TError>(ok: message.IsSuccessStatusCode, status: message.StatusCode, data: data, error: err);
|
|
}
|
|
|
|
public static async Task FetchAsync<TData, TError>(this Task<SimplifiedResponse<TData, TError>> responseAsync, Action<TData> next, Action<TError> error = null, CancellationToken cancellationToken = default)
|
|
{
|
|
if (cancellationToken.IsCancellationRequested)
|
|
return;
|
|
|
|
var res = await responseAsync;
|
|
if (res.Ok)
|
|
next(res.Data);
|
|
else
|
|
error?.Invoke(res.Error);
|
|
}
|
|
|
|
public static void Fetch<TData, TError>(this Task<SimplifiedResponse<TData, TError>> responseAsync, Action<TData> next, Action<TError> error = null, CancellationToken cancellationToken = default)
|
|
{
|
|
Task.Run(async () =>
|
|
{
|
|
if (cancellationToken.IsCancellationRequested)
|
|
return;
|
|
|
|
var res = await responseAsync;
|
|
if (res.Ok)
|
|
next(res.Data);
|
|
else
|
|
error?.Invoke(res.Error);
|
|
});
|
|
}
|
|
|
|
public static void Fetch<TData, TError>(this Task<SimplifiedResponse<TData, TError>> responseAsync, int millisecondsDelay, Action<TData> next, Action<TError> error = null, CancellationToken cancellationToken = default)
|
|
{
|
|
Task.Run(async () =>
|
|
{
|
|
while (!cancellationToken.IsCancellationRequested)
|
|
{
|
|
var res = await responseAsync;
|
|
if (res.Ok)
|
|
next(res.Data);
|
|
else
|
|
error?.Invoke(res.Error);
|
|
|
|
await Task.Delay(millisecondsDelay, cancellationToken);
|
|
}
|
|
});
|
|
}
|
|
|
|
public static Func<Task> CreateFetchAsync<TData, TError>(this Task<SimplifiedResponse<TData, TError>> responseAsync, Action<TData> next, Action<TError> error = null, CancellationToken cancellationToken = default)
|
|
{
|
|
return async () =>
|
|
{
|
|
if (cancellationToken.IsCancellationRequested)
|
|
return;
|
|
|
|
var res = await responseAsync;
|
|
if (res.Ok)
|
|
next(res.Data);
|
|
else
|
|
error?.Invoke(res.Error);
|
|
};
|
|
}
|
|
|
|
public static Action CreateFetch<TData, TError>(this Task<SimplifiedResponse<TData, TError>> responseAsync, Action<TData> next, Action<TError> error = null, CancellationToken cancellationToken = default)
|
|
{
|
|
return () => Task.Run(async () =>
|
|
{
|
|
if (cancellationToken.IsCancellationRequested)
|
|
return;
|
|
|
|
var res = await responseAsync;
|
|
if (res.Ok)
|
|
next(res.Data);
|
|
else
|
|
error?.Invoke(res.Error);
|
|
});
|
|
}
|
|
}
|
|
} |