public IActionResult Query(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return(null);
            }

            var request = new PlacesAutoCompleteRequest();

            request.Key   = this.configuration.GetValue <string>("API:Google:Key").ToString();
            request.Input = input;
            request.Types = new List <RestrictPlaceType>()
            {
                RestrictPlaceType.Cities
            };
            request.Language = !Regex.IsMatch(input, @"\P{IsCyrillic}") ? Language.Bulgarian : Language.English;
            var uri         = request.GetUri();
            var parameterss = request.GetQueryStringParameters();

            var response = GooglePlaces.AutoComplete.Query(request);

            if (string.IsNullOrEmpty(response.ErrorMessage))
            {
                var predictions = response.Predictions.Select(p => p.Terms.FirstOrDefault());
                return(Json(predictions.Select(p => p.Value).Distinct().ToArray()));
            }
            return(null);
        }
Ejemplo n.º 2
0
        public void GetUriTest()
        {
            var request = new PlacesAutoCompleteRequest
            {
                Key   = "abc",
                Input = "abc"
            };

            var uri = request.GetUri();

            Assert.IsNotNull(uri);
            Assert.AreEqual($"/maps/api/place/autocomplete/json?key={request.Key}&input={request.Input}&language={request.Language.ToCode()}", uri.PathAndQuery);
        }
Ejemplo n.º 3
0
        public void GetUriWhenLocationTest()
        {
            var request = new PlacesAutoCompleteRequest
            {
                Key      = "abc",
                Input    = "abc",
                Location = new Location(1, 1)
            };

            var uri = request.GetUri();

            Assert.IsNotNull(uri);
            Assert.AreEqual($"/maps/api/place/autocomplete/json?key={request.Key}&input={request.Input}&language={request.Language.ToCode()}&location={Uri.EscapeDataString(request.Location.ToString())}", uri.PathAndQuery);
        }
Ejemplo n.º 4
0
        public void GetUriWhenComponentsTest()
        {
            var request = new PlacesAutoCompleteRequest
            {
                Key        = "abc",
                Input      = "abc",
                Components = new[]
                {
                    new KeyValuePair <Component, string>(Component.Administrative_Area, "abc")
                }
            };

            var uri = request.GetUri();

            Assert.IsNotNull(uri);
            Assert.AreEqual($"/maps/api/place/autocomplete/json?key={request.Key}&input={request.Input}&language={request.Language.ToCode()}&components={Uri.EscapeDataString(string.Join("|", request.Components.Select(x => $"{x.Key.ToString().ToLower()}:{x.Value}")))}", uri.PathAndQuery);
        }