Beispiel #1
0
        public async Task <IActionResult> CheckSeo([FromQuery] SearchEnginesEnum searchEngine, [FromQuery] string searchToken, [FromQuery] string url)
        {
            var result = await _service.FindOccurrences(searchEngine, searchToken, url);

            if (result.Success)
            {
                return(new OkObjectResult(result.Value));
            }

            return(result.Type == ErrorTypesEnum.BadRequest
                ? new BadRequestObjectResult(new HttpErrorResponse {
                Errors = result.Errors.ToList()
            })
                : new ObjectResult(new HttpErrorResponse {
                Errors = result.Errors.ToList()
            })
            {
                StatusCode = (int)HttpStatusCode.InternalServerError
            });
        }
Beispiel #2
0
        public async Task <IResult <ParseResult> > FindOccurrences(SearchEnginesEnum searchEngine, string searchEngineSearchToken, string searchForUrl)
        {
            if (string.IsNullOrEmpty(searchEngineSearchToken))
            {
                return(new Result <ParseResult>("Please enter a search token", ErrorTypesEnum.BadRequest));
            }

            if (string.IsNullOrEmpty(searchForUrl))
            {
                return(new Result <ParseResult>("Please enter the website url", ErrorTypesEnum.BadRequest));
            }

            try
            {
                var ret = await _cacheService.GetOrSet($"{searchEngine}:{searchEngineSearchToken}:{searchForUrl}", async() =>
                {
                    var queryBuilder             = _context.ResolveKeyed <ISearchEngineQueryBuilder>(searchEngine);
                    var searchEngineResultParser = _context.ResolveKeyed <ISearchEngineResultParser>(searchEngine);
                    var url         = queryBuilder.GetUrl(searchEngineSearchToken);
                    var response    = await _httpHandler.GetAsync(url);
                    var content     = await response.Content.ReadAsStringAsync();
                    var parseResult = searchEngineResultParser.Process(content, searchForUrl);
                    return(parseResult);
                }, new CacheOptions
                {
                    LocalTimeout = CacheOptions.Normal
                });

                return(new Result <ParseResult>(ret));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Couldn't impress the CEO.");
                return(new Result <ParseResult>($"Sorry. An exception occurred. {e.Message}", ErrorTypesEnum.ServerError));
            }
        }