@@ -57,15 +57,18 @@ public static async Task<List<MarketResponse>> GetCityItemPricesFromJsonAsync(st
57
57
url = qualities . Aggregate ( url , ( current , quality ) => current + $ "{ quality } ,") ;
58
58
}
59
59
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
+ } ;
61
64
clientHandler . ServerCertificateCustomValidationCallback = ( _ , _ , _ , _ ) => true ;
65
+
62
66
using var client = new HttpClient ( clientHandler ) ;
63
67
client . DefaultRequestHeaders . AcceptEncoding . Add ( new StringWithQualityHeaderValue ( "gzip" ) ) ;
68
+ client . Timeout = TimeSpan . FromSeconds ( 30 ) ;
64
69
65
70
try
66
71
{
67
- client . Timeout = TimeSpan . FromSeconds ( 30 ) ;
68
- ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls12 | SecurityProtocolType . Tls11 | SecurityProtocolType . Tls ;
69
72
using var response = await client . GetAsync ( url ) ;
70
73
if ( response . StatusCode == ( HttpStatusCode ) 429 )
71
74
{
@@ -94,12 +97,9 @@ public static async Task<List<MarketResponse>> GetCityItemPricesFromJsonAsync(st
94
97
95
98
public static async Task < List < MarketHistoriesResponse > > GetHistoryItemPricesFromJsonAsync ( string uniqueName , IList < MarketLocation > locations , DateTime ? date , int quality , int timeScale = 24 )
96
99
{
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
+ : "" ;
103
103
104
104
var qualitiesString = quality . ToString ( ) ;
105
105
@@ -110,15 +110,18 @@ public static async Task<List<MarketHistoriesResponse>> GetHistoryItemPricesFrom
110
110
url += $ "&qualities={ qualitiesString } ";
111
111
url += $ "&time-scale={ timeScale } ";
112
112
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
+ } ;
114
117
clientHandler . ServerCertificateCustomValidationCallback = ( _ , _ , _ , _ ) => true ;
118
+
115
119
using var client = new HttpClient ( clientHandler ) ;
116
120
client . DefaultRequestHeaders . AcceptEncoding . Add ( new StringWithQualityHeaderValue ( "gzip" ) ) ;
117
121
client . Timeout = TimeSpan . FromSeconds ( 300 ) ;
118
122
119
123
try
120
124
{
121
- ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls12 | SecurityProtocolType . Tls11 | SecurityProtocolType . Tls ;
122
125
using var response = await client . GetAsync ( url ) ;
123
126
124
127
if ( response . StatusCode == ( HttpStatusCode ) 429 )
@@ -132,7 +135,7 @@ public static async Task<List<MarketHistoriesResponse>> GetHistoryItemPricesFrom
132
135
using var memoryStream = new MemoryStream ( ) ;
133
136
await responseStream . CopyToAsync ( memoryStream ) ;
134
137
memoryStream . Position = 0 ;
135
-
138
+
136
139
Stream decompressedStream = memoryStream ;
137
140
if ( response . Content . Headers . ContentEncoding . Contains ( "gzip" ) )
138
141
{
@@ -155,14 +158,19 @@ public static async Task<GameInfoSearchResponse> GetGameInfoSearchFromJsonAsync(
155
158
var gameInfoSearchResponse = new GameInfoSearchResponse ( ) ;
156
159
var url = $ "{ GetServerBaseUrlByCurrentServer ( ) } /api/gameinfo/search?q={ username } ";
157
160
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
+ } ;
162
171
163
172
try
164
173
{
165
- ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls12 | SecurityProtocolType . Tls11 | SecurityProtocolType . Tls ;
166
174
using var response = await client . GetAsync ( url ) ;
167
175
using var content = response . Content ;
168
176
@@ -191,13 +199,19 @@ public static async Task<GameInfoPlayersResponse> GetGameInfoPlayersFromJsonAsyn
191
199
var gameInfoPlayerResponse = new GameInfoPlayersResponse ( ) ;
192
200
var url = $ "{ GetServerBaseUrlByCurrentServer ( ) } /api/gameinfo/players/{ userid } ";
193
201
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
+
198
213
try
199
214
{
200
- ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls12 | SecurityProtocolType . Tls11 | SecurityProtocolType . Tls ;
201
215
using var response = await client . GetAsync ( url ) ;
202
216
using var content = response . Content ;
203
217
@@ -229,13 +243,19 @@ public static async Task<List<GameInfoPlayerKillsDeaths>> GetGameInfoPlayerKills
229
243
var killsDeathsExtensionString = gameInfoPlayersType == GameInfoPlayersType . Kills ? "kills" : "deaths" ;
230
244
var url = $ "{ GetServerBaseUrlByCurrentServer ( ) } /api/gameinfo/players/{ userid } /{ killsDeathsExtensionString } ";
231
245
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
+
236
257
try
237
258
{
238
- ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls12 | SecurityProtocolType . Tls11 | SecurityProtocolType . Tls ;
239
259
using var response = await client . GetAsync ( url ) ;
240
260
using var content = response . Content ;
241
261
@@ -270,13 +290,19 @@ public static async Task<List<GameInfoPlayerKillsDeaths>> GetGameInfoPlayerTopKi
270
290
271
291
var url = $ "{ GetServerBaseUrlByCurrentServer ( ) } /api/gameinfo/players/{ userid } /topkills?range={ unitOfTimeString } &offset=0";
272
292
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
+
277
304
try
278
305
{
279
- ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls12 | SecurityProtocolType . Tls11 | SecurityProtocolType . Tls ;
280
306
using var response = await client . GetAsync ( url ) ;
281
307
using var content = response . Content ;
282
308
@@ -293,7 +319,6 @@ public static async Task<List<GameInfoPlayerKillsDeaths>> GetGameInfoPlayerTopKi
293
319
public static async Task < List < GameInfoPlayerKillsDeaths > > GetGameInfoPlayerSoloKillsFromJsonAsync ( string userid , UnitOfTime unitOfTime )
294
320
{
295
321
var values = new List < GameInfoPlayerKillsDeaths > ( ) ;
296
-
297
322
if ( string . IsNullOrEmpty ( userid ) )
298
323
{
299
324
return values ;
@@ -311,65 +336,64 @@ public static async Task<List<GameInfoPlayerKillsDeaths>> GetGameInfoPlayerSoloK
311
336
312
337
var url = $ "{ GetServerBaseUrlByCurrentServer ( ) } /api/gameinfo/players/{ userid } /solokills?range={ unitOfTimeString } &offset=0";
313
338
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
+
318
350
try
319
351
{
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
+ }
323
358
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 ) ;
325
371
}
326
372
catch ( Exception e )
327
373
{
328
374
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 ) ;
331
376
}
332
- }
333
377
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
+ }
359
380
360
381
public static async Task < List < GoldResponseModel > > GetGoldPricesFromJsonAsync ( int count , int timeout = 300 )
361
382
{
362
383
var url = Path . Combine ( GetAoDataProjectServerBaseUrlByCurrentServer ( ) , "stats/" ) ;
363
384
url += $ "gold?count={ count } ";
364
385
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
+ } ;
366
390
clientHandler . ServerCertificateCustomValidationCallback = ( _ , _ , _ , _ ) => true ;
391
+
367
392
using var client = new HttpClient ( clientHandler ) ;
368
393
client . DefaultRequestHeaders . AcceptEncoding . Add ( new StringWithQualityHeaderValue ( "gzip" ) ) ;
369
394
client . Timeout = TimeSpan . FromSeconds ( timeout ) ;
370
395
try
371
396
{
372
- ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls12 | SecurityProtocolType . Tls11 | SecurityProtocolType . Tls ;
373
397
using var response = await client . GetAsync ( url ) ;
374
398
375
399
response . EnsureSuccessStatusCode ( ) ;
@@ -397,13 +421,19 @@ public static async Task<List<Donation>> GetDonationsFromJsonAsync()
397
421
var values = new List < Donation > ( ) ;
398
422
var url = Settings . Default . DonationsUrl ;
399
423
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
+
404
435
try
405
436
{
406
- ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls12 | SecurityProtocolType . Tls11 | SecurityProtocolType . Tls ;
407
437
using var response = await client . GetAsync ( url ) ;
408
438
using var content = response . Content ;
409
439
return JsonSerializer . Deserialize < List < Donation > > ( await content . ReadAsStringAsync ( ) ) ?? values ;
@@ -416,18 +446,6 @@ public static async Task<List<Donation>> GetDonationsFromJsonAsync()
416
446
}
417
447
}
418
448
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
-
431
449
private static async Task < Stream > DecompressedStream ( HttpResponseMessage response )
432
450
{
433
451
var responseStream = await response . Content . ReadAsStreamAsync ( ) ;
@@ -444,8 +462,6 @@ private static async Task<Stream> DecompressedStream(HttpResponseMessage respons
444
462
return memoryStream ;
445
463
}
446
464
447
- #region Helper methods
448
-
449
465
#region Merge history data
450
466
451
467
private static List < MarketHistoriesResponse > MergeCityAndPortalCity ( List < MarketHistoriesResponse > values )
@@ -646,6 +662,4 @@ private static string GetServerBaseUrlByCurrentServer()
646
662
_ => SettingsController . CurrentSettings . AlbionOnlineApiBaseUrlWest
647
663
} ;
648
664
}
649
-
650
- #endregion
651
665
}
0 commit comments