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.
160 lines
5.3 KiB
160 lines
5.3 KiB
using Dapper;
|
|
using DealerSelection.Common.Data.Dapper;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json.Linq;
|
|
using Newtonsoft.Json;
|
|
using System.Configuration;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Net.Http.Headers;
|
|
using DealerSelection.Common.Interfaces.HttpClient;
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
using System.Net.Http;
|
|
|
|
namespace WebJobService;
|
|
|
|
public class Service : RepositoryBaseDapperAsync, IService
|
|
{
|
|
private readonly ILogger _logger;
|
|
private IHttpClientHandler ClientHandler { get; }
|
|
private string connectionString = Convert.ToString(ConfigurationManager.ConnectionStrings["NoDealerBooking"]);
|
|
public Service(string cxnName, IHttpClientHandler clientHandler) : base(cxnName)
|
|
{
|
|
//_logger = logger;
|
|
ClientHandler = clientHandler;
|
|
}
|
|
|
|
public void Run()
|
|
{
|
|
////_logger.LogInformation($"Service Started: {DateTime.Now}.");
|
|
GetRecordToProcess().Wait();
|
|
////_logger.LogInformation($"Service Completed: {DateTime.Now}.");
|
|
}
|
|
|
|
private async Task GetRecordToProcess()
|
|
{
|
|
try
|
|
{
|
|
using (SqlConnection cxn = await OpenCxnAsync())
|
|
{
|
|
IEnumerable<SelectedData> recordsToProcess = await cxn.QueryAsync<SelectedData>("usp_get_dealer_selection_for_job",
|
|
commandType: CommandType.StoredProcedure);
|
|
var recordList = recordsToProcess.ToList();
|
|
//_logger.LogInformation($"Records to Process: {recordList.Count}.");
|
|
foreach (var item in recordList)
|
|
{
|
|
HitDeleareSelectionApi(item);
|
|
}
|
|
//_logger.LogInformation($"Records processed sucessfully: {recordList.Count}.");
|
|
}
|
|
|
|
}
|
|
catch (Exception exp)
|
|
{
|
|
throw exp;
|
|
}
|
|
//sqlConnection1.Close();
|
|
}
|
|
|
|
private async Task HitDeleareSelectionApi(SelectedData item)
|
|
{
|
|
//item.BuId, item.RecorId
|
|
|
|
string jwtToken = GenerateToken();
|
|
string url = $"https://localhost:7122/DealerSelection/DealerSelection?buId={item.BuId}&recordId={item.RecordId}";
|
|
//string resultJson = string.Empty;
|
|
|
|
//string modelDetailsApi = string.Format("https://booking-preprod.bajajauto.com/DealerSelection/DealerSelection?buId=1&recordId=123");
|
|
try
|
|
{
|
|
//using (var webClient = new WebClient { Encoding = Encoding.UTF8 })
|
|
//{
|
|
// webClient.Headers.Add("Content-Type", "application/json");
|
|
// webClient.Headers.Add("Authorization", "Bearer " + jwtToken);
|
|
// resultJson = webClient.DownloadString(modelDetailsApi);
|
|
//}
|
|
//var resultObject = JObject.Parse(resultJson);
|
|
//if (resultObject.Value<bool>("IsRequestSuccessfull"))
|
|
//{
|
|
// var response = (JObject)resultObject.Value<JArray>("Data")[0];
|
|
// Console.WriteLine(response.ToString());
|
|
//}
|
|
|
|
|
|
//var data = "{\"buId\": \"" + item.BuId + "\"}";
|
|
|
|
//var data = new
|
|
//{
|
|
// item.BuId,
|
|
// item.RecorId,
|
|
//};
|
|
|
|
//string requestJson = JsonConvert.SerializeObject(data);
|
|
|
|
|
|
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;
|
|
var contents = await response.Content.ReadAsStringAsync();
|
|
Console.WriteLine(contents);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.StackTrace);
|
|
}
|
|
}
|
|
|
|
private string GenerateToken()
|
|
{
|
|
string jwtTokenApiUrl = string.Format("{0}/JWTAuth/GetAuthToken", "https://localhost:7122");
|
|
|
|
var requestBody = new
|
|
{
|
|
clientId = "ee12e3d9cb9a4dc6af3599934cc4152b",
|
|
secretId = "92a2bede7b414cb19861efa3cc3a8c0d",
|
|
buId = 1
|
|
};
|
|
|
|
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();
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
}
|
|
return jwtToken;
|
|
}
|
|
|
|
|
|
class SelectedData
|
|
{
|
|
public int BuId { get; set; }
|
|
public int RecordId { get; set; }
|
|
}
|
|
}
|