/
GraphService.cs
668 lines (592 loc) · 28.2 KB
/
GraphService.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using W8lessLabs.GraphAPI.Logging;
namespace W8lessLabs.GraphAPI
{
public class GraphService : IGraphService
{
internal const string GraphEndpoint = "https://graph.microsoft.com/v1.0";
internal const string GraphEndpoint_Me = GraphEndpoint+ "/me";
internal const string GraphEndpoint_Drives = GraphEndpoint + "/drives";
internal const string GraphEndpoint_DriveRoot = GraphEndpoint+ "/me/drive/root";
internal const string GraphEndpoint_DriveSpecial = GraphEndpoint + "/me/drive/special";
internal const string GraphEndpoint_DriveItems = GraphEndpoint + "/me/drive/items";
internal const string GraphEndpoint_DriveRootChildren = GraphEndpoint + "/me/drive/root/children";
internal const string GraphEndpoint_Delta = GraphEndpoint + "/me/drive/root/delta";
internal const string SkipTokenParam = "$skipToken=";
internal const string DeltaTokenParam = "token=";
internal const int MaxSingleFileUploadSize = 1048576 * 4; // 4 MiB
private readonly IAuthTokenProvider _authProvider;
private readonly IHttpService _http;
private readonly IJsonSerializer _json;
private readonly ILogger _logger;
// Tokens cache
private Dictionary<string, AccountToken> _accountTokens;
// keep a small LRU queue of drive items
private LinkedList<LRUCacheEntry<GetDriveItemsResponse>> _itemsCache;
private const int CacheLimit = 30;
private const int MaxCacheAgeSeconds = 120;
public GraphService(IAuthTokenProvider authProvider, IHttpService http, IJsonSerializer json, ILoggerProvider loggerProvider = null)
{
_authProvider = authProvider ?? throw new ArgumentNullException(nameof(authProvider));
_http = http ?? throw new ArgumentNullException(nameof(http));
_json = json ?? throw new ArgumentNullException(nameof(json));
_itemsCache = new LinkedList<LRUCacheEntry<GetDriveItemsResponse>>();
_accountTokens = new Dictionary<string, AccountToken>();
if (loggerProvider is null)
_logger = NullLogger.Instance;
else
_logger = loggerProvider.GetLogger();
}
private async Task<(bool tokenSuccess, string token)> _TryGetTokenAsync(string accountId)
{
bool tokenSuccess = false;
string token = null;
DateTimeOffset tokenExpires = default;
if(_accountTokens.TryGetValue(accountId, out AccountToken cachedToken))
{
tokenSuccess = true;
token = cachedToken.Token;
tokenExpires = cachedToken.TokenExpires;
}
if(token == null || tokenExpires == default || DateTimeOffset.Now > (tokenExpires - TimeSpan.FromSeconds(30)))
{
GraphTokenResult result = await _authProvider.GetTokenAsync(accountId).ConfigureAwait(false);
tokenSuccess = result.Success;
if(result.Success)
{
token = result.AccessToken;
tokenExpires = result.Expires;
// cache the token
_accountTokens[accountId] = new AccountToken(token, tokenExpires);
}
}
return (tokenSuccess, token);
}
private string _GetRequestUrlForPath(string path, SpecialFolder? specialFolder = null)
{
string driveRoot = GraphEndpoint_DriveRoot;
if (specialFolder.HasValue)
driveRoot = GraphEndpoint_DriveSpecial + "/" + specialFolder.Value.ToFolderName();
if (!(string.IsNullOrEmpty(path) || path == "/"))
return driveRoot + ":" + Uri.EscapeUriString(path);
else
return driveRoot;
}
private string _GetRequestUrlForChildren(string path, SpecialFolder? specialFolder = null)
{
if (string.IsNullOrEmpty(path) || path == "/")
{
if (specialFolder.HasValue)
return GraphEndpoint_DriveSpecial + "/" + specialFolder.Value.ToFolderName() + "/children";
else
return GraphEndpoint_DriveRootChildren;
}
else
{
if(specialFolder.HasValue)
return GraphEndpoint_DriveSpecial + "/" + specialFolder.Value.ToFolderName() + ":" + Uri.EscapeUriString(path) + ":/children";
else
return GraphEndpoint_DriveRoot + ":" + Uri.EscapeUriString(path) + ":/children";
}
}
private string _GetRequestUrlForContent(string path, SpecialFolder? specialFolder = null)
{
if (string.IsNullOrEmpty(path) || path == "/")
{
if (specialFolder.HasValue)
return GraphEndpoint_DriveSpecial + "/" + specialFolder.Value.ToFolderName() + "/content";
else
return GraphEndpoint_DriveRootChildren;
}
else
{
if (specialFolder.HasValue)
return GraphEndpoint_DriveSpecial + "/" + specialFolder.Value.ToFolderName() + ":" + Uri.EscapeUriString(path) + ":/content";
else
return GraphEndpoint_DriveRoot + ":" + Uri.EscapeUriString(path) + ":/content";
}
//if (string.IsNullOrEmpty(path) || path == "/")
// return GraphEndpoint_DriveRootChildren;
//else
// return GraphEndpoint_DriveRoot + ":" + Uri.EscapeUriString(path) + ":/content";
}
private string _GetRequestUrlForPermissions(string path, SpecialFolder? specialFolder = null)
{
string driveRoot = GraphEndpoint_DriveRoot;
if (specialFolder.HasValue)
driveRoot = GraphEndpoint_DriveSpecial + "/" + specialFolder.Value.ToFolderName();
if (string.IsNullOrEmpty(path) || path == "/")
return driveRoot + "/permissions";
else
return driveRoot + ":" + Uri.EscapeUriString(path) + ":/permissions";
}
private bool _TryGetRequestUrlForItemId(string driveItemId, out string requestUrl)
{
if (string.IsNullOrEmpty(driveItemId))
{
requestUrl = null;
return false;
}
else
requestUrl = GraphEndpoint_DriveItems + "/" + Uri.EscapeUriString(driveItemId);
return true;
}
private bool _TryGetRequestUrlForPermissionsById(string driveItemId, out string requestUrl)
{
if (string.IsNullOrEmpty(driveItemId))
{
requestUrl = string.Empty;
return false;
}
else
requestUrl = GraphEndpoint_DriveItems + "/" + Uri.EscapeUriString(driveItemId) + "/permissions";
return true;
}
private string _GetRequestUrlWithPaging(GetDriveItemsRequest request)
{
string requestUrl = _GetRequestUrlForChildren(request?.Path, request?.SpecialFolder);
int pageSize = Math.Max(request.PageSize, GetDriveItemsRequest.MinimumPageSize);
requestUrl += "?$top=" + pageSize;
if (!string.IsNullOrEmpty(request.SkipToken))
requestUrl += "&$skipToken=" + WebUtility.UrlEncode(request.SkipToken);
return requestUrl;
}
private string _GetSkipToken(string nextLink)
{
string skipToken = null;
if(!string.IsNullOrEmpty(nextLink))
{
int index = nextLink.IndexOf(SkipTokenParam, StringComparison.OrdinalIgnoreCase);
if(index != -1)
{
skipToken = nextLink.Substring(index + SkipTokenParam.Length);
index = skipToken.IndexOf('&');
if(index != -1) // there's another parameter to trim off...
skipToken = skipToken.Substring(0, index);
}
}
return skipToken;
}
private HttpServiceHeadersScope _WithAuthHeader(string token) =>
_http.WithHeaders(("Authorization", "Bearer " + token));
private T _UnwrapResponse<T>(HttpResponseValue<T> response)
{
if (response.Success)
return response.Value;
else
{
_logger.Error("Request Failed - Request URI: {0} - Error Code: {1} - Response Message: {2}",
response.RequestUri,
response.ErrorMessage?.Code,
response.ErrorMessage?.Message);
return default;
}
}
public async Task<GraphUser> GetMeAsync(string accountId)
{
(bool tokenSuccess, string token) = await _TryGetTokenAsync(accountId).ConfigureAwait(false);
if (tokenSuccess)
using (_WithAuthHeader(token))
return _UnwrapResponse(await _http.GetJsonAsync<GraphUser>(GraphEndpoint_Me).ConfigureAwait(false));
else
return null;
}
public async Task<DriveItem> GetDriveItemAsync(string accountId, string itemPath, SpecialFolder? specialFolder = null)
{
if(!string.IsNullOrEmpty(itemPath))
{
string requestUrl = _GetRequestUrlForPath(itemPath, specialFolder);
(bool tokenSuccess, string token) = await _TryGetTokenAsync(accountId).ConfigureAwait(false);
if (tokenSuccess)
using (_WithAuthHeader(token))
return _UnwrapResponse(await _http.GetJsonAsync<DriveItem>(requestUrl).ConfigureAwait(false));
}
return null;
}
public async Task<DriveItem> GetDriveItemByIdAsync(string accountId, string driveItemId)
{
if (_TryGetRequestUrlForItemId(driveItemId, out string requestUrl))
{
(bool tokenSuccess, string token) = await _TryGetTokenAsync(accountId).ConfigureAwait(false);
if (tokenSuccess)
using (_WithAuthHeader(token))
return _UnwrapResponse(await _http.GetJsonAsync<DriveItem>(requestUrl).ConfigureAwait(false));
}
return null;
}
public async Task<GetDriveItemsResponse> GetDriveItemsAsync(string accountId, GetDriveItemsRequest request)
{
if (request != null)
{
if (request.AllowCache && _TryGetFromCache(request.GetCacheKey(), out GetDriveItemsResponse cachedResponse))
return cachedResponse;
else
{
string requestUrl = _GetRequestUrlWithPaging(request);
(bool tokenSuccess, string token) = await _TryGetTokenAsync(accountId).ConfigureAwait(false);
if (tokenSuccess)
{
using (_WithAuthHeader(token))
{
var response = await _http.GetJsonAsync<GraphResponse<DriveItem[]>>(requestUrl).ConfigureAwait(false);
if (response.Success)
{
var graphResponse = response.Value;
var driveItems = new GetDriveItemsResponse(graphResponse.Value, _GetSkipToken(graphResponse.NextLink));
_AddToCache(request, driveItems);
return driveItems;
}
else
return default;
}
}
}
}
return null;
}
public async Task<GetDriveItemsDeltaResponse> GetDriveItemsDeltaAsync(string accountId, string deltaOrNextLink = null)
{
string requestUrl = GraphEndpoint_DriveRoot + "/delta";
if (!string.IsNullOrEmpty(deltaOrNextLink))
requestUrl = deltaOrNextLink;
(bool tokenSuccess, string token) = await _TryGetTokenAsync(accountId).ConfigureAwait(false);
if (tokenSuccess)
{
using (_WithAuthHeader(token))
{
var response = await _http.GetJsonAsync<GraphResponse<DriveItem[]>>(requestUrl).ConfigureAwait(false);
if (response.Success)
{
var graphResponse = response.Value;
var driveItems = new GetDriveItemsDeltaResponse(graphResponse.Value, graphResponse.NextLink, graphResponse.DeltaLink);
return driveItems;
}
else
return default;
}
}
return null;
}
public string GetDeltaLinkFromToken(string deltaToken)
{
if (deltaToken is null) throw new ArgumentNullException(nameof(deltaToken));
return GraphEndpoint_Delta + "?" + DeltaTokenParam + Uri.EscapeDataString(deltaToken);
}
public async Task<int> GetChildItemsCountAsync(string accountId, string path, SpecialFolder? specialFolder = null)
{
int count = 0;
string requestUrl = _GetRequestUrlForPath(path, specialFolder);
requestUrl += "?expand=children(select=id)";
(bool tokenSuccess, string token) = await _TryGetTokenAsync(accountId).ConfigureAwait(false);
if (tokenSuccess)
{
using (_WithAuthHeader(token))
{
var response = await _http.GetJsonAsync<DriveItem>(requestUrl).ConfigureAwait(false);
if(response.Success)
count = response.Value?.IsFolder() == true ? response.Value.Folder.ChildCount : 0;
}
}
return count;
}
public async Task<IEnumerable<Permission>> GetPermissionsAsync(string accountId, string path, SpecialFolder? specialFolder = null)
{
string requestUrl = _GetRequestUrlForPermissions(path, specialFolder);
(bool tokenSuccess, string token) = await _TryGetTokenAsync(accountId).ConfigureAwait(false);
if (tokenSuccess)
{
using (_WithAuthHeader(token))
{
return _UnwrapResponse(
await _http.GetJsonAsync<GraphResponse<Permission[]>>(requestUrl).ConfigureAwait(false))?.Value;
}
}
return Array.Empty<Permission>();
}
public async Task<IEnumerable<Permission>> GetPermissionsByIdAsync(string accountId, string driveItemId)
{
if (_TryGetRequestUrlForPermissionsById(driveItemId, out string requestUrl))
{
(bool tokenSuccess, string token) = await _TryGetTokenAsync(accountId).ConfigureAwait(false);
if (tokenSuccess)
{
using (_WithAuthHeader(token))
{
return _UnwrapResponse(
await _http.GetJsonAsync<GraphResponse<Permission[]>>(requestUrl).ConfigureAwait(false))?.Value;
}
}
}
return Array.Empty<Permission>();
}
public async Task<Permission> CreateSharingLinkAsync(string accountId, string driveItemId, SharingLinkTypeOptions type, SharingLinkScopeOptions scope)
{
if (_TryGetRequestUrlForItemId(driveItemId, out string requestUrl))
{
requestUrl += "/createLink";
(bool tokenSuccess, string token) = await _TryGetTokenAsync(accountId).ConfigureAwait(false);
if (tokenSuccess)
{
using (_WithAuthHeader(token))
return _UnwrapResponse(
// POST /me/drive/items/{itemId}/createLink
await _http.PostJsonAsync<Permission>(requestUrl,
_json.Serialize(new CreateSharingLinkRequest(type, scope))).ConfigureAwait(false));
}
}
return null;
}
public async Task<PermissionUpdateResponse> AddPermissionAsync(string accountId, string driveItemId, SharingInvitationRequest request)
{
if (_TryGetRequestUrlForItemId(driveItemId, out string requestUrl))
{
requestUrl += "/invite";
(bool tokenSuccess, string token) = await _TryGetTokenAsync(accountId).ConfigureAwait(false);
if (tokenSuccess)
{
using (_WithAuthHeader(token))
{
// POST /me/drive/items/{item-id}/invite
var response = await _http.PostJsonAsync<SharingInvitationResponse>(requestUrl,
_json.Serialize(request)).ConfigureAwait(false);
// send through Unwrap as well for logging...
var value = _UnwrapResponse(response);
return new PermissionUpdateResponse(response.Success, value?.Value.FirstOrDefault(), response.ErrorMessage);
}
}
}
return null;
}
public async Task<PermissionUpdateResponse> UpdatePermissionAsync(string accountId, string driveItemId, string permissionId, params PermissionRoleOptions[] roles)
{
if (_TryGetRequestUrlForItemId(driveItemId, out string requestUrl))
{
requestUrl += "/permissions/" + Uri.EscapeUriString(permissionId);
(bool tokenSuccess, string token) = await _TryGetTokenAsync(accountId).ConfigureAwait(false);
if (tokenSuccess)
{
using (_WithAuthHeader(token))
{
// PATCH /me/drive/items/{item-id}/permissions/{perm-id}
var response = await _http.PatchJsonAsync<Permission>(requestUrl,
_json.Serialize(new
{
roles = roles.Select(r => r.AsString()).ToArray()
})).ConfigureAwait(false);
// send through Unwrap as well for logging...
var value = _UnwrapResponse(response);
return new PermissionUpdateResponse(response.Success, response.Value, response.ErrorMessage);
}
}
}
return null;
}
public async Task<DriveItem> CreateFolderAsync(string accountId, string path, string newFolderName, SpecialFolder? specialFolder = null)
{
string requestUrl = _GetRequestUrlForChildren(path, specialFolder);
(bool tokenSuccess, string token) = await _TryGetTokenAsync(accountId).ConfigureAwait(false);
if (tokenSuccess)
{
using (_WithAuthHeader(token))
{
var createFolder = new
{
Name = newFolderName,
Folder = new Folder()
};
return _UnwrapResponse(await _http.PostJsonAsync<DriveItem>(requestUrl, _json.Serialize(createFolder)).ConfigureAwait(false));
}
}
return null;
}
private async Task<DriveItem> _UploadSmallFileAsync(string accountId, FileUploadRequest request, Stream fileContent)
{
string requestUrl = _GetRequestUrlForContent(request.GetFullFilePath(), request.SpecialFolder);
(bool tokenSuccess, string token) = await _TryGetTokenAsync(accountId).ConfigureAwait(false);
if (tokenSuccess)
{
using (_WithAuthHeader(token))
return _UnwrapResponse(await _http.PutBinaryAsync<DriveItem>(requestUrl, fileContent).ConfigureAwait(false));
}
return default;
}
public async Task<DriveItem> _UploadLargeFileAsync(string accountId, FileUploadRequest request, Stream fileContent)
{
string createSessionUrl = GraphEndpoint_DriveRoot;
if (request.SpecialFolder.HasValue)
createSessionUrl = GraphEndpoint_DriveSpecial + "/" + request.SpecialFolder.Value.ToFolderName();
createSessionUrl += ":" + Uri.EscapeUriString(request.GetFullFilePath()) + ":/createUploadSession";
(bool tokenSuccess, string token) = await _TryGetTokenAsync(accountId).ConfigureAwait(false);
if (tokenSuccess)
{
using (_WithAuthHeader(token))
{
string replace = request.Overwrite ? "replace" : "fail";
string fileName = _json.Serialize(request.FileName);
//string description = _json.Serialize(request.Description); // Only supported on ODrive Personal
//string fileSystemInfo = @"{ ""odata.type"": ""microsoft.graph.fileSystemInfo"" }"; // TODO - support adding FSInfo
string createSessionRequest = $@"{{
""item"": {{
""@microsoft.graph.conflictBehavior"": ""{replace}"",
""name"": {fileName}
}}
}}";
var createSessionResponse = _UnwrapResponse(await _http.PostJsonAsync<CreateUploadSessionResponse>(createSessionUrl, createSessionRequest).ConfigureAwait(false));
if (createSessionResponse != null)
{
using (var session = new FileUploadSession(request,
createSessionResponse.UploadUrl,
createSessionResponse.ExpirationDateTime,
_http,
_json))
{
await session.UploadAsync(fileContent, request.FileSize).ConfigureAwait(false);
}
}
}
}
return default;
}
public async Task<DriveItem> UploadFileAsync(string accountId, FileUploadRequest request, Stream fileContent)
{
if (string.IsNullOrEmpty(request.FilePath))
throw new ArgumentException("FilePath", "A File Path is required.");
if (string.IsNullOrEmpty(request.FileName))
throw new ArgumentException("FileName", "A File Name is required.");
return await _UploadLargeFileAsync(accountId, request, fileContent).ConfigureAwait(false);
}
public async Task<Stream> DownloadFileAsync(string accountId, string path, (int start, int end) range = default, SpecialFolder? specialFolder = null)
{
string requestUrl = _GetRequestUrlForContent(path, specialFolder);
(bool tokenSuccess, string token) = await _TryGetTokenAsync(accountId).ConfigureAwait(false);
if (tokenSuccess)
{
using (_WithAuthHeader(token))
{
(string header, string value)[] contentHeaders = default;
if (range.end > 0 && range.end > range.start)
contentHeaders = new[] { ("Range", string.Format("bytes={0}-{1}", range.start, range.end)) };
return _UnwrapResponse(await _http.GetStreamAsync(requestUrl, contentHeaders).ConfigureAwait(false));
}
}
return Stream.Null;
}
public async Task<DriveItem> UpdateItemByIdAsync(string accountId, string driveItemId, Dictionary<string, object> updateValues)
{
if (_TryGetRequestUrlForItemId(driveItemId, out string requestUrl))
{
(bool tokenSuccess, string token) = await _TryGetTokenAsync(accountId).ConfigureAwait(false);
if (tokenSuccess)
using (_WithAuthHeader(token))
// PATCH /me/drive/items/{item-id}
return _UnwrapResponse(await _http.PatchJsonAsync<DriveItem>(requestUrl, _json.Serialize(updateValues)).ConfigureAwait(false));
}
return null;
}
public async Task<bool> DeleteItemByIdAsync(string accountId, string driveItemId)
{
if (_TryGetRequestUrlForItemId(driveItemId, out string requestUrl))
{
(bool tokenSuccess, string token) = await _TryGetTokenAsync(accountId).ConfigureAwait(false);
if (tokenSuccess)
{
using (_WithAuthHeader(token))
{
// DELETE /me/drive/items/{item-id}
var response = await _http.DeleteAsync(requestUrl).ConfigureAwait(false);
return response.Success;
}
}
}
return false;
}
public async Task<bool> DeletePermissionByIdAsync(string accountId, string driveItemId, string permissionId)
{
if (_TryGetRequestUrlForItemId(driveItemId, out string requestUrl))
{
if (!string.IsNullOrEmpty(permissionId))
{
requestUrl += "/permissions/" + Uri.EscapeUriString(permissionId);
(bool tokenSuccess, string token) = await _TryGetTokenAsync(accountId).ConfigureAwait(false);
if (tokenSuccess)
{
using (_WithAuthHeader(token))
{
// DELETE /me/drive/items/{item-id}/permissions/{perm-id}
var response = await _http.DeleteAsync(requestUrl).ConfigureAwait(false);
return response.Success;
}
}
}
}
return false;
}
private bool _TryGetFromCache(string cacheKey, out GetDriveItemsResponse cachedResponse)
{
var entry = _itemsCache.FirstOrDefault(i => i.Key == cacheKey);
bool found = false;
if (entry != null)
{
if ((DateTime.Now - entry.Created).TotalSeconds > MaxCacheAgeSeconds)
{
_itemsCache.Remove(entry);
cachedResponse = default;
}
else
{
entry.LastUsed = DateTime.Now;
cachedResponse = entry.Value;
found = true;
}
}
else
cachedResponse = default;
return found;
}
private void _AddToCache(GetDriveItemsRequest request, GetDriveItemsResponse response)
{
if (!_TryGetFromCache(request.GetCacheKey(), out GetDriveItemsResponse cachedResponse))
{
if (_itemsCache.Count > CacheLimit)
{
var prune = _itemsCache.OrderBy(c => c.LastUsed).First();
_itemsCache.Remove(prune);
}
_itemsCache.AddFirst(new LRUCacheEntry<GetDriveItemsResponse>()
{
Key = request.GetCacheKey(),
Value = response,
LastUsed = DateTime.Now,
Created = DateTime.Now
});
}
}
private class AccountToken
{
public AccountToken(string token, DateTimeOffset tokenExpires)
{
Token = token;
TokenExpires = tokenExpires;
}
public string Token;
public DateTimeOffset TokenExpires;
}
private class LRUCacheEntry<T>
{
public string Key { get; set; }
public DateTime LastUsed { get; set; }
public DateTime Created { get; set; }
public T Value { get; set; }
}
private class CreateUploadSessionResponse
{
public string UploadUrl { get; set; }
public DateTimeOffset ExpirationDateTime { get; set; }
}
}
}