Exemplo n.º 1
0
        protected override async Task QueryAsync(IQuery query, CancellationToken ct)
        {
            if (string.IsNullOrWhiteSpace(query.Argument))
            {
                var fallbacks = WikipediaResult.CreateFallback(_config.BaseUrls);
                Available(fallbacks);
                return;
            }

            var queryTasks = _clients
                             .Select(client => QueryWikipediaAsync(client, query.Argument, ct))
                             .ToList();
            await Task.WhenAll(queryTasks);

            ct.ThrowIfCancellationRequested();

            var results = queryTasks.SelectMany(q => q.Result.Suggestions);

            if (query is DefaultQuery)
            {
                results = results.Take(3);
            }

            if (results.Any() || query is DefaultQuery)
            {
                Available(WikipediaResult.Create(results));
            }
            else
            {
                var searchResults = WikipediaResult.CreateSearch(Encode(query.Argument), _config.BaseUrls);
                Available(searchResults);
            }
        }
Exemplo n.º 2
0
        public async Task <IEnumerable <QueryResult> > QueryAsync(Query query, CancellationToken ct)
        {
            if (string.IsNullOrWhiteSpace(query.Argument))
            {
                return(WikipediaResult.CreateFallback(_config.BaseUrls));
            }

            var queryTasks = new List <Task <IEnumerable <QueryResult> > >();

            foreach (var url in _config.BaseUrls)
            {
                var searchTask = QueryWikipediaAsync(url, query.Argument, ct);
                queryTasks.Add(searchTask);
            }
            await Task.WhenAll(queryTasks);

            var results = queryTasks.SelectMany(q => q.Result).ToList();

            if (!results.Any())
            {
                return(WikipediaResult.CreateSearch(Encode(query.Argument), _config.BaseUrls));
            }
            return(results);
        }