using DigitalData.Core.Legacy.Client; using System; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using Newtonsoft.Json; using System.Text; using System.Collections.Generic; namespace WindreamHub.Legacy.Client.Models { public static class ModelExtensions { public static async Task> Simplify(this HttpResponseMessage message) { TData data = message.IsSuccessStatusCode ? await message.Json() : default; TError err = !message.IsSuccessStatusCode ? await message.Json() : default; return new SimplifiedResponse(ok: message.IsSuccessStatusCode, status: message.StatusCode, data: data, error: err); } public static async Task FetchAsync(this Task> responseAsync, Action next, Action 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(this Task> responseAsync, Action next, Action 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(this Task> responseAsync, int millisecondsDelay, Action next, Action 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 CreateFetchAsync(this Task> responseAsync, Action next, Action 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(this Task> responseAsync, Action next, Action 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); }); } public static string Serialize(this object model) => JsonConvert.SerializeObject(model); public static T Deserialize(this string json) => JsonConvert.DeserializeObject(json); public static dynamic Deserialize(this string json) => JsonConvert.DeserializeObject(json); public static IEnumerable DeserializeList(this string json) => JsonConvert.DeserializeObject>(json); public static HttpContent ToContent(this string json, Encoding encoding = null, string mediaType = "application/json") => new StringContent(json, encoding ?? Encoding.UTF8, mediaType); public static HttpContent Stringify(this object model, Encoding encoding = null, string mediaType = "application/json") => model.Serialize().ToContent(encoding ?? Encoding.UTF8, mediaType); } }