Retail_manager/RMWPFInterfaceLibrary/Api/APIHelper.cs
s459315 57d2ab60a7 Created Sales View and Database prodecures for displaying items.
Created and binded a View, created logic behind Cart Product Model
2022-07-23 17:09:13 +02:00

93 lines
3.1 KiB
C#

using RMWPFInterfaceLibrary.Models;
using RMWPFUserInterface.Models;
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;
namespace RMWPFInterfaceLibrary.Api
{
public class APIHelper : IAPIHelper
{
private HttpClient _apiClient;
private ILoggedInUserModel _loggedInUser;
public APIHelper(ILoggedInUserModel loggedInUser)
{
InitializeClient();
_loggedInUser = loggedInUser;
}
public HttpClient ApiClient
{
get
{
return _apiClient;
}
}
private void InitializeClient()
{
string api = ConfigurationManager.AppSettings["api"];
_apiClient = new HttpClient();
_apiClient.BaseAddress = new Uri(api);
_apiClient.DefaultRequestHeaders.Accept.Clear();
_apiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
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),
});
using (HttpResponseMessage response = await _apiClient.PostAsync("/Token", data))
{
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsAsync<AuthenticatedUser>();
return result;
}
else
{
throw new Exception(response.ReasonPhrase);
}
}
}
public async Task GetLogedInUserInfo(string token)
{
_apiClient.DefaultRequestHeaders.Clear();
_apiClient.DefaultRequestHeaders.Accept.Clear();
_apiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
_apiClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");
using (HttpResponseMessage response = await _apiClient.GetAsync("api/User"))
{
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);
}
}
}
}
}