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.
162 lines
5.6 KiB
162 lines
5.6 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;
|
|
using Microsoft.VisualBasic;
|
|
|
|
namespace WebJobService;
|
|
|
|
public class Service : RepositoryBaseDapperAsync, IService
|
|
{
|
|
private readonly ILogger _logger;
|
|
private IHttpClientHandler ClientHandler { get; }
|
|
public Service(string cxnName, IHttpClientHandler clientHandler) : base(cxnName)
|
|
{
|
|
//_logger = logger;
|
|
ClientHandler = clientHandler;
|
|
}
|
|
|
|
public void Run()
|
|
{
|
|
Console.WriteLine($"Service Started: {DateTime.Now}.");
|
|
////_logger.LogInformation($"DS Api:-($"Service Started: {DateTime.Now}.");
|
|
GetRecordToProcess().Wait();
|
|
Console.WriteLine($"Service Completed: {DateTime.Now}.");
|
|
////_logger.LogInformation($"DS Api:-($"Service Completed: {DateTime.Now}.");
|
|
}
|
|
|
|
private async Task GetRecordToProcess()
|
|
{
|
|
try
|
|
{
|
|
using (SqlConnection cxn = await OpenCxnAsync())
|
|
{
|
|
IEnumerable<SelectedData> recordsToProcess = await cxn.QueryAsync<SelectedData>("DealerSelection.usp_get_dealer_selection_for_job",
|
|
commandType: CommandType.StoredProcedure);
|
|
var recordList = recordsToProcess.ToList();
|
|
Console.WriteLine($"Records to Process: {recordList.Count}.");
|
|
//_logger.LogInformation($"DS Api:-($"Records to Process: {recordList.Count}.");
|
|
foreach (var item in recordList)
|
|
{
|
|
HitDeleareSelectionApi(item);
|
|
}
|
|
Console.WriteLine($"Records processed sucessfully: {recordList.Count}.");
|
|
}
|
|
|
|
}
|
|
catch (Exception exp)
|
|
{
|
|
throw exp;
|
|
}
|
|
}
|
|
|
|
private async Task HitDeleareSelectionApi(SelectedData item)
|
|
{
|
|
//item.BuId, item.RecorId
|
|
|
|
string jwtToken = GenerateToken();
|
|
string url = string.Format($"https://dealerselection-dev.bajajauto.com/DealerSelection?buId={item.BuId}&recordId={item.RecordId}");
|
|
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);
|
|
|
|
Console.WriteLine($"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;
|
|
var contents = await response.Content.ReadAsStringAsync();
|
|
Console.WriteLine($"Deler Selection URl Response: {contents}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.StackTrace);
|
|
}
|
|
}
|
|
|
|
private string GenerateToken()
|
|
{
|
|
Console.WriteLine($"GenerateToken Started: {DateAndTime.Now}");
|
|
string jwtTokenApiUrl = string.Format("{0}/jwtauth/gettoken", "https://dealerselection-dev.bajajauto.com");
|
|
|
|
var requestBody = new
|
|
{
|
|
client_id = "5ef10013c846424cbe32aaffbdc2d408",
|
|
client_secret = "4ca8d784dc9d4b8db9113d04770c0a7a"
|
|
};
|
|
|
|
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();
|
|
}
|
|
Console.WriteLine($"GenerateToken End: {DateAndTime.Now} - Token :{jwtToken}");
|
|
}
|
|
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Exception at GenerateToken :- {ex.StackTrace}");
|
|
}
|
|
return jwtToken;
|
|
}
|
|
|
|
|
|
class SelectedData
|
|
{
|
|
public int BuId { get; set; }
|
|
public int RecordId { get; set; }
|
|
}
|
|
}
|