You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
4.2 KiB
124 lines
4.2 KiB
using DealerSelection.Common.Interfaces.HttpClient;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.VisualBasic;
|
|
using Newtonsoft.Json;
|
|
using System.Data.SqlClient;
|
|
using System.Net;
|
|
using System.Net.Http.Headers;
|
|
using System.Text;
|
|
|
|
namespace WebJobService;
|
|
|
|
public class Service : IService
|
|
{
|
|
private ILogger _logger;
|
|
private IHttpClientHandler ClientHandler { get; }
|
|
private IRepository Repository { get; }
|
|
public Service(IHttpClientHandler clientHandler, ILogger<Service> logger, IRepository repository)
|
|
{
|
|
_logger = logger;
|
|
ClientHandler = clientHandler;
|
|
Repository = repository;
|
|
}
|
|
|
|
public void Run()
|
|
{
|
|
_logger.LogError($"WJ:-Service Started");
|
|
// GetRecordToProcess().Wait();
|
|
_logger.LogInformation($"WJ:-Service Completed");
|
|
}
|
|
|
|
private async Task GetRecordToProcess()
|
|
{
|
|
try
|
|
{
|
|
var recordList = await Repository.GetDealerSelectionDataForJobAsync();// recordsToProcess.ToList();
|
|
_logger.LogInformation($"WJ:-Records to Process: {recordList.Count}");
|
|
foreach (var item in recordList)
|
|
{
|
|
await HitDeleareSelectionApi(item);
|
|
}
|
|
_logger.LogInformation($"WJ:-Records processed sucessfully: {recordList.Count}.");
|
|
}
|
|
catch (Exception exp)
|
|
{
|
|
_logger.LogError($"WJ:-Error at GetRecordToProcess {exp.StackTrace}");
|
|
throw ;
|
|
}
|
|
}
|
|
|
|
private async Task HitDeleareSelectionApi(SelectedData item)
|
|
{
|
|
var builder = new ConfigurationBuilder().AddJsonFile($"appsettings.json", true, true);
|
|
var config = builder.Build();
|
|
var baseApiUrl = config["BaseApiUrl"];
|
|
string jwtToken = GenerateToken(baseApiUrl, config["ClientId"], config["ClientSecret"]);
|
|
|
|
string url = string.Format($"{baseApiUrl}/DealerSelection?buId={item.BuId}&recordId={item.RecordId}");
|
|
try
|
|
{
|
|
|
|
_logger.LogInformation($"WJ:-Deler Selection URl Request: {url}.");
|
|
HttpClient request = ClientHandler.GetHttpClient();
|
|
Uri requestUri = new(url);
|
|
var httpContent = new MultipartFormDataContent();
|
|
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
|
request.DefaultRequestHeaders.Add("Authorization", "Bearer " + jwtToken);
|
|
HttpResponseMessage response = request.PostAsync(url, null).Result;
|
|
_logger.LogInformation($"WJ:-Deler Selection URl ResponseStatus: {response}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError($"WJ:-Error at GetRecordToProcess:- {ex.StackTrace}");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
private string GenerateToken(string baseApiUrl, string client_Id, string client_secret)
|
|
{
|
|
_logger.LogInformation($"WJ:-GenerateToken Started.");
|
|
string jwtTokenApiUrl = string.Format($"{baseApiUrl}/jwtauth/gettoken");
|
|
|
|
var requestBody = new
|
|
{
|
|
client_id = client_Id,
|
|
client_secret
|
|
};
|
|
|
|
string requestJson = JsonConvert.SerializeObject(requestBody);
|
|
byte[] bodyBytes = Encoding.UTF8.GetBytes(requestJson);
|
|
|
|
HttpWebRequest request = WebRequest.Create(jwtTokenApiUrl) as HttpWebRequest;
|
|
request.Method = "POST";
|
|
request.ContentType = "application/json";
|
|
request.ContentLength = bodyBytes.Length;
|
|
|
|
string jwtToken = string.Empty;
|
|
try
|
|
{
|
|
using (var dataStream = request.GetRequestStream())
|
|
{
|
|
dataStream.Write(bodyBytes, 0, bodyBytes.Length);
|
|
dataStream.Close();
|
|
}
|
|
|
|
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
|
|
Stream responseStream = response.GetResponseStream();
|
|
|
|
using (StreamReader reader = new StreamReader(responseStream))
|
|
{
|
|
jwtToken = reader.ReadToEnd();
|
|
}
|
|
_logger.LogInformation($"WJ:-GenerateToken End: {DateAndTime.Now} - Token :{jwtToken}");
|
|
}
|
|
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError($"WJ:-Error at GenerateToken:- {ex.StackTrace}");
|
|
throw;
|
|
}
|
|
return jwtToken;
|
|
}
|
|
|
|
}
|