public async Task <GetDriveItemsResponse> GetDriveItemsAsync(GetDriveItemsRequest request) { if (_TryGetFromCache(request.GetCacheKey(), out GetDriveItemsResponse cachedResponse)) { return(cachedResponse); } else { if (_TryGetRequestUrlWithPaging(request, out string requestUrl)) { (bool tokenSuccess, string token) = await _TryGetTokenAsync(); if (tokenSuccess) { _http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token); string responseStr = await _http.GetStringAsync(requestUrl); var graphResponse = SimpleJson.SimpleJson.DeserializeObject <GraphResponse <DriveItem[]> >(responseStr, new SimpleJson.DataContractJsonSerializerStrategy()); var response = new GetDriveItemsResponse(graphResponse.Value, _GetSkipToken(graphResponse.NextLink)); _AddToCache(request, response); return(response); } } return(null); } }
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 bool _TryGetRequestUrlWithPaging(GetDriveItemsRequest request, out string requestUrl) { if (_TryGetRequestUrlForChildren(request?.Path, out requestUrl)) { int pageSize = Math.Max(request.PageSize, GetDriveItemsRequest.MinimumPageSize); requestUrl += "?$top=" + pageSize; if (!string.IsNullOrEmpty(request.SkipToken)) { requestUrl += "&$skipToken=" + WebUtility.UrlEncode(request.SkipToken); } return(true); } else { requestUrl = null; } return(false); }