EnVisageOnline/Main/Source/PrevuWebAPI/Code/Managers/APIClientInformationCallMan...

73 lines
2.5 KiB
C#

using Code.Security;
using Code.Utils;
using PrevuWebAPI.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.Caching;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace PrevuWebAPI.Code.Managers
{
public class APIClientInformationCallManager: BaseManager
{
// private HttpClient _client = null;
private static System.Runtime.Caching.ObjectCache cache = MemoryCache.Default;
public static APIClientInfoControlModel ClientInfo { get
{
string Id = RESTAuthorizeAttribute.Token();
if (string.IsNullOrEmpty(Id))
return null;
if (cache[Id] != null)
return (APIClientInfoControlModel) cache[Id];
return null;
}
}
public Guid GetClientID(string name)
{
HttpClient _client = Connect();
Settings.Logger.Log(NLog.LogLevel.Debug, "calling client to get info");
HttpResponseMessage response = _client.GetAsync("api/ClientInformationAPI/GetClientInfoByToken/" + name).Result;
if (response.IsSuccessStatusCode)
{
var _ClientInfo = response.Content.ReadAsAsync<APIClientInfoControlModel>().Result;
Settings.Logger.Log(NLog.LogLevel.Debug, "client info loaded for client:"+_ClientInfo.ClientName);
string Id = RESTAuthorizeAttribute.Token();
cache[Id] = _ClientInfo;
Settings.Logger.Log(NLog.LogLevel.Debug, "Adding client info to cache");
return _ClientInfo.Id;
}
else {
Settings.Logger.Log(NLog.LogLevel.Debug, "call failed!");
Settings.Logger.Log(NLog.LogLevel.Debug, "request code " + response.StatusCode.ToString());
}
return Guid.Empty;
}
private HttpClient Connect()
{
string url = ConfigurationManager.AppSettings["ClientInformationControlUrl"];
Settings.Logger.Log(NLog.LogLevel.Debug, "connecting to "+url);
HttpClient _client = new HttpClient();
_client.BaseAddress = new Uri(url);
_client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
Settings.Logger.Log(NLog.LogLevel.Debug, "connected to " + url);
return _client;
}
}
}