public static async Task <Suggestions> callAutosuggestSearchAPI(BingAutosuggestQuery query)
        {
            var url = $"{ApiEndpoints.AUTOSUGGEST_URL}{query.ToQueryString()}";

            using (var bingClient = new BingHttpClient(query.SearchKey))
            {
                var httpResponseMessage = await bingClient.GetAsync(url);

                if (httpResponseMessage.IsSuccessStatusCode)
                {
                    var responseContent = httpResponseMessage.Content.ReadAsStringAsync().Result;
                    return(JsonConvert.DeserializeObject <Suggestions>(responseContent));
                }
                else
                {
                    throw new InvalidOperationException("An error occurred fetching the results from the autosuggest service");
                }
            }
        }
Example #2
0
 // Matt Custom Search Part 7
 public static void CallBingCustomAutosuggestAPI()
 {
     try
     {
         // Get the file name
         Console.WriteLine("Please enter a search query:");
         var searchQuery = Console.ReadLine();
         var query       = new BingAutosuggestQuery(searchQuery, Constants.AUTOSUGGEST_SUBSCRIPTION_KEY, Constants.CUSTOM_CONFIG_ID);
         var result      = BingAutosuggestService.callAutosuggestSearchAPI(query).Result;
         ExportJSON(JsonConvert.SerializeObject(result));
         Console.WriteLine("\nPress Enter to exit ");
         Console.ReadLine();
     }
     catch (Exception ex)
     {
         Console.WriteLine("Error: " + ex.Message);
         Console.ReadLine();
         throw;
     }
 }