コード例 #1
0
        private async Task <IEnumerable <QueryResult> > QueryWikipediaAsync(string baseUrl, string searchTerm, CancellationToken ct)
        {
            var requestUrl = $"{baseUrl}{CommonUrlPath}{Encode(searchTerm)}";
            var response   = await _httpClient.GetAsync(requestUrl, ct);

            ct.ThrowIfCancellationRequested();

            if (!response.IsSuccessStatusCode)
            {
                return(Enumerable.Empty <QueryResult>());
            }

            var jsonContent = await response.Content.ReadAsStringAsync();

            using (var textReader = new StringReader(jsonContent))
                using (var jsonReader = new JsonTextReader(textReader))
                {
                    try
                    {
                        var wikiRespose = _serializer.Deserialize <WikipediaResponse>(jsonReader);
                        _logger.Information("The search phrase {term} resulted in {suggestionCount} suggestions.", wikiRespose.Term, wikiRespose.Suggestions.Count);
                        return(WikipediaResult.Create(wikiRespose));
                    }
                    catch (Exception e)
                    {
                        _logger.Error(e, "An unhandled error occured when deserializing response.");
                        throw;
                    }
                    finally
                    {
                        response.Dispose();
                    }
                }
        }
コード例 #2
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);
            }
        }
コード例 #3
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);
        }