public PSAutoCompleteResourceV3(DataClient client, ServiceIndexResourceV3 serviceIndex, RegistrationResourceV3 regResource) : base() { _regResource = regResource; _serviceIndex = serviceIndex; _client = client; }
public override async Task <Tuple <bool, INuGetResource> > TryCreate(SourceRepository source, CancellationToken token) { SearchLatestResourceV3 curResource = null; ServiceIndexResourceV3 serviceIndex = await source.GetResourceAsync <ServiceIndexResourceV3>(token); if (serviceIndex != null) { var rawSearch = await source.GetResourceAsync <RawSearchResourceV3>(token); if (rawSearch != null) { curResource = new SearchLatestResourceV3(rawSearch); } } return(new Tuple <bool, INuGetResource>(curResource != null, curResource)); }
public override async Task <Tuple <bool, INuGetResource> > TryCreate(SourceRepository source, CancellationToken token) { RawSearchResourceV3 curResource = null; ServiceIndexResourceV3 serviceIndex = await source.GetResourceAsync <ServiceIndexResourceV3>(); if (serviceIndex != null) { var endpoints = serviceIndex[ServiceTypes.SearchQueryService].ToArray(); if (endpoints.Length > 0) { var messageHandlerResource = await source.GetResourceAsync <HttpHandlerResource>(token); // construct a new resource curResource = new RawSearchResourceV3(messageHandlerResource.MessageHandler, endpoints); } } return(new Tuple <bool, INuGetResource>(curResource != null, curResource)); }
// TODO: refresh the file when it gets old public override async Task<Tuple<bool, INuGetResource>> TryCreate(SourceRepository source, CancellationToken token) { ServiceIndexResourceV3 index = null; string url = source.PackageSource.Source; // the file type can easily rule out if we need to request the url if (url.EndsWith(".json", StringComparison.OrdinalIgnoreCase)) { // check the cache before downloading the file if (!_cache.TryGetValue(url, out index)) { var messageHandlerResource = await source.GetResourceAsync<HttpHandlerResource>(token); DataClient client = new DataClient(messageHandlerResource.MessageHandler); JObject json = await client.GetJObjectAsync(new Uri(url), token); if (json != null) { // Use SemVer instead of NuGetVersion, the service index should always be // in strict SemVer format SemanticVersion version = null; var status = json.Value<string>("version"); if (status != null && SemanticVersion.TryParse(status, out version)) { if (version.Major == 3) { index = new ServiceIndexResourceV3(json, DateTime.UtcNow); } } } } // cache the value even if it is null to avoid checking it again later _cache.TryAdd(url, index); } return new Tuple<bool, INuGetResource>(index != null, index); }
// TODO: refresh the file when it gets old public override async Task <Tuple <bool, INuGetResource> > TryCreate(SourceRepository source, CancellationToken token) { ServiceIndexResourceV3 index = null; string url = source.PackageSource.Source; // the file type can easily rule out if we need to request the url if (url.EndsWith(".json", StringComparison.OrdinalIgnoreCase)) { // check the cache before downloading the file if (!_cache.TryGetValue(url, out index)) { var messageHandlerResource = await source.GetResourceAsync <HttpHandlerResource>(token); DataClient client = new DataClient(messageHandlerResource.MessageHandler); JObject json = await client.GetJObjectAsync(new Uri(url), token); if (json != null) { // Use SemVer instead of NuGetVersion, the service index should always be // in strict SemVer format SemanticVersion version = null; var status = json.Value <string>("version"); if (status != null && SemanticVersion.TryParse(status, out version)) { if (version.Major == 3) { index = new ServiceIndexResourceV3(json, DateTime.UtcNow); } } } } // cache the value even if it is null to avoid checking it again later _cache.TryAdd(url, index); } return(new Tuple <bool, INuGetResource>(index != null, index)); }
// TODO: refresh the file when it gets old public override async Task <Tuple <bool, INuGetResource> > TryCreate(SourceRepository source, CancellationToken token) { ServiceIndexResourceV3 index = null; var url = source.PackageSource.Source; // the file type can easily rule out if we need to request the url if (source.PackageSource.ProtocolVersion == 3 || (source.PackageSource.IsHttp && url.EndsWith(".json", StringComparison.OrdinalIgnoreCase))) { ServiceIndexCacheInfo cacheInfo; // check the cache before downloading the file if (!_cache.TryGetValue(url, out cacheInfo) || UtcNow - cacheInfo.CachedTime > MaxCacheDuration) { var messageHandlerResource = await source.GetResourceAsync <HttpHandlerResource>(token); var client = new DataClient(messageHandlerResource.MessageHandler); JObject json; try { json = await client.GetJObjectAsync(new Uri(url), token); } catch (JsonReaderException) { _cache.TryAdd(url, new ServiceIndexCacheInfo { CachedTime = UtcNow }); return(new Tuple <bool, INuGetResource>(false, null)); } catch (HttpRequestException) { _cache.TryAdd(url, new ServiceIndexCacheInfo { CachedTime = UtcNow }); return(new Tuple <bool, INuGetResource>(false, null)); } if (json != null) { // Use SemVer instead of NuGetVersion, the service index should always be // in strict SemVer format SemanticVersion version; JToken versionToken; if (json.TryGetValue("version", out versionToken) && versionToken.Type == JTokenType.String && SemanticVersion.TryParse((string)versionToken, out version) && version.Major == 3) { index = new ServiceIndexResourceV3(json, UtcNow); } } } else { index = cacheInfo.Index; } // cache the value even if it is null to avoid checking it again later _cache.TryAdd(url, new ServiceIndexCacheInfo { CachedTime = UtcNow, Index = index }); } return(new Tuple <bool, INuGetResource>(index != null, index)); }
public override async Task <Tuple <bool, INuGetResource> > TryCreate(SourceRepository source, CancellationToken token) { ServiceIndexResourceV3 index = null; ServiceIndexCacheInfo cacheInfo = null; var url = source.PackageSource.Source; // the file type can easily rule out if we need to request the url if (source.PackageSource.ProtocolVersion == 3 || (source.PackageSource.IsHttp && url.EndsWith(".json", StringComparison.OrdinalIgnoreCase))) { var utcNow = DateTime.UtcNow; var entryValidCutoff = utcNow.Subtract(MaxCacheDuration); // check the cache before downloading the file if (!_cache.TryGetValue(url, out cacheInfo) || entryValidCutoff > cacheInfo.CachedTime) { // Track if the semaphore needs to be released var release = false; try { await _semaphore.WaitAsync(token); release = true; token.ThrowIfCancellationRequested(); // check the cache again, another thread may have finished this one waited for the lock if (!_cache.TryGetValue(url, out cacheInfo) || entryValidCutoff > cacheInfo.CachedTime) { index = await GetServiceIndexResourceV3(source, utcNow, NullLogger.Instance, token); // cache the value even if it is null to avoid checking it again later var cacheEntry = new ServiceIndexCacheInfo { CachedTime = utcNow, Index = index }; // If the cache entry has expired it will already exist _cache.AddOrUpdate(url, cacheEntry, (key, value) => cacheEntry); } } finally { if (release) { _semaphore.Release(); } } } } if (index == null && cacheInfo != null) { index = cacheInfo.Index; } return(new Tuple <bool, INuGetResource>(index != null, index)); }
// TODO: refresh the file when it gets old public override async Task<Tuple<bool, INuGetResource>> TryCreate(SourceRepository source, CancellationToken token) { ServiceIndexResourceV3 index = null; var url = source.PackageSource.Source; // the file type can easily rule out if we need to request the url if (source.PackageSource.ProtocolVersion == 3 || (source.PackageSource.IsHttp && url.EndsWith(".json", StringComparison.OrdinalIgnoreCase))) { ServiceIndexCacheInfo cacheInfo; // check the cache before downloading the file if (!_cache.TryGetValue(url, out cacheInfo) || UtcNow - cacheInfo.CachedTime > MaxCacheDuration) { var messageHandlerResource = await source.GetResourceAsync<HttpHandlerResource>(token); var client = new DataClient(messageHandlerResource.MessageHandler); JObject json; try { json = await client.GetJObjectAsync(new Uri(url), token); } catch (JsonReaderException) { _cache.TryAdd(url, new ServiceIndexCacheInfo { CachedTime = UtcNow }); return new Tuple<bool, INuGetResource>(false, null); } catch (HttpRequestException) { _cache.TryAdd(url, new ServiceIndexCacheInfo { CachedTime = UtcNow }); return new Tuple<bool, INuGetResource>(false, null); } if (json != null) { // Use SemVer instead of NuGetVersion, the service index should always be // in strict SemVer format SemanticVersion version; JToken versionToken; if (json.TryGetValue("version", out versionToken) && versionToken.Type == JTokenType.String && SemanticVersion.TryParse((string)versionToken, out version) && version.Major == 3) { index = new ServiceIndexResourceV3(json, UtcNow); } } } else { index = cacheInfo.Index; } // cache the value even if it is null to avoid checking it again later _cache.TryAdd(url, new ServiceIndexCacheInfo { CachedTime = UtcNow, Index = index }); } return new Tuple<bool, INuGetResource>(index != null, index); }