55 lines
2.5 KiB
C#
55 lines
2.5 KiB
C#
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<HttpResponseMessage> httpResponseMessageTask = httpClient.GetAsync(string.Concat(basePage, api, "/workItems/", id));
|
|
httpResponseMessageTask.Wait();
|
|
if (!httpResponseMessageTask.Result.IsSuccessStatusCode)
|
|
throw new Exception(httpResponseMessageTask.Result.StatusCode.ToString());
|
|
Task<Stream> streamTask = httpResponseMessageTask.Result.Content.ReadAsStreamAsync();
|
|
streamTask.Wait();
|
|
if (!streamTask.Result.CanRead)
|
|
{
|
|
JsonElement? jsonElement = JsonSerializer.Deserialize<JsonElement>(streamTask.Result);
|
|
if (jsonElement is null)
|
|
throw new NullReferenceException(nameof(jsonElement));
|
|
}
|
|
Root? root = JsonSerializer.Deserialize<Root>(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<HttpResponseMessage> httpResponseMessageTask = httpClient.GetAsync(string.Concat(basePage, api, query));
|
|
httpResponseMessageTask.Wait();
|
|
if (!httpResponseMessageTask.Result.IsSuccessStatusCode)
|
|
throw new Exception(httpResponseMessageTask.Result.StatusCode.ToString());
|
|
Task<Stream> streamTask = httpResponseMessageTask.Result.Content.ReadAsStreamAsync();
|
|
streamTask.Wait();
|
|
if (!streamTask.Result.CanRead)
|
|
{
|
|
JsonElement? jsonElement = JsonSerializer.Deserialize<JsonElement>(streamTask.Result);
|
|
if (jsonElement is null)
|
|
throw new NullReferenceException(nameof(jsonElement));
|
|
}
|
|
WIQL.Root? root = JsonSerializer.Deserialize<WIQL.Root>(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;
|
|
}
|
|
|
|
} |