using CPRNIMS.Domain.UIContracts.Common; using CPRNIMS.Domain.UIContracts.SMTP; using CPRNIMS.Infrastructure.Helper; using CPRNIMS.Infrastructure.Models.Account; using CPRNIMS.Infrastructure.Models.Common; using CPRNIMS.Infrastructure.ViewModel.PR; using CPRNIMS.Infrastructure.ViewModel.SMTP; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.Json; using System.Threading.Tasks; namespace CPRNIMS.Domain.UIServices.SMTP { public class SMTP : ISMTP { private readonly IConfiguration _configuration; private readonly TokenHelper _tokenHelper; private readonly IApiConfigurationService _apiConfigurationService; public SMTP(IConfiguration configuration, TokenHelper tokenHelper, IApiConfigurationService apiConfigurationService) { _configuration = configuration; _tokenHelper = tokenHelper; _apiConfigurationService = apiConfigurationService; } #region SendRequest service public async Task SendPostApiRequest(User user, SMTPCredentialVM viewModel, string apiEndpoint) { var token = await _tokenHelper.GetJwtTokenAsync(user); try { if (string.IsNullOrEmpty(token)) { // Handle token retrieval failure return null; } viewModel.UserId = user.UserId; var jsonContent = JsonSerializer.Serialize(viewModel); var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); using (var httpClient = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token)) { HttpResponseMessage response; response = await httpClient.PostAsync(apiEndpoint, content); if (response.IsSuccessStatusCode) { var responseObject = JsonSerializer.Deserialize(await response.Content.ReadAsStringAsync()); viewModel.Message = responseObject.message; viewModel.StatusResponse = responseObject.statusResponse; return viewModel; } else { // Handle API request failure return null; } } } catch (Exception ex) { ex.ToString(); throw; } } public async Task> SendGetApiRequest(User user, SMTPCredentialVM viewModel, string apiEndpoint) { var token = await _tokenHelper.GetJwtTokenAsync(user); try { if (string.IsNullOrEmpty(token)) { // Handle token retrieval failure return null; } viewModel.UserId = user.UserId; var jsonContent = JsonSerializer.Serialize(viewModel); var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); using (var httpClient = _apiConfigurationService.CreateHttpClientWithDefaultHeaders(token)) { HttpResponseMessage response; response = await httpClient.PostAsync(apiEndpoint, content); if (response.IsSuccessStatusCode) { var jsonResponse = await response.Content.ReadAsStringAsync(); var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; var myArtWork = JsonSerializer.Deserialize>(jsonResponse, options); return myArtWork; } else { // Handle API request failure return null; } } } catch (Exception ex) { throw; } } #endregion public async Task> GetAllSmtp(User user, SMTPCredentialVM viewModel) { return await SendGetApiRequest(user, viewModel, _configuration["LLI:NonInvent:SMTPMgmt:GetAllSmtp"]); } public async Task> GetMySmtp(User user, SMTPCredentialVM viewModel) { return await SendGetApiRequest(user, viewModel, _configuration["LLI:NonInvent:SMTPMgmt:GetMySmtp"]); } public async Task PostPutSmtp(User user, SMTPCredentialVM viewModel) { return await SendPostApiRequest(user, viewModel, _configuration["LLI:NonInvent:SMTPMgmt:PostPutSmtp"]); } } }