EnVisageOnline/Main/Source/EnVisage/Code/BLL/CustomerInfoControlAPIManag...

114 lines
4.4 KiB
C#

using EnVisage.Code.Cache;
using EnVisage.Models;
using Newtonsoft.Json;
using NLog;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace EnVisage.Code.BLL
{
public class CustomerInfoControlAPIManager
{
protected readonly Logger Logger = LogManager.GetCurrentClassLogger();
public ClientInfoControlModel GetClientInfo(string name)
{
char[] splprm = { '.' };
string[] parts = name.Split(splprm);
if (parts.Length > 1)
name = parts[0];
HttpClient _client = Connect();
if (_client == null)
return new ClientInfoControlModel();
HttpResponseMessage response = _client.GetAsync("api/ClientInformationAPI/GetClientInfo/" + name).Result;
if (response.IsSuccessStatusCode)
{
var _ClientInfo = response.Content.ReadAsAsync<ClientInfoControlModel>().Result;
return _ClientInfo;
}
else {
}
return null;
}
public ClientInfoControlModel GetClientInfo()
{
try {
string domain = HttpContext.Current.Request.Url.Host;
char[] splprm = { '.' };
string[] parts = domain.Split(splprm);
if (parts.Length > 1)
domain = parts[0];
//ClientInfoAPICache cache = new ClientInfoAPICache();
//var cliObj = cache.GetValueFromCache();
//if (cliObj == null)
// cliObj = new ClientInfoControlModel();
//if (cliObj.Id != Guid.Empty && cliObj.InstanceUrl == domain)
// return cliObj;
Logger.Log(NLog.LogLevel.Debug, "Getting ClientInfo for " + domain);
HttpClient _client = Connect();
if (_client == null)
{
var url = ConfigurationManager.AppSettings["ClientInformationControlUrl"];
Logger.Log(NLog.LogLevel.Debug, "Failed Client Info api connection" + url == null ? "No URL" : url);
return new ClientInfoControlModel();
}
HttpResponseMessage response = _client.GetAsync("api/ClientInformationAPI/GetClientInfo/" + domain).Result;
if (response.IsSuccessStatusCode)
{
Logger.Log(NLog.LogLevel.Debug, "Loading client info data from cic");
var _ClientInfo = response.Content.ReadAsAsync<ClientInfoControlModel>().Result;
Logger.Log(NLog.LogLevel.Debug, "Client Info Data:" + JsonConvert.SerializeObject(_ClientInfo));
// cache.AddValueToCache(_ClientInfo);
return _ClientInfo;
}
else {
Logger.Log(NLog.LogLevel.Debug, "Failed Client Info api connection Result status:" + response.StatusCode.ToDisplayValue());
}
}catch(Exception dds)
{
var sb = DateTime.Now.ToString() + ": " + dds.Message;
var logger = LogManager.GetCurrentClassLogger();
if (logger != null)
logger.Fatal(sb.ToString());
Logger.Log(NLog.LogLevel.Debug, "Failed Client Info api connection exception:" + dds.ToString());
}
return null;
}
private HttpClient Connect()
{
try {
string url = ConfigurationManager.AppSettings["ClientInformationControlUrl"];
if (url != null)
{
HttpClient _client = new HttpClient();
_client.BaseAddress = new Uri(url);
_client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
return _client;
}
}catch(Exception dds)
{
var sb = DateTime.Now.ToString() + ": " + dds.Message;
var logger = LogManager.GetCurrentClassLogger();
if (logger != null)
logger.Fatal(sb.ToString());
}
return null;
}
}
}