11// Copyright (c) .NET Foundation. All rights reserved.
22// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
34using System ;
45using System . Collections . Generic ;
56using System . Data . Entity ;
67using System . Globalization ;
8+ using System . Linq ;
9+ using System . Net . Http ;
10+ using System . Threading . Tasks ;
11+ using Newtonsoft . Json . Linq ;
12+ using NuGet . Services . Search . Client ;
13+ using NuGetGallery . Configuration ;
714
815namespace NuGetGallery
916{
1017 public interface IPackageIdsQuery
1118 {
12- IEnumerable < string > Execute (
19+ Task < IEnumerable < string > > Execute (
1320 string partialId ,
1421 bool ? includePrerelease = false ) ;
1522 }
1623
24+ public class AutocompleteServicePackageIdsQuery : IPackageIdsQuery
25+ {
26+ private readonly ServiceDiscoveryClient _serviceDiscoveryClient ;
27+ private readonly string _autocompleteServiceResourceType ;
28+ private readonly RetryingHttpClientWrapper _httpClient ;
29+
30+ public AutocompleteServicePackageIdsQuery ( IAppConfiguration configuration )
31+ {
32+ _serviceDiscoveryClient = new ServiceDiscoveryClient ( configuration . ServiceDiscoveryUri ) ;
33+ _autocompleteServiceResourceType = configuration . AutocompleteServiceResourceType ;
34+ _httpClient = new RetryingHttpClientWrapper ( new HttpClient ( ) ) ;
35+ }
36+
37+ public async Task < IEnumerable < string > > Execute ( string partialId , bool ? includePrerelease )
38+ {
39+ if ( partialId == null )
40+ {
41+ partialId = string . Empty ;
42+ }
43+
44+ var queryString = "take=30&q=" + Uri . EscapeUriString ( partialId ) ;
45+ if ( ! includePrerelease . HasValue )
46+ {
47+ queryString += "&prerelease=false" ;
48+ }
49+ else
50+ {
51+ queryString += "&prerelease=" + includePrerelease . Value ;
52+ }
53+
54+ var endpoints = await _serviceDiscoveryClient . GetEndpointsForResourceType ( _autocompleteServiceResourceType ) ;
55+ endpoints = endpoints . Select ( e => new Uri ( e + "?" + queryString ) ) . AsEnumerable ( ) ;
56+
57+ var result = await _httpClient . GetStringAsync ( endpoints ) ;
58+ var resultObject = JObject . Parse ( result ) ;
59+
60+ return resultObject [ "data" ] . Select ( entry => entry . ToString ( ) ) ;
61+ }
62+ }
63+
1764 public class PackageIdsQuery : IPackageIdsQuery
1865 {
1966 private const string PartialIdSqlFormat = @"SELECT TOP 30 pr.ID
@@ -37,24 +84,24 @@ public PackageIdsQuery(IEntitiesContext entities)
3784 _entities = entities ;
3885 }
3986
40- public IEnumerable < string > Execute (
87+ public Task < IEnumerable < string > > Execute (
4188 string partialId ,
4289 bool ? includePrerelease = false )
4390 {
4491 var dbContext = ( DbContext ) _entities ;
4592
4693 if ( String . IsNullOrWhiteSpace ( partialId ) )
4794 {
48- return dbContext . Database . SqlQuery < string > ( NoPartialIdSql ) ;
95+ return Task . FromResult ( dbContext . Database . SqlQuery < string > ( NoPartialIdSql ) ) ;
4996 }
5097
5198 var prereleaseFilter = String . Empty ;
5299 if ( ! includePrerelease . HasValue || ! includePrerelease . Value )
53100 {
54101 prereleaseFilter = "AND p.IsPrerelease = {1}" ;
55102 }
56- return dbContext . Database . SqlQuery < string > (
57- String . Format ( CultureInfo . InvariantCulture , PartialIdSqlFormat , prereleaseFilter ) , partialId + "%" , includePrerelease ?? false ) ;
103+ return Task . FromResult ( dbContext . Database . SqlQuery < string > (
104+ String . Format ( CultureInfo . InvariantCulture , PartialIdSqlFormat , prereleaseFilter ) , partialId + "%" , includePrerelease ?? false ) ) ;
58105 }
59106 }
60107}
0 commit comments