I am developing a GUI application that interacts with a control system's REST-based API via an HttpClient, but in trying to encapsulate the HttpClient and its related functions (GET/POST/PATCH) in a separate class, I'm finding that the GUI form cannot get any of the values or results that were supposed to be set or returned from the separate class's asynchronous methods.
If I instantiate the HttpClient in the GUI form's class and place its related functions in there too, the form is able to get the values, but this results in redundant code in every form needing to make calls to the API, not to mention wrecking any semblance of MVC/MVVM.
Is there any way to get the result from these asynchronous methods that were encapsulated in another class?
My partial code is as follows:
Encapsulated HttpClient Class
public class ControlClient
{
public static HttpClient Client = new HttpClient()
{
BaseAddress = new Uri($"{ipAddress}:{port}/api/v1/")
};
private string sessionID = "";
public string SessionID {
get { return sessionID; }
set { sessionID = value; }
}
public ControlClient()
{
RunAsync().ConfigureAwait(false);
}
public async Task RunAsync()
{
Client.DefaultRequestHeaders.Accept.Clear();
Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var loginBody = new StringContent(LOGIN_BODY, Encoding.UTF8, "application/json");
using (HttpResponseMessage loginResponse = await Client.PostAsync("authentication/login", loginBody))
{
var loginJSon = await loginResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
var loginJObject = JObject.Parse(loginJSon).ConfigureAwait(false);
SessionID = loginJObject.GetValue("UserID").ToString();
}
}
public async Task<string> Get(string requestUri, Dictionary<string, string> headerValues)
{
using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, requestUri))
{
headerValues.ToList().ForEach(x => requestMessage.Headers.Add(x.Key, x.Value));
var responseMessage = await Client.SendAsync(requestMessage).ConfigureAwait(false);
return await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
}
}
}
GUI Form Class
public partial class Form1: Form
{
private ControlClient client = new ControlClient();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show(client.SessionID);
}
private void button1_Click(object sender, EventArgs e)
{
var users = JsonConvert.DeserializeObject<List<UserDto>>(client.Get("config/user?includedProperties=All", headers).Result);
}
}