ServiceAvailabilityChecker
using Relativity.DataExchange.Logger;
using Relativity.DataTransfer.Legacy.SDK.ImportExport.V1.Models;
using Relativity.Logging;
using Relativity.Services.Exceptions;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Net;
namespace Relativity.DataExchange.Service.WebApiVsKeplerSwitch
{
public class ServiceAvailabilityChecker : IServiceAvailabilityChecker
{
private readonly IIAPICommunicationModeManager iApiCommunicationModeManager;
private readonly ILog logger;
private bool? isWebApiAvailable;
private bool? isKeplerAvailable;
private IAPICommunicationMode? iApiCommunicationMode;
public ServiceAvailabilityChecker(IIAPICommunicationModeManager iApiCommunicationModeManager, ILog logger)
{
this.iApiCommunicationModeManager = iApiCommunicationModeManager;
this.logger = (((object)logger) ?? ((object)RelativityLogger.Instance));
}
public ServiceAvailabilityChecker(IIAPICommunicationModeManager iApiCommunicationModeManager)
: this(iApiCommunicationModeManager, RelativityLogger.Instance)
{
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "We catch general exception type to check if service is available. We are not interested in any specific exception type and we don't want to rethrow the exception.")]
public bool IsWebApiAvailable()
{
if (isWebApiAvailable.HasValue)
return isWebApiAvailable.Value;
try {
RelativityInstanceInfo relativityInstanceInfo = new RelativityInstanceInfo();
relativityInstanceInfo.Credentials = new NetworkCredential();
relativityInstanceInfo.CookieContainer = new CookieContainer();
relativityInstanceInfo.WebApiServiceUrl = new Uri(AppSettings.Instance.WebApiServiceUrl);
new RelativityManagerService(relativityInstanceInfo).GetRelativityUrl();
isWebApiAvailable = true;
} catch (Exception ex) {
ILog obj = logger;
if (obj != null)
obj.LogError(ex, $"""{ex}", Array.Empty<object>());
isWebApiAvailable = false;
}
return isWebApiAvailable.Value;
}
public bool IsKeplerAvailable()
{
RefreshKeplerAvailabilityInfo();
return isKeplerAvailable.GetValueOrDefault();
}
public IAPICommunicationMode? ReadImportApiCommunicationMode()
{
RefreshKeplerAvailabilityInfo();
return iApiCommunicationMode;
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "We catch general exception type to check if service is available. We are not interested in any specific exception type and we don't want to rethrow the exception.")]
private void RefreshKeplerAvailabilityInfo()
{
if (!isKeplerAvailable.HasValue || !iApiCommunicationMode.HasValue)
try {
iApiCommunicationMode = iApiCommunicationModeManager.ReadImportApiCommunicationMode().GetAwaiter().GetResult();
isKeplerAvailable = true;
} catch (NotAuthorizedException) {
isKeplerAvailable = true;
ILog obj = logger;
if (obj != null)
obj.LogWarning("NotAuthorizedException was thrown when reading communication mode.", Array.Empty<object>());
} catch (Exception ex) {
ILog obj2 = logger;
if (obj2 != null)
obj2.LogError(ex, $"""{ex}", Array.Empty<object>());
isKeplerAvailable = false;
}
}
}
}