22 changed files with 414 additions and 159 deletions
-
4Source/DealerSelection.Api.Models/WebEngageEvent.cs
-
18Source/DealerSelection.Api/AssignDealerApi.cs
-
14Source/DealerSelection.Api/CCAvenueApi.cs
-
6Source/DealerSelection.Api/MulesoftApi.cs
-
14Source/DealerSelection.Api/YellowAIApi.cs
-
8Source/DealerSelection.Common/CommonBaseClass/ExceptionFilterBase.cs
-
29Source/DealerSelection.Common/CommonBaseClass/StartupBase.cs
-
4Source/DealerSelection.DependencyInjection/ApiRegistry.cs
-
5Source/DealerSelection.DependencyInjection/InfrastructureRegistry.cs
-
4Source/DealerSelection.Infrastructure/CustomerDetail/Repository.cs
-
2Source/DealerSelection.Infrastructure/Mulesoft/MulesoftCustomerInfoDto.cs
-
4Source/DealerSelection.WebApi/Controllers/BatchJobController.cs
-
8Source/DealerSelection.WebApi/appsettings.json
-
6Source/WebJobService/IRepository.cs
-
81Source/WebJobService/Program.cs
-
33Source/WebJobService/Repository.cs
-
7Source/WebJobService/SelectedData.cs
-
114Source/WebJobService/Service.cs
-
140Source/WebJobService/ServiceBackUp.cs
-
20Source/WebJobService/WebJobService.csproj
-
8Source/WebJobService/appsettings.Development.json
-
14Source/WebJobService/appsettings.json
@ -0,0 +1,6 @@ |
|||||
|
namespace WebJobService; |
||||
|
|
||||
|
public interface IRepository |
||||
|
{ |
||||
|
Task<List<SelectedData>> GetDealerSelectionDataForJobAsync(); |
||||
|
} |
@ -1,34 +1,87 @@ |
|||||
using DealerSelection.Common.HttpClient; |
using DealerSelection.Common.HttpClient; |
||||
using DealerSelection.Common.Interfaces.HttpClient; |
using DealerSelection.Common.Interfaces.HttpClient; |
||||
using Lamar; |
using Lamar; |
||||
|
using Microsoft.ApplicationInsights.Extensibility; |
||||
|
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel; |
||||
|
using Microsoft.AspNetCore.Builder; |
||||
|
using Microsoft.AspNetCore.Hosting; |
||||
|
using Microsoft.Extensions.Configuration; |
||||
using Microsoft.Extensions.DependencyInjection; |
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Microsoft.Extensions.Hosting; |
||||
using Microsoft.Extensions.Logging; |
using Microsoft.Extensions.Logging; |
||||
|
using Microsoft.Extensions.Logging.ApplicationInsights; |
||||
|
|
||||
namespace WebJobService; |
namespace WebJobService; |
||||
|
|
||||
|
|
||||
public class Program : ServiceRegistry |
public class Program : ServiceRegistry |
||||
{ |
{ |
||||
private IHttpClientHandler HttpClientHandler { get; set; } |
|
||||
private ILogger<Program> logger; |
|
||||
private IServiceProvider serviceProvider; |
|
||||
|
|
||||
static void Main() |
static void Main() |
||||
{ |
{ |
||||
var container=new Container(x=> |
|
||||
{ |
|
||||
x.AddSingleton<IService, Service>(); |
|
||||
x.AddSingleton<IHttpClientHandler, MyHttpClientHandler>(); |
|
||||
x.For<IService>().Use<Service>().Ctor<string>("cxnName").Is("BOOKING").Singleton(); |
|
||||
}); |
|
||||
//IHttpClientHandler clientHandler = container.GetInstance<IHttpClientHandler>();
|
|
||||
//ILogger logger = container.GetInstance<ILogger>();
|
|
||||
IService service = container.GetInstance<IService>(); |
|
||||
// IHttpClientHandler clientHandler = container.GetInstance<IHttpClientHandler>();
|
|
||||
//ILogger logger = container.GetInstance<ILogger>();
|
|
||||
|
var registry = new ServiceRegistry(); |
||||
|
registry.AddSingleton<IHttpClientHandler, MyHttpClientHandler>(); |
||||
|
registry.AddSingleton<IRepository, Repository>(); |
||||
|
|
||||
|
var builder = new ConfigurationBuilder().AddJsonFile($"appsettings.json", true, true); |
||||
|
var config = builder.Build(); |
||||
|
var aiUrl = config["AZURE_LOGGING"]; |
||||
|
|
||||
|
registry.AddLogging(x => x.AddApplicationInsights( |
||||
|
configureTelemetryConfiguration: (config) => config.ConnectionString = |
||||
|
aiUrl, configureApplicationInsightsLoggerOptions: (options) => { } |
||||
|
).AddFilter<ApplicationInsightsLoggerProvider>("traces", LogLevel.Trace)); |
||||
|
|
||||
|
|
||||
|
|
||||
|
registry.For<IRepository>() |
||||
|
.Use<Repository>() |
||||
|
.Ctor<string>().Is("BOOKING") |
||||
|
.Transient(); |
||||
|
|
||||
|
registry.For<IService>().Use<Service>().Singleton(); |
||||
|
|
||||
|
//IServiceCollection services1 = new ServiceCollection();
|
||||
|
//var CHANNEL1 = new ServerTelemetryChannel();
|
||||
|
//services1.Configure<TelemetryConfiguration>((config) =>
|
||||
|
//{
|
||||
|
// config.TelemetryChannel = CHANNEL1;
|
||||
|
//});
|
||||
|
|
||||
|
//services1.AddLogging(builder => { builder.AddApplicationInsights(aiUrl); });
|
||||
|
//var provider=services1.BuildServiceProvider();
|
||||
|
|
||||
|
//var logger=provider.GetService<ILogger<Program>>();
|
||||
|
//logger.LogInformation("Its a Test");
|
||||
|
|
||||
|
//Host.CreateDefaultBuilder(args)
|
||||
|
// .ConfigureWebHostDefaults(webBuilder =>
|
||||
|
// {
|
||||
|
// webBuilder.UseStartup();
|
||||
|
// })
|
||||
|
// .UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
|
||||
|
// .ReadFrom.Configuration(hostingContext.Configuration)
|
||||
|
// .WriteTo.ApplicationInsights(new TelemetryConfiguration { InstrumentationKey = "xxxxxxxxx" }, TelemetryConverter.Traces)
|
||||
|
// );
|
||||
|
|
||||
|
var container = new Container(registry); |
||||
|
|
||||
|
|
||||
|
IService service = container.GetInstance<IService>(); |
||||
service.Run(); |
service.Run(); |
||||
|
// Handling finalizing when process is ended
|
||||
|
|
||||
|
|
||||
|
} |
||||
|
|
||||
|
private void AzureLogging(WebApplicationBuilder builder) |
||||
|
{ |
||||
|
builder.Logging.AddApplicationInsights( |
||||
|
configureTelemetryConfiguration: (config) => config.ConnectionString = |
||||
|
builder.Configuration.GetConnectionString("AZURE_LOGGING"), |
||||
|
configureApplicationInsightsLoggerOptions: (options) => { } |
||||
|
); |
||||
|
builder.Logging.AddFilter<ApplicationInsightsLoggerProvider>("traces", LogLevel.Trace); |
||||
} |
} |
||||
|
|
||||
|
|
||||
|
@ -0,0 +1,33 @@ |
|||||
|
using Dapper; |
||||
|
using DealerSelection.Common.Data.Dapper; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
using System.Data; |
||||
|
using System.Data.SqlClient; |
||||
|
|
||||
|
namespace WebJobService; |
||||
|
|
||||
|
public class Repository : RepositoryBaseDapperAsync, IRepository |
||||
|
{ |
||||
|
private readonly ILogger _logger; |
||||
|
public Repository(string cxnName, ILogger<Repository> logger) : base(cxnName) |
||||
|
{ |
||||
|
_logger = logger; |
||||
|
} |
||||
|
|
||||
|
public async Task<List<SelectedData>> GetDealerSelectionDataForJobAsync() |
||||
|
{ |
||||
|
try |
||||
|
{ |
||||
|
using SqlConnection cxn = await OpenCxnAsync(); |
||||
|
IEnumerable<SelectedData> recordsToProcess = await cxn.QueryAsync<SelectedData>("DealerSelection.usp_get_dealer_selection_for_job", |
||||
|
commandType: CommandType.StoredProcedure); |
||||
|
return recordsToProcess.ToList(); |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
_logger.LogError($"WJ:-Repo:-Error at Repository:GetDealerSelectionDataForJobAsync.", ex.StackTrace); |
||||
|
throw; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,7 @@ |
|||||
|
namespace WebJobService; |
||||
|
|
||||
|
public class SelectedData |
||||
|
{ |
||||
|
public int BuId { get; set; } |
||||
|
public int RecordId { get; set; } |
||||
|
} |
@ -0,0 +1,140 @@ |
|||||
|
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; |
||||
|
using Microsoft.Extensions.Configuration; |
||||
|
|
||||
|
namespace WebJobService; |
||||
|
|
||||
|
public class ServiceBackUp : RepositoryBaseDapperAsync, IService |
||||
|
{ |
||||
|
private ILogger _logger; |
||||
|
|
||||
|
private IHttpClientHandler ClientHandler { get; } |
||||
|
public ServiceBackUp(string cxnName, IHttpClientHandler clientHandler,ILogger logger) : base(cxnName) |
||||
|
{ |
||||
|
_logger = logger; |
||||
|
ClientHandler = clientHandler; |
||||
|
} |
||||
|
|
||||
|
public void Run() |
||||
|
{ |
||||
|
|
||||
|
Console.WriteLine($"Service Started: {DateTime.Now}."); |
||||
|
_logger.LogInformation($"Service Started: {DateTime.Now}."); |
||||
|
GetRecordToProcess().Wait(); |
||||
|
Console.WriteLine($"Service Completed: {DateTime.Now}."); |
||||
|
_logger.LogInformation($"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) |
||||
|
{ |
||||
|
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 |
||||
|
{ |
||||
|
|
||||
|
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(string baseApiUrl, string client_Id, string client_secret) |
||||
|
{ |
||||
|
Console.WriteLine($"GenerateToken Started: {DateAndTime.Now}"); |
||||
|
string jwtTokenApiUrl = string.Format($"{baseApiUrl}/jwtauth/gettoken"); |
||||
|
|
||||
|
var requestBody = new |
||||
|
{ |
||||
|
client_id = client_Id, |
||||
|
client_secret = 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(); |
||||
|
} |
||||
|
Console.WriteLine($"GenerateToken End: {DateAndTime.Now} - Token :{jwtToken}"); |
||||
|
} |
||||
|
|
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
Console.WriteLine($"Exception at GenerateToken :- {ex.StackTrace}"); |
||||
|
} |
||||
|
return jwtToken; |
||||
|
} |
||||
|
|
||||
|
} |
@ -0,0 +1,8 @@ |
|||||
|
{ |
||||
|
"Logging": { |
||||
|
"LogLevel": { |
||||
|
"Default": "Information", |
||||
|
"Microsoft.AspNetCore": "Warning" |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,14 @@ |
|||||
|
{ |
||||
|
|
||||
|
"Logging": { |
||||
|
"LogLevel": { |
||||
|
"Default": "Information", |
||||
|
"Microsoft.AspNetCore": "Warning" |
||||
|
} |
||||
|
}, |
||||
|
"BaseApiUrl": "https://dealerselection-dev.bajajauto.com", |
||||
|
"ClientId": "5ef10013c846424cbe32aaffbdc2d408", |
||||
|
"ClientSecret": "4ca8d784dc9d4b8db9113d04770c0a7a", |
||||
|
"AZURE_LOGGING": "InstrumentationKey=2fead737-737a-4241-9e66-dfc5391583ba;IngestionEndpoint=https://centralindia-0.in.applicationinsights.azure.com/;LiveEndpoint=https://centralindia.livediagnostics.monitor.azure.com/" |
||||
|
|
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue