-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathIntegrationConfigProvider.cs
More file actions
102 lines (96 loc) · 4.23 KB
/
IntegrationConfigProvider.cs
File metadata and controls
102 lines (96 loc) · 4.23 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
using QueueIT.KnownUserV3.SDK.IntegrationConfig;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Timers;
using System.Web.Script.Serialization;
namespace QueueIT.KnownUserV3.SDK.IntegrationConfigLoader
{
/// <summary>
/// This is an example showing how the Integration Configuration can be downloaded from Go Queue-it.
/// It is also deserialized to a CustomerIntegration object and cached in memory for 5 minutes.
/// The retry logic will try 5 times with 5 seconds interval and with a 4 second timeout.
/// </summary>
public static class IntegrationConfigProvider
{
private const int _downloadTimeoutMS = 4000;
internal static int _RefreshIntervalS = 5 * 60;
internal static double _RetryExceptionSleepS = 5;
private static Timer _timer;
private static readonly object _lockObject = new object();
static CustomerIntegration _cachedIntegrationConfig;
private static bool _isInitialized = false;
private static string _customerId;
private static string _apiKey;
public static CustomerIntegration GetCachedIntegrationConfig(string customerId, string apiKey)
{
if(!_isInitialized)
{
_customerId = customerId;
_apiKey = apiKey;
lock (_lockObject)
{
if (!_isInitialized)
{
RefreshCache(init:true);
_timer = new Timer();
_timer.Interval = _RefreshIntervalS * 1000;
_timer.AutoReset = false;
_timer.Elapsed += TimerElapsed;
_timer.Start();
_isInitialized = true;
}
}
}
return _cachedIntegrationConfig;
}
private static void TimerElapsed(object sender, ElapsedEventArgs e)
{
RefreshCache(init:false);
_timer.Start();
}
private static void RefreshCache(bool init)
{
int tryCount = 0;
while (tryCount < 5)
{
var configUrl = string.Format("https://{0}.queue-it.net/status/integrationconfig/secure/{0}", _customerId);
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(configUrl);
request.Headers.Add("api-key", _apiKey);
request.UserAgent = "queueit-connector";
request.Timeout = _downloadTimeoutMS;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode != HttpStatusCode.OK)
throw new Exception($"It was not sucessful retriving config file status code {response.StatusCode} from {configUrl}");
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
JavaScriptSerializer deserializer = new JavaScriptSerializer();
var deserialized = deserializer.Deserialize<CustomerIntegration>(reader.ReadToEnd());
if (deserialized == null)
throw new Exception("CustomerIntegration is null");
_cachedIntegrationConfig = deserialized;
}
return;
}
}
catch (Exception ex)
{
++tryCount;
if (tryCount >= 5)
{
//Use your favorit logging framework to log the exceptoin
break;
}
if (!init)
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(_RetryExceptionSleepS));
else
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(0.200 * tryCount));
}
}
}
}
}