1 |
3c82c2ad
|
Pultak
|
using System.Diagnostics;
|
2 |
|
|
using System.Net.Http.Json;
|
3 |
88eab964
|
Pultak
|
using System.Text;
|
4 |
3c82c2ad
|
Pultak
|
using System.Text.Json;
|
5 |
|
|
using System.Text.Json.Serialization;
|
6 |
88eab964
|
Pultak
|
using DiskQueue;
|
7 |
3c82c2ad
|
Pultak
|
using LDClient.network.data;
|
8 |
|
|
|
9 |
|
|
namespace LDClient.network {
|
10 |
|
|
public class ApiClient : IApiClient {
|
11 |
|
|
|
12 |
|
|
public static readonly Payload ExampleInfo = new() {
|
13 |
|
|
UserName = "honikCz",
|
14 |
|
|
HostName = "Bramborak",
|
15 |
|
|
TimeStamp = DateTime.Parse("2022-03-21 18:05:00.168895"),
|
16 |
|
|
HeadDevice = new DebuggerInfo {
|
17 |
|
|
SerialNumber = "C12345678912"
|
18 |
|
|
},
|
19 |
|
|
BodyDevice = new DebuggerInfo {
|
20 |
|
|
SerialNumber = "C98765432198"
|
21 |
|
|
},
|
22 |
|
|
Status = ConnectionStatus.Connected
|
23 |
|
|
};
|
24 |
|
|
|
25 |
88eab964
|
Pultak
|
private bool _running;
|
26 |
|
|
|
27 |
3c82c2ad
|
Pultak
|
|
28 |
|
|
private readonly string _uri;
|
29 |
|
|
private readonly HttpClient _client;
|
30 |
88eab964
|
Pultak
|
private readonly IPersistentQueue _cache;
|
31 |
3c82c2ad
|
Pultak
|
private readonly uint _retryPeriod;
|
32 |
88eab964
|
Pultak
|
private readonly uint _maxEntries;
|
33 |
|
|
private readonly uint _maxRetries;
|
34 |
|
|
|
35 |
3c82c2ad
|
Pultak
|
|
36 |
|
|
|
37 |
88eab964
|
Pultak
|
public ApiClient(string url, uint port, string path, uint retryPeriod, uint maxEntries, uint maxRetries,
|
38 |
|
|
string cacheFilename) {
|
39 |
3c82c2ad
|
Pultak
|
_uri = $"{url}:{port}{path}";
|
40 |
|
|
_retryPeriod = retryPeriod;
|
41 |
88eab964
|
Pultak
|
_maxEntries = maxEntries;
|
42 |
|
|
_maxRetries = maxRetries;
|
43 |
|
|
|
44 |
3c82c2ad
|
Pultak
|
_client = new HttpClient();
|
45 |
88eab964
|
Pultak
|
_cache = new PersistentQueue(cacheFilename);
|
46 |
3c82c2ad
|
Pultak
|
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
public async Task SendPayloadAsync(Payload payload) {
|
50 |
|
|
try {
|
51 |
|
|
Stopwatch stopWatch = new();
|
52 |
|
|
stopWatch.Start();
|
53 |
522ba9b4
|
Pultak
|
|
54 |
3c82c2ad
|
Pultak
|
var response = await _client.PostAsJsonAsync(_uri, payload, new JsonSerializerOptions {
|
55 |
|
|
Converters = {
|
56 |
|
|
new JsonStringEnumConverter( JsonNamingPolicy.CamelCase)
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
});
|
60 |
|
|
stopWatch.Stop();
|
61 |
522ba9b4
|
Pultak
|
CreateRequestLog(payload, response, stopWatch.ElapsedMilliseconds);
|
62 |
3c82c2ad
|
Pultak
|
|
63 |
|
|
response.EnsureSuccessStatusCode();
|
64 |
|
|
} catch (Exception e) {
|
65 |
|
|
Program.DefaultLogger.Error($"Failed to send {payload} to the server. Due to: {e.Message}");
|
66 |
522ba9b4
|
Pultak
|
CachePayload(payload);
|
67 |
3c82c2ad
|
Pultak
|
}
|
68 |
|
|
}
|
69 |
|
|
|
70 |
522ba9b4
|
Pultak
|
private static void CreateRequestLog(Payload payload, HttpResponseMessage response, long durationMs) {
|
71 |
0ba79cc3
|
Pultak
|
var responseToLog = new {
|
72 |
|
|
statusCode = response.StatusCode,
|
73 |
|
|
content = response.Content,
|
74 |
|
|
headers = response.Headers,
|
75 |
|
|
errorMessage = response.RequestMessage,
|
76 |
|
|
};
|
77 |
|
|
|
78 |
|
|
Program.DefaultLogger.Info($"Request completed in {durationMs} ms,\n" +
|
79 |
522ba9b4
|
Pultak
|
$"Request body: {payload},\n" +
|
80 |
|
|
$"Response: {responseToLog}");
|
81 |
|
|
}
|
82 |
|
|
|
83 |
88eab964
|
Pultak
|
|
84 |
eda1e8c7
|
Pultak
|
private async Task ResendPayloadsAsync() {
|
85 |
|
|
var numberOfPayloadsToResend = Math.Min(_maxRetries, _cache.EstimatedCountOfItemsInQueue);
|
86 |
|
|
var payloads = new List<Payload>();
|
87 |
|
|
|
88 |
|
|
using (var session = _cache.OpenSession()) {
|
89 |
|
|
for (var i = 0; i < numberOfPayloadsToResend; i++) {
|
90 |
|
|
var rawBytes = session.Dequeue();
|
91 |
|
|
var payload = JsonSerializer.Deserialize<Payload>(rawBytes);
|
92 |
|
|
if (payload is not null) {
|
93 |
|
|
payloads.Add(payload);
|
94 |
|
|
}
|
95 |
|
|
}
|
96 |
|
|
session.Flush();
|
97 |
|
|
}
|
98 |
|
|
|
99 |
|
|
if (payloads.Count > 0) {
|
100 |
|
|
Program.DefaultLogger.Debug($"ResendPayloadAsync -> {payloads.Count} unsent payloads");
|
101 |
|
|
var tasks = new List<Task>();
|
102 |
|
|
foreach (var payload in payloads) {
|
103 |
|
|
Program.DefaultLogger.Info($"Resending {payload}.");
|
104 |
|
|
tasks.Add(SendPayloadAsync(payload));
|
105 |
|
|
}
|
106 |
|
|
await Task.WhenAll(tasks);
|
107 |
|
|
}
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
|
111 |
|
|
private void CachePayload(Payload payload) {
|
112 |
|
|
Program.DefaultLogger.Info($"Storing {payload} into the cache.");
|
113 |
|
|
var numberOfCachedPayloads = _cache.EstimatedCountOfItemsInQueue;
|
114 |
|
|
using var session = _cache.OpenSession();
|
115 |
|
|
if (numberOfCachedPayloads >= _maxEntries) {
|
116 |
|
|
session.Dequeue();
|
117 |
|
|
}
|
118 |
|
|
var payloadJson = JsonSerializer.Serialize(payload);
|
119 |
|
|
session.Enqueue(Encoding.UTF8.GetBytes(payloadJson));
|
120 |
|
|
session.Flush();
|
121 |
|
|
}
|
122 |
|
|
|
123 |
88eab964
|
Pultak
|
public async void Run() {
|
124 |
|
|
Program.DefaultLogger.Info("Api Client thread has started");
|
125 |
|
|
_running = true;
|
126 |
|
|
while (_running) {
|
127 |
|
|
await ResendPayloadsAsync();
|
128 |
|
|
Thread.Sleep((int)_retryPeriod);
|
129 |
|
|
}
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
public void Stop() {
|
133 |
|
|
_running = false;
|
134 |
0ba79cc3
|
Pultak
|
}
|
135 |
3c82c2ad
|
Pultak
|
}
|
136 |
|
|
}
|