-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.cs
More file actions
284 lines (264 loc) · 10.6 KB
/
Copy pathAPI.cs
File metadata and controls
284 lines (264 loc) · 10.6 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
using Avalonia.Threading;
using System;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace AutoLogout
{
public class AppAPI
{
// Constants
#if DEBUG
private string Url = "http://localhost:8111/api/";
#else
private string Url = "https://autologout.yiays.com/api/";
#endif
private static readonly string API_VERSION = "3";
private static readonly string UASTRING =
$"AutoLogoutClient/{API_VERSION} (AutoLogout {State.Current.Version}) ({RuntimeInformation.OSDescription})";
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web)
{
Converters = { new DateOnlyWithOffsetJsonConverter() }
};
private readonly HttpClient httpClient = new()
{
Timeout = TimeSpan.FromSeconds(10),
DefaultRequestHeaders =
{
{ "User-Agent", UASTRING },
{ "Accept", "application/json" }
}
};
public DispatcherTimer syncTimer = new()
{
Interval = TimeSpan.FromSeconds(10)
};
public AppAPI()
{
syncTimer.Tick += async (o, e) =>
{
if(State.Current.Store.Online)
await Sync();
else {
Console.WriteLine("Stopped sync timer as Online mode was off");
syncTimer.Stop();
}
};
}
// Response models
private struct ApiResult<T>
{
public bool success { get; set; }
public HttpResponseMessage response { get; set; }
public T? result { get; set; }
}
private struct SyncResult
{
public bool accepted { get; set; }
public string? error { get; set; }
public DeltaState? delta { get; set; }
}
private sealed class DateOnlyWithOffsetJsonConverter : JsonConverter<DateOnly>
{
public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.String)
{
throw new JsonException("Expected a string value for DateOnly.");
}
string? value = reader.GetString();
if (string.IsNullOrWhiteSpace(value))
{
return default;
}
string datePart = value.Trim();
int separatorIndex = datePart.IndexOfAny(new[] { ' ', 'T' });
if (separatorIndex >= 0)
{
datePart = datePart[..separatorIndex];
}
return DateOnly.ParseExact(datePart, "yyyy-MM-dd", CultureInfo.InvariantCulture);
}
public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
}
}
private struct DeauthResult
{
public bool success { get; set; }
public string? error { get; set; }
}
public struct UpdateCheckResult
{
public string version { get; set; }
}
private async Task<ApiResult<T>> ApiCall<T>(
string endpoint, HttpMethod method, StringContent? content, Guid? authKey
)
{
if (authKey is not null && authKey != Guid.Empty)
{
httpClient.DefaultRequestHeaders.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authKey.ToString());
}
else
{
httpClient.DefaultRequestHeaders.Remove("Authorization");
}
HttpResponseMessage response;
try
{
response = await httpClient.SendAsync(
new HttpRequestMessage(method, Url + endpoint)
{
Content = content
}
);
}
catch (HttpRequestException ex)
{
Console.WriteLine($"Failed to call {endpoint}: {ex.Message}");
return new ApiResult<T> { success = false };
}
if (response.Headers.TryGetValues("X-Api-Version", out var apiVersionHeaders))
{
string apiVersion = apiVersionHeaders.FirstOrDefault() ?? "";
if (apiVersion != API_VERSION)
{
if (State.Current.Update != UpdateUrgency.Critical)
{
Console.WriteLine(
$"It appears this client is out of date. Expected API version: {API_VERSION}, got: {apiVersion}"
);
State.Current.Update = UpdateUrgency.Critical;
}
}
}
if (!response.IsSuccessStatusCode)
{
Console.WriteLine($"API call '{endpoint}' failed: {response.ReasonPhrase}");
if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
{
string responseBody = await response.Content.ReadAsStringAsync();
Console.Write(responseBody);
}
return new ApiResult<T> { success = false, response = response };
}
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
try {
T? result = JsonSerializer.Deserialize<T>(responseBody, JsonOptions);
if (result == null)
{
Console.WriteLine("Failed to deserialize API result.");
return new ApiResult<T> { success = false, response = response };
}
return new ApiResult<T> { success = true, response = response, result = result };
}
catch(JsonException e)
{
Console.WriteLine("Error encountered deserializing API result: "+e.Message);
}
}
return new ApiResult<T> { success = false, response = response };
}
public async Task<bool> Sync(Guid? syncAuthor = null)
{
// Convert state to JSON and share with online service
TimeSpan offset = TimeZoneInfo.Local.BaseUtcOffset;
string sign = offset < TimeSpan.Zero ? "-" : "+";
string timezone = sign + (offset < TimeSpan.Zero ? -offset : offset).ToString(@"hh\:mm");
string usageDate = State.Current.Store.usageDate.ToString(@"yyyy\-MM\-dd", CultureInfo.InvariantCulture) + ' ' + timezone;
string json = JsonSerializer.Serialize(new
{
State.Current.Store.hashedPassword,
State.Current.Store.dailyTimeLimit,
State.Current.Store.todayTimeLimit,
State.Current.Store.usedTime,
usageDate,
State.Current.Store.usage,
State.Current.Store.bedtime,
State.Current.Store.waketime,
syncAuthor = syncAuthor is null ? State.Current.Store.authKey : syncAuthor
});
var apiResponse = await ApiCall<SyncResult>(
"sync/" + State.Current.Store.uuid.ToString(), HttpMethod.Post,
new StringContent(json, System.Text.Encoding.UTF8, "application/json"),
State.Current.Store.authKey
);
if (apiResponse.success)
{
if (apiResponse.result.accepted)
{
// Sync was successful, no changes needed
if (apiResponse.result.delta is DeltaState delta)
{
// Server must have provided us with an authKey
if (delta.authKey is Guid authkey)
{
State.Current.Store.authKey = authkey;
await OS.Current.SaveState();
Console.WriteLine("Recieved new authKey");
}
else
{
string responseBody = await apiResponse.response.Content.ReadAsStringAsync();
throw new Exception("Unhandled delta\n" + responseBody);
}
}
return true;
}
else
{
// Sync was rejected
if (apiResponse.result.error != null)
{
Console.WriteLine($"Sync failed: {apiResponse.result.error}");
return false;
}
else if (apiResponse.result.delta != null)
{
Console.WriteLine("Accepting alternative State.Current.Store from server");
State.Current.AcceptDelta(apiResponse.result.delta);
await OS.Current.SaveState();
// Sync again with the foreign syncAuthor id to acknowledge the new state
return await Sync(apiResponse.result.delta.syncAuthor);
}
}
}
#if DEBUG
else
{
Console.WriteLine(json);
}
#endif
return false;
}
public async Task<bool> Deauth()
{
// Request the server deletes all client data
var apiResult = await ApiCall<DeauthResult>(
"deauth/" + State.Current.Store.uuid.ToString(), HttpMethod.Delete, null, State.Current.Store.authKey
);
return apiResult.success;
}
public async Task<UpdateCheckResult> UpdateCheck()
{
// Request the server deletes all client data
var apiResult = await ApiCall<UpdateCheckResult>("update", HttpMethod.Get, null, null);
if(apiResult.success)
return apiResult.result;
else return new UpdateCheckResult { version = "0.0.0" };
}
}
public static class API
{
public static readonly AppAPI Current = new();
}
}