From 0aefcb97de61c4d1adcda9ba523e1fdacf33ff9b Mon Sep 17 00:00:00 2001 From: "ashutosh.nehete" Date: Sat, 26 Jul 2025 08:56:10 +0530 Subject: [PATCH] Added call for lign API --- Marco.Pms.UtilityApplication/ApiService.cs | 42 ++++++++++++++++++- .../MainWindow.xaml.cs | 35 +++++++++++----- 2 files changed, 65 insertions(+), 12 deletions(-) diff --git a/Marco.Pms.UtilityApplication/ApiService.cs b/Marco.Pms.UtilityApplication/ApiService.cs index 362f8b8..9da370f 100644 --- a/Marco.Pms.UtilityApplication/ApiService.cs +++ b/Marco.Pms.UtilityApplication/ApiService.cs @@ -1,5 +1,7 @@ using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System.Net.Http; +using System.Net.Http.Headers; using System.Text; namespace Marco.Pms.UtilityApplication @@ -15,13 +17,15 @@ namespace Marco.Pms.UtilityApplication _apiUrl = apiUrl; } - public async Task SendDataAsync(T data) + public async Task SendDataAsync(T data, string token) { try { var jsonContent = JsonConvert.SerializeObject(data); var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); + _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); + HttpResponseMessage response = await _httpClient.PostAsync(_apiUrl, content); response.EnsureSuccessStatusCode(); // Throws an exception if the HTTP response status is an error code. @@ -49,5 +53,41 @@ namespace Marco.Pms.UtilityApplication return false; } } + public async Task LoginAsync(T data) + { + try + { + var jsonContent = JsonConvert.SerializeObject(data); + var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); + + HttpResponseMessage response = await _httpClient.PostAsync(_apiUrl, content); + + response.EnsureSuccessStatusCode(); // Throws an exception if the HTTP response status is an error code. + + string responseBody = await response.Content.ReadAsStringAsync(); + // You can parse the responseBody if your API returns something useful + Console.WriteLine($"API Response: {responseBody}"); + var jObject = JObject.Parse(responseBody); + + string jwt = jObject["data"]?["token"]?.ToString() ?? string.Empty; ; + return jwt; + } + catch (HttpRequestException ex) + { + Console.WriteLine($"API request error: {ex.Message}"); + // Log or handle the error appropriately in your UI + return string.Empty; + } + catch (JsonException ex) + { + Console.WriteLine($"JSON serialization error: {ex.Message}"); + return string.Empty; + } + catch (Exception ex) + { + Console.WriteLine($"An unexpected error occurred: {ex.Message}"); + return string.Empty; + } + } } } diff --git a/Marco.Pms.UtilityApplication/MainWindow.xaml.cs b/Marco.Pms.UtilityApplication/MainWindow.xaml.cs index f39ab11..0105fe1 100644 --- a/Marco.Pms.UtilityApplication/MainWindow.xaml.cs +++ b/Marco.Pms.UtilityApplication/MainWindow.xaml.cs @@ -1,4 +1,5 @@ -using Marco.Pms.Model.Dtos.Directory; +using Marco.Pms.Model.Dtos.Authentication; +using Marco.Pms.Model.Dtos.Directory; using Microsoft.Win32; using System.Windows; @@ -12,12 +13,15 @@ namespace Marco.Pms.UtilityApplication //DirectoryHelper helper = new DirectoryHelper(); private readonly DirectoryHelper _directoryHelper; private readonly ApiService _apiService; + private readonly ApiService _loginApiService; // Replace with your actual API endpoint - private const string ApiEndpoint = "https://your-api-url.com/api/data"; + private const string DirectoryApiEndpoint = "http://localhost:5032/api/directory/create/list"; + private const string LoginApiEndpoint = "http://localhost:5032/api/auth/login"; public MainWindow() { InitializeComponent(); - _apiService = new ApiService(ApiEndpoint); + _apiService = new ApiService(DirectoryApiEndpoint); + _loginApiService = new ApiService(LoginApiEndpoint); _directoryHelper = new DirectoryHelper(); } @@ -36,7 +40,7 @@ namespace Marco.Pms.UtilityApplication } } - + private async void btnSubmit_Click(object sender, RoutedEventArgs e) { List contactsData = null; @@ -46,9 +50,9 @@ namespace Marco.Pms.UtilityApplication List createContactDto = null; List failedContacts = null; - (contactsData, categoryMasters, tagsMaster, projectMaster) = await _directoryHelper.ReadExcelData(txtFilePath.Text); + (contactsData, categoryMasters, tagsMaster, projectMaster) = await _directoryHelper.ReadExcelData(txtFilePath.Text); - ( createContactDto, failedContacts) = await _directoryHelper.GenerateCreateContactDto(contactsData, categoryMasters, tagsMaster, projectMaster); + (createContactDto, failedContacts) = await _directoryHelper.GenerateCreateContactDto(contactsData, categoryMasters, tagsMaster, projectMaster); // Check if there's data to display if (failedContacts != null && failedContacts.Any()) @@ -65,11 +69,20 @@ namespace Marco.Pms.UtilityApplication // StatusTextBlock.Text = "No contacts to display."; } - foreach (Contacts contact in contactsData) + LoginDto loginDto = new LoginDto { - //.Text = $"Sending {apiDataList.Count} records to API..."; - bool success = await _apiService.SendDataAsync(contact); - } + Username = "admin@marcoaiot.com", + Password = "User@123" + }; + + string jwt = await _loginApiService.LoginAsync(loginDto); + + bool success = await _apiService.SendDataAsync(createContactDto, jwt); + //foreach (Contacts contact in contactsData) + //{ + // //.Text = $"Sending {apiDataList.Count} records to API..."; + // bool success = await _apiService.SendDataAsync(contact); + //} } - } + } } \ No newline at end of file