using Mesa_Backlog.Library.WorkItems; using System.Text.Json; namespace Mesa_Backlog.Library; public class HttpClient { public static Root GetWorkItem(System.Net.Http.HttpClient httpClient, string basePage, string api, int id) { Root result; Task httpResponseMessageTask = httpClient.GetAsync(string.Concat(basePage, api, "/workItems/", id)); httpResponseMessageTask.Wait(); if (!httpResponseMessageTask.Result.IsSuccessStatusCode) throw new Exception(httpResponseMessageTask.Result.StatusCode.ToString()); Task streamTask = httpResponseMessageTask.Result.Content.ReadAsStreamAsync(); streamTask.Wait(); if (!streamTask.Result.CanRead) { JsonElement? jsonElement = JsonSerializer.Deserialize(streamTask.Result); if (jsonElement is null) throw new NullReferenceException(nameof(jsonElement)); } Root? root = JsonSerializer.Deserialize(streamTask.Result, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }); streamTask.Result.Dispose(); if (root is null || root.Fields is null) throw new NullReferenceException(nameof(root)); result = root; return result; } public static WIQL.WorkItem[] GetWorkItems(System.Net.Http.HttpClient httpClient, string basePage, string api, string query) { WIQL.WorkItem[] results; Task httpResponseMessageTask = httpClient.GetAsync(string.Concat(basePage, api, query)); httpResponseMessageTask.Wait(); if (!httpResponseMessageTask.Result.IsSuccessStatusCode) throw new Exception(httpResponseMessageTask.Result.StatusCode.ToString()); Task streamTask = httpResponseMessageTask.Result.Content.ReadAsStreamAsync(); streamTask.Wait(); if (!streamTask.Result.CanRead) { JsonElement? jsonElement = JsonSerializer.Deserialize(streamTask.Result); if (jsonElement is null) throw new NullReferenceException(nameof(jsonElement)); } WIQL.Root? root = JsonSerializer.Deserialize(streamTask.Result, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true }); streamTask.Result.Dispose(); if (root is null || root.WorkItems is null) throw new NullReferenceException(nameof(root)); results = root.WorkItems; return results; } }