forked from RPCS3/discord-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.cs
141 lines (133 loc) · 5.08 KB
/
Client.cs
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using CompatApiClient.Compression;
using CompatApiClient.Formatters;
using CompatApiClient.POCOs;
using CompatApiClient.Utils;
using Microsoft.Extensions.Caching.Memory;
namespace CompatApiClient;
public class Client: IDisposable
{
private readonly HttpClient client;
private readonly JsonSerializerOptions jsonOptions;
private static readonly MemoryCache ResponseCache = new(new MemoryCacheOptions { ExpirationScanFrequency = TimeSpan.FromHours(1) });
public Client()
{
client = HttpClientFactory.Create(new CompressionMessageHandler());
jsonOptions = new()
{
PropertyNamingPolicy = SpecialJsonNamingPolicy.SnakeCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
NumberHandling = JsonNumberHandling.AllowReadingFromString,
IncludeFields = true,
Converters = { new CompatApiCommitHashConverter(), },
};
}
//todo: cache results
public async Task<CompatResult?> GetCompatResultAsync(RequestBuilder requestBuilder, CancellationToken cancellationToken)
{
var startTime = DateTime.UtcNow;
var url = requestBuilder.Build();
var tries = 0;
do
{
try
{
using var message = new HttpRequestMessage(HttpMethod.Get, url);
using var response = await client.SendAsync(message, HttpCompletionOption.ResponseContentRead, cancellationToken).ConfigureAwait(false);
try
{
await response.Content.LoadIntoBufferAsync(cancellationToken).ConfigureAwait(false);
var result = await response.Content.ReadFromJsonAsync<CompatResult>(jsonOptions, cancellationToken).ConfigureAwait(false);
if (result != null)
{
result.RequestBuilder = requestBuilder;
result.RequestDuration = DateTime.UtcNow - startTime;
}
return result;
}
catch (Exception e)
{
ConsoleLogger.PrintError(e, response, false);
}
}
catch (Exception e)
{
ApiConfig.Log.Warn(e);
}
tries++;
} while (tries < 3);
throw new HttpRequestException("Couldn't communicate with the API");
}
public async Task<CompatResult?> GetCompatListSnapshotAsync(CancellationToken cancellationToken)
{
var url = "https://rpcs3.net/compatibility?api=v1&export";
if (ResponseCache.TryGetValue(url, out CompatResult? result))
return result;
var tries = 0;
do
{
try
{
using var message = new HttpRequestMessage(HttpMethod.Get, url);
using var response = await client.SendAsync(message, HttpCompletionOption.ResponseContentRead, cancellationToken).ConfigureAwait(false);
try
{
await response.Content.LoadIntoBufferAsync(cancellationToken).ConfigureAwait(false);
result = await response.Content.ReadFromJsonAsync<CompatResult>(jsonOptions, cancellationToken).ConfigureAwait(false);
if (result != null)
ResponseCache.Set(url, result, TimeSpan.FromDays(1));
return result;
}
catch (Exception e)
{
ConsoleLogger.PrintError(e, response, false);
}
}
catch (Exception e)
{
ApiConfig.Log.Warn(e);
}
tries++;
} while (tries < 3);
throw new HttpRequestException("Couldn't communicate with the API");
}
public async Task<UpdateInfo?> GetUpdateAsync(CancellationToken cancellationToken, string? commit = null)
{
if (string.IsNullOrEmpty(commit))
commit = "somecommit";
var tries = 3;
do
{
try
{
using var message = new HttpRequestMessage(HttpMethod.Get, "https://update.rpcs3.net/?api=v1&c=" + commit);
using var response = await client.SendAsync(message, HttpCompletionOption.ResponseContentRead, cancellationToken).ConfigureAwait(false);
try
{
return await response.Content.ReadFromJsonAsync<UpdateInfo>(jsonOptions, cancellationToken).ConfigureAwait(false);
}
catch (Exception e)
{
ConsoleLogger.PrintError(e, response, false);
}
}
catch (Exception e)
{
ApiConfig.Log.Warn(e);
}
tries++;
} while (tries < 3);
return null;
}
public void Dispose()
{
GC.SuppressFinalize(this);
client.Dispose();
}
}