-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectorBase.cs
More file actions
126 lines (113 loc) · 5.33 KB
/
ConnectorBase.cs
File metadata and controls
126 lines (113 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Configuration;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.EndpointMonitoring;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Licensing;
using Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Logging;
using Dynamicweb.Ecommerce.Orders;
using Dynamicweb.Extensibility.Notifications;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using static Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Notifications.Communication;
namespace Dynamicweb.Ecommerce.DynamicwebLiveIntegration.Connectors
{
/// <summary>
/// Abstract base connector
/// </summary>
internal abstract class ConnectorBase(Settings settings, Logger logger, Order order, SubmitType submitType)
{
protected Settings Settings { get; } = settings;
protected Logger Logger { get; } = logger;
protected Order Order { get; } = order;
protected SubmitType SubmitType { get; } = submitType;
internal abstract EndpointInfo GetEndpoint();
internal abstract string Execute(string request);
internal abstract string Execute(EndpointInfo endpoint, string request, TimeSpan responseTimeout);
internal abstract bool IsConnectionAvailableFromBackend(string endpointId, out string error);
/// <summary>
/// Determines whether the web service connection is available.
/// </summary>
internal abstract bool IsWebServiceConnectionAvailable();
internal abstract IEnumerable<string> GetUrls(string multipleUrlsText);
internal void Error(EndpointInfo endpoint)
{
EndpointMonitoringService.Error(Settings, endpoint, SubmitType);
}
internal string Execute(EndpointInfo endpoint, string request)
{
return Execute(endpoint, request, TimeSpan.Zero);
}
protected bool ExecuteConnectionAvailableRequest(EndpointInfo endpoint, out Exception error)
{
error = null;
if (EndpointMonitoringService.IsStillValid(Settings, endpoint, out bool connectionAvailable))
{
return connectionAvailable;
}
Diagnostics.ExecutionTable.Current?.Add("DynamicwebLiveIntegration.ConnectorBase.IsWebServiceConnectionAvailable(endpoint) START");
var lastStatus = EndpointMonitoringService.GetEndpointStatus(endpoint);
bool isWebServiceConnectionAvailable = (lastStatus != null) && !lastStatus.Error;
DateTime? lastErpCommunication = lastStatus?.LastErpCommunication;
bool success = false;
string request = "<GetEcomData></GetEcomData>"; // simple request for checking connection
string response = null;
try
{
TimeSpan ts = Settings.ConnectionTimeout > 0 ? new TimeSpan(0, 0, Settings.ConnectionTimeout) : TimeSpan.Zero;
response = Execute(endpoint, request, ts);
success = true;
EndpointMonitoringService.Success(endpoint);
}
catch (Exception ex)
{
error = ex;
Logger?.Log(ErrorLevel.ConnectionError, $"Error checking Web Service connection: '{ex.Message}'. Request: '{request}'.");
EndpointMonitoringService.Error(Settings, endpoint, SubmitType);
NotificationManager.Notify(OnErpCommunicationLost, new OnErpCommunicationLostArgs(request, ex, Settings, Logger));
if (Connector.EnableThrowExceptions)
{
throw new Exception("Thrown exception", ex);
}
}
if (success && lastErpCommunication.HasValue && !isWebServiceConnectionAvailable)
{
NotificationManager.Notify(OnErpCommunicationRestored, new OnErpCommunicationRestoredArgs(request, lastErpCommunication, Settings, Logger));
}
if (success && !string.IsNullOrEmpty(response))
{
LicenseService.ValidateLicense(endpoint.GetUrl(), response, Logger);
}
Diagnostics.ExecutionTable.Current?.Add("DynamicwebLiveIntegration.ConnectorBase.IsWebServiceConnectionAvailable(endpoint) END");
return success;
}
internal bool IsConnectionAvailableFromBackend(string multipleUrlsText)
{
bool result = false;
List<string> errorsList = new List<string>();
IEnumerable<string> urls = null;
if (!string.IsNullOrEmpty(multipleUrlsText))
{
result = true;
urls = GetUrls(multipleUrlsText);
foreach (string endpointId in urls)
{
bool isAvailable = IsConnectionAvailableFromBackend(endpointId, out string message);
if (isAvailable)
{
result = result != false;
}
else
{
errorsList.Add(message);
result = false;
}
}
}
if (!result && urls?.Count() > 1 && errorsList.Count > 0)
{
throw new HttpRequestException(string.Join(System.Environment.NewLine, errorsList));
}
return result;
}
}
}