Added call for lign API

This commit is contained in:
ashutosh.nehete 2025-07-26 08:56:10 +05:30
parent c45d9238c8
commit 0aefcb97de
2 changed files with 65 additions and 12 deletions

View File

@ -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<bool> SendDataAsync<T>(T data)
public async Task<bool> SendDataAsync<T>(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<string> LoginAsync<T>(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;
}
}
}
}

View File

@ -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();
}
@ -46,9 +50,9 @@ namespace Marco.Pms.UtilityApplication
List<CreateContactDto> createContactDto = null;
List<Contacts> 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);
//}
}
}
}
}