/// <summary> /// Return Bing Web Search results /// </summary> /// <param name="searchType">string</param> /// <param name="query">string</param> /// <returns>Task<ResultModel></returns> /// <exception cref="Exception">searchType - excepted (Web, Image or Video)</exception> /// <method>Search(string searchType, string query)</method> public async Task <ResultModel> Search(string searchType, string query) { ResultModel results = new ResultModel(searchType.Trim().Clean()); // set search query url string queryString = string.Empty; switch (results.Type) { case "Web": queryString += QUERY_PARAMETER; break; case "Image": queryString += "images/" + QUERY_PARAMETER; break; case "Video": queryString += "videos/" + QUERY_PARAMETER; break; } queryString += Uri.EscapeDataString(query.Trim().Clean()); queryString += CUSTOM_CONFIG + _customConfigId; queryString += MKT_PARAMETER + "en-US"; queryString += TEXT_DECORATIONS_PARAMETER + Boolean.TrueString; queryString += COUNT_PARAMETER + results.MaxResults; queryString += OFFSET_PARAMETER + ((results.PageNumber * results.MaxResults) - results.MaxResults); JsonClient client = new JsonClient(_baseUri); client.AddRequestHeader("Ocp-Apim-Subscription-Key", _subscriptionKey); results.StatusCode = await client.SendRequest(HttpMethod.Get, queryString, string.Empty); // wait 1 second to prevent throttling Thread.Sleep(1000); results.StatusMessage = client.GetResponseString(); if (client.IsResponseSuccess) { Dictionary <string, object> resultDictionary = client.GetResponseObject <Dictionary <string, object> >(); switch (results.Type) { case "Web": JObject webResultObject = (JObject)resultDictionary.Where(x => x.Key == "webPages").Select(x => x.Value).FirstOrDefault(); results.Items = ((JArray)webResultObject["value"]).ToObject <List <ItemModel> >(); results.StatusMessage = client.StatusCode.ToString(); break; case "Image": Object[] imageArray = resultDictionary.Where(x => x.Key == "value").Select(x => x.Value).ToArray(); results.Items = ((JArray)imageArray[0]).ToObject <List <ItemModel> >(); results.StatusMessage = client.StatusCode.ToString(); break; case "Video": Object[] videoArray = resultDictionary.Where(x => x.Key == "value").Select(x => x.Value).ToArray(); results.Items = ((JArray)videoArray[0]).ToObject <List <ItemModel> >(); results.StatusMessage = client.StatusCode.ToString(); break; } } return(results); }