Skip to content

Commit e040b59

Browse files
committed
Merge branch 'dev'
2 parents 849bfb0 + aa94b22 commit e040b59

13 files changed

+242
-134
lines changed

src/StatisticsAnalysisTool/App.xaml.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ protected override async void OnStartup(StartupEventArgs e)
4242
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
4343
DispatcherUnhandledException += Application_DispatcherUnhandledException;
4444

45-
await AutoUpdateController.AutoUpdateAsync();
46-
4745
SettingsController.LoadSettings();
4846

47+
await AutoUpdateController.AutoUpdateAsync();
48+
4949
Culture.SetCulture(Culture.GetCultureByIetfLanguageTag(SettingsController.CurrentSettings.CurrentCultureIetfLanguageTag));
5050
if (!LocalizationController.Init())
5151
{

src/StatisticsAnalysisTool/Common/ApiController.cs

+106-92
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,18 @@ public static async Task<List<MarketResponse>> GetCityItemPricesFromJsonAsync(st
5757
url = qualities.Aggregate(url, (current, quality) => current + $"{quality},");
5858
}
5959

60-
using var clientHandler = new HttpClientHandler();
60+
using var clientHandler = new HttpClientHandler
61+
{
62+
SslProtocols = System.Security.Authentication.SslProtocols.Tls13 | System.Security.Authentication.SslProtocols.Tls12,
63+
};
6164
clientHandler.ServerCertificateCustomValidationCallback = (_, _, _, _) => true;
65+
6266
using var client = new HttpClient(clientHandler);
6367
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
68+
client.Timeout = TimeSpan.FromSeconds(30);
6469

6570
try
6671
{
67-
client.Timeout = TimeSpan.FromSeconds(30);
68-
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
6972
using var response = await client.GetAsync(url);
7073
if (response.StatusCode == (HttpStatusCode) 429)
7174
{
@@ -94,12 +97,9 @@ public static async Task<List<MarketResponse>> GetCityItemPricesFromJsonAsync(st
9497

9598
public static async Task<List<MarketHistoriesResponse>> GetHistoryItemPricesFromJsonAsync(string uniqueName, IList<MarketLocation> locations, DateTime? date, int quality, int timeScale = 24)
9699
{
97-
var locationsString = "";
98-
99-
if (locations?.Count > 0)
100-
{
101-
locationsString = string.Join(",", locations.Select(x => ((int) x).ToString()));
102-
}
100+
var locationsString = locations?.Count > 0
101+
? string.Join(",", locations.Select(x => ((int) x).ToString()))
102+
: "";
103103

104104
var qualitiesString = quality.ToString();
105105

@@ -110,15 +110,18 @@ public static async Task<List<MarketHistoriesResponse>> GetHistoryItemPricesFrom
110110
url += $"&qualities={qualitiesString}";
111111
url += $"&time-scale={timeScale}";
112112

113-
using var clientHandler = new HttpClientHandler();
113+
using var clientHandler = new HttpClientHandler
114+
{
115+
SslProtocols = System.Security.Authentication.SslProtocols.Tls13 | System.Security.Authentication.SslProtocols.Tls12,
116+
};
114117
clientHandler.ServerCertificateCustomValidationCallback = (_, _, _, _) => true;
118+
115119
using var client = new HttpClient(clientHandler);
116120
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
117121
client.Timeout = TimeSpan.FromSeconds(300);
118122

119123
try
120124
{
121-
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
122125
using var response = await client.GetAsync(url);
123126

124127
if (response.StatusCode == (HttpStatusCode) 429)
@@ -132,7 +135,7 @@ public static async Task<List<MarketHistoriesResponse>> GetHistoryItemPricesFrom
132135
using var memoryStream = new MemoryStream();
133136
await responseStream.CopyToAsync(memoryStream);
134137
memoryStream.Position = 0;
135-
138+
136139
Stream decompressedStream = memoryStream;
137140
if (response.Content.Headers.ContentEncoding.Contains("gzip"))
138141
{
@@ -155,14 +158,19 @@ public static async Task<GameInfoSearchResponse> GetGameInfoSearchFromJsonAsync(
155158
var gameInfoSearchResponse = new GameInfoSearchResponse();
156159
var url = $"{GetServerBaseUrlByCurrentServer()}/api/gameinfo/search?q={username}";
157160

158-
using var clientHandler = new HttpClientHandler();
159-
clientHandler.ServerCertificateCustomValidationCallback = (_, _, _, _) => true;
160-
using var client = new HttpClient(clientHandler);
161-
client.Timeout = TimeSpan.FromSeconds(600);
161+
using var clientHandler = new HttpClientHandler
162+
{
163+
SslProtocols = System.Security.Authentication.SslProtocols.Tls13 | System.Security.Authentication.SslProtocols.Tls12,
164+
ServerCertificateCustomValidationCallback = (_, _, _, _) => true
165+
};
166+
167+
using var client = new HttpClient(clientHandler)
168+
{
169+
Timeout = TimeSpan.FromSeconds(120)
170+
};
162171

163172
try
164173
{
165-
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
166174
using var response = await client.GetAsync(url);
167175
using var content = response.Content;
168176

@@ -191,13 +199,19 @@ public static async Task<GameInfoPlayersResponse> GetGameInfoPlayersFromJsonAsyn
191199
var gameInfoPlayerResponse = new GameInfoPlayersResponse();
192200
var url = $"{GetServerBaseUrlByCurrentServer()}/api/gameinfo/players/{userid}";
193201

194-
using var clientHandler = new HttpClientHandler();
195-
clientHandler.ServerCertificateCustomValidationCallback = (_, _, _, _) => true;
196-
using var client = new HttpClient(clientHandler);
197-
client.Timeout = TimeSpan.FromSeconds(120);
202+
using var clientHandler = new HttpClientHandler
203+
{
204+
SslProtocols = System.Security.Authentication.SslProtocols.Tls13 | System.Security.Authentication.SslProtocols.Tls12,
205+
ServerCertificateCustomValidationCallback = (_, _, _, _) => true
206+
};
207+
208+
using var client = new HttpClient(clientHandler)
209+
{
210+
Timeout = TimeSpan.FromSeconds(120)
211+
};
212+
198213
try
199214
{
200-
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
201215
using var response = await client.GetAsync(url);
202216
using var content = response.Content;
203217

@@ -229,13 +243,19 @@ public static async Task<List<GameInfoPlayerKillsDeaths>> GetGameInfoPlayerKills
229243
var killsDeathsExtensionString = gameInfoPlayersType == GameInfoPlayersType.Kills ? "kills" : "deaths";
230244
var url = $"{GetServerBaseUrlByCurrentServer()}/api/gameinfo/players/{userid}/{killsDeathsExtensionString}";
231245

232-
using var clientHandler = new HttpClientHandler();
233-
clientHandler.ServerCertificateCustomValidationCallback = (_, _, _, _) => true;
234-
using var client = new HttpClient(clientHandler);
235-
client.Timeout = TimeSpan.FromSeconds(600);
246+
using var clientHandler = new HttpClientHandler
247+
{
248+
SslProtocols = System.Security.Authentication.SslProtocols.Tls13 | System.Security.Authentication.SslProtocols.Tls12,
249+
ServerCertificateCustomValidationCallback = (_, _, _, _) => true
250+
};
251+
252+
using var client = new HttpClient(clientHandler)
253+
{
254+
Timeout = TimeSpan.FromSeconds(120)
255+
};
256+
236257
try
237258
{
238-
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
239259
using var response = await client.GetAsync(url);
240260
using var content = response.Content;
241261

@@ -270,13 +290,19 @@ public static async Task<List<GameInfoPlayerKillsDeaths>> GetGameInfoPlayerTopKi
270290

271291
var url = $"{GetServerBaseUrlByCurrentServer()}/api/gameinfo/players/{userid}/topkills?range={unitOfTimeString}&offset=0";
272292

273-
using var clientHandler = new HttpClientHandler();
274-
clientHandler.ServerCertificateCustomValidationCallback = (_, _, _, _) => true;
275-
using var client = new HttpClient(clientHandler);
276-
client.Timeout = TimeSpan.FromSeconds(600);
293+
using var clientHandler = new HttpClientHandler
294+
{
295+
SslProtocols = System.Security.Authentication.SslProtocols.Tls13 | System.Security.Authentication.SslProtocols.Tls12,
296+
ServerCertificateCustomValidationCallback = (_, _, _, _) => true
297+
};
298+
299+
using var client = new HttpClient(clientHandler)
300+
{
301+
Timeout = TimeSpan.FromSeconds(120)
302+
};
303+
277304
try
278305
{
279-
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
280306
using var response = await client.GetAsync(url);
281307
using var content = response.Content;
282308

@@ -293,7 +319,6 @@ public static async Task<List<GameInfoPlayerKillsDeaths>> GetGameInfoPlayerTopKi
293319
public static async Task<List<GameInfoPlayerKillsDeaths>> GetGameInfoPlayerSoloKillsFromJsonAsync(string userid, UnitOfTime unitOfTime)
294320
{
295321
var values = new List<GameInfoPlayerKillsDeaths>();
296-
297322
if (string.IsNullOrEmpty(userid))
298323
{
299324
return values;
@@ -311,65 +336,64 @@ public static async Task<List<GameInfoPlayerKillsDeaths>> GetGameInfoPlayerSoloK
311336

312337
var url = $"{GetServerBaseUrlByCurrentServer()}/api/gameinfo/players/{userid}/solokills?range={unitOfTimeString}&offset=0";
313338

314-
using var clientHandler = new HttpClientHandler();
315-
clientHandler.ServerCertificateCustomValidationCallback = (_, _, _, _) => true;
316-
using var client = new HttpClient(clientHandler);
317-
client.Timeout = TimeSpan.FromSeconds(600);
339+
using var clientHandler = new HttpClientHandler
340+
{
341+
SslProtocols = System.Security.Authentication.SslProtocols.Tls13 | System.Security.Authentication.SslProtocols.Tls12,
342+
ServerCertificateCustomValidationCallback = (_, _, _, _) => true
343+
};
344+
345+
using var client = new HttpClient(clientHandler)
346+
{
347+
Timeout = TimeSpan.FromSeconds(120)
348+
};
349+
318350
try
319351
{
320-
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
321-
using var response = await client.GetAsync(url);
322-
using var content = response.Content;
352+
var response = await client.GetAsync(url);
353+
if (!response.IsSuccessStatusCode)
354+
{
355+
Log.Warning("Request failed: {url}, Status: {statusCode}, Reason: {reason}", url, response.StatusCode, response.ReasonPhrase);
356+
return values;
357+
}
323358

324-
return JsonSerializer.Deserialize<List<GameInfoPlayerKillsDeaths>>(await content.ReadAsStringAsync()) ?? values;
359+
var jsonResponse = await response.Content.ReadAsStringAsync();
360+
return JsonSerializer.Deserialize<List<GameInfoPlayerKillsDeaths>>(jsonResponse) ?? values;
361+
}
362+
catch (JsonException e)
363+
{
364+
ConsoleManager.WriteLineForError(MethodBase.GetCurrentMethod()?.DeclaringType, e);
365+
Log.Error(e, "JSON deserialization failed. URL: {url}", url);
366+
}
367+
catch (TaskCanceledException e)
368+
{
369+
ConsoleManager.WriteLineForWarning(MethodBase.GetCurrentMethod()?.DeclaringType, e);
370+
Log.Warning(e, "Request timed out: {url}", url);
325371
}
326372
catch (Exception e)
327373
{
328374
ConsoleManager.WriteLineForError(MethodBase.GetCurrentMethod()?.DeclaringType, e);
329-
Log.Error(e, "{message}", MethodBase.GetCurrentMethod()?.DeclaringType);
330-
return values;
375+
Log.Error(e, "Unexpected error: {url}", url);
331376
}
332-
}
333377

334-
//public static async Task<GameInfoGuildsResponse> GetGameInfoGuildsFromJsonAsync(string guildId)
335-
//{
336-
// var url = $"{GetServerBaseUrlByCurrentServer()}/api/gameinfo/guilds/{guildId}";
337-
338-
// using (var client = new HttpClient())
339-
// {
340-
// client.Timeout = TimeSpan.FromSeconds(30);
341-
// try
342-
// {
343-
// using (var response = await client.GetAsync(url))
344-
// {
345-
// using (var content = response.Content)
346-
// {
347-
// return JsonConvert.DeserializeObject<GameInfoGuildsResponse>(await content.ReadAsStringAsync());
348-
// }
349-
// }
350-
// }
351-
// catch (Exception e)
352-
// {
353-
// ConsoleManager.WriteLineForError(MethodBase.GetCurrentMethod()?.DeclaringType, e);
354-
// Log.Error(e, "{message}", MethodBase.GetCurrentMethod()?.DeclaringType);
355-
// return null;
356-
// }
357-
// }
358-
//}
378+
return values;
379+
}
359380

360381
public static async Task<List<GoldResponseModel>> GetGoldPricesFromJsonAsync(int count, int timeout = 300)
361382
{
362383
var url = Path.Combine(GetAoDataProjectServerBaseUrlByCurrentServer(), "stats/");
363384
url += $"gold?count={count}";
364385

365-
using var clientHandler = new HttpClientHandler();
386+
using var clientHandler = new HttpClientHandler
387+
{
388+
SslProtocols = System.Security.Authentication.SslProtocols.Tls13 | System.Security.Authentication.SslProtocols.Tls12,
389+
};
366390
clientHandler.ServerCertificateCustomValidationCallback = (_, _, _, _) => true;
391+
367392
using var client = new HttpClient(clientHandler);
368393
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
369394
client.Timeout = TimeSpan.FromSeconds(timeout);
370395
try
371396
{
372-
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
373397
using var response = await client.GetAsync(url);
374398

375399
response.EnsureSuccessStatusCode();
@@ -397,13 +421,19 @@ public static async Task<List<Donation>> GetDonationsFromJsonAsync()
397421
var values = new List<Donation>();
398422
var url = Settings.Default.DonationsUrl;
399423

400-
using var clientHandler = new HttpClientHandler();
401-
clientHandler.ServerCertificateCustomValidationCallback = (_, _, _, _) => true;
402-
using var client = new HttpClient(clientHandler);
403-
client.Timeout = TimeSpan.FromSeconds(600);
424+
using var clientHandler = new HttpClientHandler
425+
{
426+
SslProtocols = System.Security.Authentication.SslProtocols.Tls13 | System.Security.Authentication.SslProtocols.Tls12,
427+
ServerCertificateCustomValidationCallback = (_, _, _, _) => true
428+
};
429+
430+
using var client = new HttpClient(clientHandler)
431+
{
432+
Timeout = TimeSpan.FromSeconds(600)
433+
};
434+
404435
try
405436
{
406-
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
407437
using var response = await client.GetAsync(url);
408438
using var content = response.Content;
409439
return JsonSerializer.Deserialize<List<Donation>>(await content.ReadAsStringAsync()) ?? values;
@@ -416,18 +446,6 @@ public static async Task<List<Donation>> GetDonationsFromJsonAsync()
416446
}
417447
}
418448

419-
//private static async Task<Stream> DecompressedStream(HttpResponseMessage response)
420-
//{
421-
// await using var responseStream = await response.Content.ReadAsStreamAsync();
422-
// Stream decompressedStream = responseStream;
423-
// if (response.Content.Headers.ContentEncoding.Contains("gzip"))
424-
// {
425-
// decompressedStream = new GZipStream(responseStream, CompressionMode.Decompress);
426-
// }
427-
428-
// return decompressedStream;
429-
//}
430-
431449
private static async Task<Stream> DecompressedStream(HttpResponseMessage response)
432450
{
433451
var responseStream = await response.Content.ReadAsStreamAsync();
@@ -444,8 +462,6 @@ private static async Task<Stream> DecompressedStream(HttpResponseMessage respons
444462
return memoryStream;
445463
}
446464

447-
#region Helper methods
448-
449465
#region Merge history data
450466

451467
private static List<MarketHistoriesResponse> MergeCityAndPortalCity(List<MarketHistoriesResponse> values)
@@ -646,6 +662,4 @@ private static string GetServerBaseUrlByCurrentServer()
646662
_ => SettingsController.CurrentSettings.AlbionOnlineApiBaseUrlWest
647663
};
648664
}
649-
650-
#endregion
651665
}

0 commit comments

Comments
 (0)