Beispiel #1
0
 public void Validate(CodingChallengeRequestModel model, out List <string> messages, out bool hardStop)
 {
     hardStop = false;
     messages = new List <string> {
         $"Info: Image Type Set to {model.Type.ToString().ToLower()}"
     };
 }
Beispiel #2
0
        private HttpResponseMessage CodingChallengeResponseModel(CodingChallengeRequestModel model, PixabayResponseModel pixabayResponse, OxfordResponseModel oxfordResponse)
        {
            var validationMessages = new List <string>();

            if (_validationServices.Any())
            {
                var stop = false;
                foreach (var validationService in _validationServices)
                {
                    validationService.Validate(model, out var messages, out var hardStop);

                    if (messages.Any())
                    {
                        validationMessages.AddRange(messages);
                    }

                    stop = hardStop || stop;
                }

                if (stop)
                {
                    return(Request.CreateResponse(HttpStatusCode.ExpectationFailed,
                                                  new { Ok = false, Messages = validationMessages.OrderBy(p => p).ToList(), Request = model }));
                }
            }

            return(Request.CreateResponse(HttpStatusCode.OK,
                                          new
            {
                Images = pixabayResponse,
                Words = oxfordResponse,
                Messages = validationMessages,
                Request = model
            }));
        }
Beispiel #3
0
        public async Task <OxfordResponseModel> OxfordDictionaryAPI(CodingChallengeRequestModel emailRequest, bool testing = false)
        {
            try
            {
                SetupContext();

                //Would have liked to use string interpolation - but it didn't work.
                var baseUrl = _apiConfigurationHelper.APIConfiguration.OxfordDictionaryAPI.UrlFormat.Replace(QUERY_REPLACE, emailRequest.Query.Split(' ').FirstOrDefault());

                var parameters = new StringBuilder();
                parameters.Append(baseUrl);

                var response = await _oxfordHttpWrapper.GetOxfordResponse(parameters.ToString())
                               .ConfigureAwait(false);

                HttpStatusCode = response.StatusCode;
                if (!response.IsSuccessStatusCode)
                {
                    _codingChallengeApiLogger.Log().Error(string.Format(SERVICE_RETURNED_THE_FOLLOWING_STATUS,
                                                                        response.StatusCode));
                }

                var result = response.DeserializeHttpMessage <OxfordResponseModel>(_codingChallengeApiLogger, testing, VerboseLogging);

                return(result);
            }
            catch (Exception ex)
            {
                _codingChallengeApiLogger.Log().Error(ex.GetInnerMostException().Message, ex);
                throw;
            }
        }
        public PixabayResponseModel Pixabay(CodingChallengeRequestModel pixabayRequest, bool testing = false)
        {
            var result = _pixabayApiWrapper.PixabayApi(pixabayRequest, testing).Result;

            result.MaxRequested = _apiConfigurationHelper.APIConfiguration.PixabayAPI.MaxNumberOfImages;

            result.Hits = result.Hits.Take(result.MaxRequested).ToList();

            return(result);
        }
        public void Fail_Test(string value)
        {
            var service = new ValidateSearchTerms();

            ;
            var model = new CodingChallengeRequestModel {
                Query = value
            };

            service.Validate(model, out var messages, out var hardStop);

            messages.Count.Should().Be(1);
            messages.First().Should().Be($"Failure: Query string cannot be empty");
            hardStop.Should().BeTrue();
        }
        public void Pass_Test()
        {
            var service = new ValidateSearchTerms();

            ;
            var model = new CodingChallengeRequestModel {
                Query = "red car"
            };

            service.Validate(model, out var messages, out var hardStop);

            messages.Count.Should().Be(1);
            messages.First().Should().Be($"Info: Query string {model.Query} is valid");
            hardStop.Should().BeFalse();
        }
        public void Validate(CodingChallengeRequestModel model, out List <string> messages, out bool hardStop)
        {
            hardStop = false;

            if (string.IsNullOrEmpty(model.Query))
            {
                hardStop = true;
                messages = new List <string> {
                    $"Failure: Query string cannot be empty"
                };
                return;
            }

            messages = new List <string> {
                $"Info: Query string {model.Query} is valid"
            };
        }
Beispiel #8
0
 public HttpResponseMessage SendChatBotData([FromUri] CodingChallengeRequestModel model)
 {
     try
     {
         return(CodingChallengeResponseModel(model, _pixabayApiService.Pixabay(model), _oxfordApiService.Oxford(model)));
     }
     catch (Exception e)
     {
         _loggingService.Error(e.GetInnerMostException().Message, e);
         return(Request.CreateResponse(HttpStatusCode.InternalServerError,
                                       new
         {
             Ok = false,
             Messages = new List <string> {
                 e.GetInnerMostException().Message, e.ToString()
             },
             Request = model
         }));
     }
 }
Beispiel #9
0
        public async Task <PixabayResponseModel> PixabayApi(CodingChallengeRequestModel emailRequest, bool testing = false)
        {
            try
            {
                SetupContext();

                //Would have liked to use string interpolation - but it didn't work.
                var baseUrl = _apiConfigurationHelper.APIConfiguration.PixabayAPI.UrlFormat.Replace(KEY_REPLACE, _apiConfigurationHelper.APIConfiguration.PixabayAPI.APIKey)
                              .Replace(QUERY_REPLACE, emailRequest.Query).Replace(CATEGORY_REPLACE, emailRequest.Category.ToString().ToLower())
                              .Replace(TYPE_REPLACE, emailRequest.Type.ToString().ToLower());

                var parameters = new StringBuilder();
                parameters.Append(baseUrl);
                if (_apiConfigurationHelper.APIConfiguration.PixabayAPI.SafeSearch)
                {
                    parameters.Append(SAFE_SEARCH_TRUE);
                }


                var response = await _pixabayHttpWrapper.GetPixabayResponse(parameters.ToString())
                               .ConfigureAwait(false);

                HttpStatusCode = response.StatusCode;
                if (!response.IsSuccessStatusCode)
                {
                    _codingChallengeApiLogger.Log().Error(string.Format(SERVICE_RETURNED_THE_FOLLOWING_STATUS,
                                                                        response.StatusCode));
                }

                var result = response.DeserializeHttpMessage <PixabayResponseModel>(_codingChallengeApiLogger, testing, VerboseLogging);

                return(result);
            }
            catch (Exception ex)
            {
                _codingChallengeApiLogger.Log().Error(ex.GetInnerMostException().Message, ex);
                throw;
            }
        }
Beispiel #10
0
        public OxfordResponseModel Oxford(CodingChallengeRequestModel oxfordRequest, bool testing = false)
        {
            var result = _oxfordApiWrapper.OxfordDictionaryAPI(oxfordRequest, testing).Result;

            return(result);
        }