Example #1
0
        public async Task <SearchResponse> SearchAsync(string queryWord)
        {
            try
            {
                using (var httpClient = new HttpClient())
                {
                    string url = string.Format("{0}/search?queryword={1}", BaseApiUrl, queryWord);

                    var httpResponse = await httpClient.GetAsync(url);

                    if (httpResponse.StatusCode == HttpStatusCode.OK)
                    {
                        var responseJson = await httpResponse.Content.ReadAsStringAsync();

                        return(JsonConvert.DeserializeObject <SearchResponse>(responseJson));
                    }
                    else
                    {
                        string error = string.Format("Error occurred. Search action failed with status {0} for search word '{1}'", httpResponse.StatusCode, queryWord);
                        _logger.Error(error);
                        return(SearchResponse.FromError(error));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                return(SearchResponse.FromException(ex));
            }
        }
Example #2
0
        public async Task <SearchResponse> SearchAsync(SearchRequest request)
        {
            try
            {
                List <SearchResultItem> results = null;
                var response = new SearchResponse();

                string key = string.Format("SearchResults_{0}", HttpUtility.UrlEncode(request.QueryWord));
                response.QueryWord = request.QueryWord;

                if (false == _cache.TryGetValue(key, out results))
                {
                    var    enteredDate = DateTime.Now;
                    string error       = string.Empty;

                    foreach (var crawler in _crawlers)
                    {
                        var crawlingRequest = buildCrawlingRequest(request);
                        var crawlingResult  = await crawler.CrawleAsync(crawlingRequest);

                        if (crawlingResult == null)
                        {
                            error = string.Format("Error occurred while crawling {0} search engine.", crawler.SearchEngine);
                            _logger.Error(error);
                            return(SearchResponse.FromError(error));
                        }

                        results = new List <SearchResultItem>();
                        foreach (var item in crawlingResult.SearchResults)
                        {
                            item.EnteredDate = enteredDate;
                            var resultItem = await _repository.CreateAsync(item);

                            if (resultItem == null)
                            {
                                error = "Error occurred while saving search result item in DB";
                                _logger.Error(error);
                            }
                            else
                            {
                                results.Add(resultItem);
                            }
                        }

                        response.Results.AddRange(results);
                    }

                    var cacheEntryOptions = new MemoryCacheEntryOptions()
                                            .SetSlidingExpiration(TimeSpan.FromHours(1));

                    _cache.Set(key, response.Results, cacheEntryOptions);
                }
                else
                {
                    response.Results.AddRange(results);
                }

                return(response);
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                return(SearchResponse.FromException(ex));
            }
        }