2022-07-22 16:45:33 +02:00
|
|
|
|
using RMWPFInterfaceLibrary.Models;
|
|
|
|
|
using RMWPFUserInterface.Models;
|
2022-07-08 17:31:58 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Configuration;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2022-07-22 16:45:33 +02:00
|
|
|
|
namespace RMWPFInterfaceLibrary.Api
|
2022-07-08 17:31:58 +02:00
|
|
|
|
{
|
|
|
|
|
public class APIHelper : IAPIHelper
|
|
|
|
|
{
|
2022-07-23 17:09:13 +02:00
|
|
|
|
private HttpClient _apiClient;
|
2022-07-22 16:45:33 +02:00
|
|
|
|
private ILoggedInUserModel _loggedInUser;
|
|
|
|
|
public APIHelper(ILoggedInUserModel loggedInUser)
|
2022-07-08 17:31:58 +02:00
|
|
|
|
{
|
|
|
|
|
InitializeClient();
|
2022-07-22 16:45:33 +02:00
|
|
|
|
_loggedInUser = loggedInUser;
|
2022-07-08 17:31:58 +02:00
|
|
|
|
}
|
2022-07-23 17:09:13 +02:00
|
|
|
|
|
|
|
|
|
public HttpClient ApiClient
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _apiClient;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-08 17:31:58 +02:00
|
|
|
|
private void InitializeClient()
|
|
|
|
|
{
|
|
|
|
|
string api = ConfigurationManager.AppSettings["api"];
|
2022-09-01 15:14:25 +02:00
|
|
|
|
|
2022-07-23 17:09:13 +02:00
|
|
|
|
_apiClient = new HttpClient();
|
|
|
|
|
_apiClient.BaseAddress = new Uri(api);
|
|
|
|
|
_apiClient.DefaultRequestHeaders.Accept.Clear();
|
|
|
|
|
_apiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
2022-07-08 17:31:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<AuthenticatedUser> Authenticate(string username, string password)
|
|
|
|
|
{
|
|
|
|
|
var data = new FormUrlEncodedContent(new[]
|
|
|
|
|
{
|
|
|
|
|
new KeyValuePair<string, string>("grant_type", "password"),
|
|
|
|
|
new KeyValuePair<string, string>("username", username),
|
|
|
|
|
new KeyValuePair<string, string>("password", password),
|
|
|
|
|
});
|
|
|
|
|
|
2022-07-23 17:09:13 +02:00
|
|
|
|
using (HttpResponseMessage response = await _apiClient.PostAsync("/Token", data))
|
2022-07-08 17:31:58 +02:00
|
|
|
|
{
|
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
var result = await response.Content.ReadAsAsync<AuthenticatedUser>();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(response.ReasonPhrase);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-22 16:45:33 +02:00
|
|
|
|
|
2022-07-30 14:21:25 +02:00
|
|
|
|
public void LogOffUser()
|
|
|
|
|
{
|
|
|
|
|
_apiClient.DefaultRequestHeaders.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 16:45:33 +02:00
|
|
|
|
public async Task GetLogedInUserInfo(string token)
|
|
|
|
|
{
|
2022-07-23 17:09:13 +02:00
|
|
|
|
_apiClient.DefaultRequestHeaders.Clear();
|
|
|
|
|
_apiClient.DefaultRequestHeaders.Accept.Clear();
|
|
|
|
|
_apiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
|
|
|
|
_apiClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");
|
2022-07-22 16:45:33 +02:00
|
|
|
|
|
2022-07-23 17:09:13 +02:00
|
|
|
|
using (HttpResponseMessage response = await _apiClient.GetAsync("api/User"))
|
2022-07-22 16:45:33 +02:00
|
|
|
|
{
|
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
var result = await response.Content.ReadAsAsync<LoggedInUserModel>();
|
|
|
|
|
_loggedInUser.CreatedDate = result.CreatedDate;
|
|
|
|
|
_loggedInUser.EmailAddress = result.EmailAddress;
|
|
|
|
|
_loggedInUser.FirstName = result.FirstName;
|
|
|
|
|
_loggedInUser.Id = result.Id;
|
|
|
|
|
_loggedInUser.LastName = result.LastName;
|
|
|
|
|
_loggedInUser.Token = token;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(response.ReasonPhrase);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-08 17:31:58 +02:00
|
|
|
|
}
|
|
|
|
|
}
|