internal static string ToJson(this IVenueSearchCriteria extendee)
        {
            VenueSearchJson json = new VenueSearchJson
            {
                PRN = extendee.Search
            };
            var result = JsonConvert.SerializeObject(json);

            return(result);
        }
        public async Task <IResult <IVenueSearchResult> > SearchAsync(IVenueSearchCriteria criteria)
        {
            Throw.IfNull(criteria, nameof(criteria));
            _logger.LogMethodEnter();

            try
            {
                _logger.LogInformationObject("Venue search criteria.", criteria);
                _logger.LogInformationObject("Venue search URI", _searchVenueUri);

                var content  = new StringContent(criteria.ToJson(), Encoding.UTF8, "application/json");
                var response = await _httpClient.PostAsync(_searchVenueUri, content);

                _logger.LogHttpResponseMessage("Venue search service http response", response);

                if (response.StatusCode == System.Net.HttpStatusCode.NoContent)
                {
                    return(Result.Ok <IVenueSearchResult>(new VenueSearchResult(new List <Venue>())));
                }

                if (response.IsSuccessStatusCode)
                {
                    var json = await response.Content.ReadAsStringAsync();

                    _logger.LogInformationObject("Venue search service json response", json);

                    var settings = new JsonSerializerSettings
                    {
                        ContractResolver = new VenueSearchResultContractResolver()
                    };
                    //var venues = JsonConvert.DeserializeObject<IEnumerable<Venue>>(json, settings).Where(x => x.Status == VenueStatus.Live || x.Status == VenueStatus.Imported).OrderBy(x => x.VenueName).ToList();
                    var venues = JsonConvert.DeserializeObject <IEnumerable <Venue> >(json, settings).OrderBy(x => x.VenueName).ToList();

                    if (!String.IsNullOrEmpty(criteria.NewAddressId))
                    {
                        var newVenueIndex = venues.FindIndex(x => x.ID == criteria.NewAddressId);
                        var newVenueItem  = venues[newVenueIndex];

                        venues.RemoveAt(newVenueIndex);
                        venues.Insert(0, newVenueItem);
                    }

                    var searchResult = new VenueSearchResult(venues);
                    return(Result.Ok <IVenueSearchResult>(searchResult));
                }
                else
                {
                    return(Result.Fail <IVenueSearchResult>("Venue search service unsuccessful http response"));
                }
            }

            catch (HttpRequestException hre)
            {
                _logger.LogException("Venue search service http request error", hre);
                return(Result.Fail <IVenueSearchResult>("Venue search service http request error."));
            }
            catch (Exception e)
            {
                _logger.LogException("Venue search service unknown error.", e);

                return(Result.Fail <IVenueSearchResult>("Venue search service unknown error."));
            }
            finally
            {
                _logger.LogMethodExit();
            }
        }
        public async Task <IResult <IVenueSearchResult> > SearchAsync(IVenueSearchCriteria criteria)
        {
            Throw.IfNull(criteria, nameof(criteria));
            _logger.LogMethodEnter();

            try
            {
                _logger.LogInformationObject("Venue search criteria.", criteria);
                _logger.LogInformationObject("Venue search URI", _searchVenueUri);

                HttpClient httpClient = new HttpClient();
                httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _settings.ApiKey);
                var response = await httpClient.GetAsync(_searchVenueUri + $"?prn={criteria.Search}");

                _logger.LogHttpResponseMessage("Venue search service http response", response);

                if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    return(Result.Ok <IVenueSearchResult>(new VenueSearchResult(new List <Venue>())));
                }

                if (response.IsSuccessStatusCode)
                {
                    var json = await response.Content.ReadAsStringAsync();

                    _logger.LogInformationObject("Venue search service json response", json);

                    var settings = new JsonSerializerSettings
                    {
                        ContractResolver = new VenueSearchResultContractResolver()
                    };
                    var venues = JsonConvert.DeserializeObject <IEnumerable <Venue> >(json, settings).OrderBy(x => x.VenueName).ToList();

                    if (!String.IsNullOrEmpty(criteria.NewAddressId))
                    {
                        var newVenueIndex = venues.FindIndex(x => x.ID == criteria.NewAddressId);
                        var newVenueItem  = venues[newVenueIndex];

                        venues.RemoveAt(newVenueIndex);
                        venues.Insert(0, newVenueItem);
                    }

                    var searchResult = new VenueSearchResult(venues);
                    return(Result.Ok <IVenueSearchResult>(searchResult));
                }
                else
                {
                    return(Result.Fail <IVenueSearchResult>("Venue search service unsuccessful http response"));
                }
            }

            catch (HttpRequestException hre)
            {
                _logger.LogException("Venue search service http request error", hre);
                return(Result.Fail <IVenueSearchResult>("Venue search service http request error."));
            }
            catch (Exception e)
            {
                _logger.LogException("Venue search service unknown error.", e);

                return(Result.Fail <IVenueSearchResult>("Venue search service unknown error."));
            }
            finally
            {
                _logger.LogMethodExit();
            }
        }