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.
86 lines
3.6 KiB
86 lines
3.6 KiB
using DealerSelection.Api.Models;
|
|
using DealerSelection.Common.Configuration;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Net;
|
|
using System.Net.Http.Headers;
|
|
using System.Text;
|
|
using DealerSelection.Common.Interfaces.HttpClient;
|
|
using DealerSelection.Common.Logging;
|
|
|
|
namespace DealerSelection.Api.CommonUtil
|
|
{
|
|
|
|
/// <summary>
|
|
/// Summary description for TestRideExcellon
|
|
/// </summary>
|
|
public static class MuleSoftIntegration
|
|
{
|
|
public static string ClientId = ConfigurationHelper.GetSetting<string>("muleSoftAPIClientID", true);
|
|
public static string ClientSecret = ConfigurationHelper.GetSetting<string>("muleSoftAIPClientSecret", true);
|
|
public static string TokenGenerateUrl = ConfigurationHelper.GetSetting<string>("muleSoftGenerateTokenURL", true);
|
|
|
|
public static async Task<MulesoftToken> generateToken(IHttpClientHandler clientHandler)
|
|
{
|
|
AzureLogHelper.LogMessage("Api:- generateToken Started :- " + DateTime.Now);
|
|
MulesoftToken token = new MulesoftToken();
|
|
try
|
|
{
|
|
var url = TokenGenerateUrl + "/token";
|
|
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
|
|
HttpClient client = clientHandler.GetHttpClient();
|
|
Uri requestUri = new Uri(url);
|
|
|
|
client.DefaultRequestHeaders.Add("client_id", ClientId);
|
|
client.DefaultRequestHeaders.Add("client_secret", ClientSecret);
|
|
client.DefaultRequestHeaders.Add("grant_type", "CLIENT_CREDENTIALS");
|
|
client.DefaultRequestHeaders.Add("cache-control", "no-cache");
|
|
|
|
var response = await client.PostAsync(requestUri, null);
|
|
|
|
if (response != null)
|
|
{
|
|
JObject obj = JObject.Parse(await response.Content.ReadAsStringAsync());
|
|
if (response.StatusCode.ToString() == "OK")
|
|
{
|
|
if (!string.IsNullOrEmpty(Convert.ToString(obj["access_token"])))
|
|
token.Value = Convert.ToString(obj["access_token"]);
|
|
if (!string.IsNullOrEmpty(Convert.ToString(obj["expires_in"])))
|
|
token.ExpiresIn = Convert.ToInt32(obj["expires_in"]);
|
|
}
|
|
}
|
|
return token;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
return token;
|
|
}
|
|
|
|
|
|
public static async Task<HttpResponseMessage> ReturnMuleSoftResponse(IHttpClientHandler clientHandler, string encstring, string url, string authToken, string encKeyToHeader)
|
|
{
|
|
try
|
|
{
|
|
var data = "{\"encData\": \"" + encstring + "\"}";
|
|
|
|
HttpClient request = clientHandler.GetHttpClient();
|
|
Uri requestUri = new Uri(url);
|
|
|
|
var httpContent = new MultipartFormDataContent();
|
|
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
|
request.DefaultRequestHeaders.Add("client_id", ClientId);
|
|
request.DefaultRequestHeaders.Add("client_secret", ClientSecret);
|
|
request.DefaultRequestHeaders.Add("Authorization", "Bearer " + authToken);
|
|
request.DefaultRequestHeaders.Add("encKey", encKeyToHeader);
|
|
return await request.PostAsync(requestUri, new StringContent(data, UnicodeEncoding.UTF8, "application/json"));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|