Skip to content

Commit 7838990

Browse files
authored
Merge pull request #107 from aahill/code-updates
[Bing Custom Search] REST API Code updates
2 parents 1fbcbdf + 056afbf commit 7838990

File tree

4 files changed

+72
-37
lines changed

4 files changed

+72
-37
lines changed

dotnet/Search/BingCustomSearchv7.cs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
11
//Copyright (c) Microsoft Corporation. All rights reserved.
22
//Licensed under the MIT License.
3-
3+
// <using>
44
using System;
55
using System.Net.Http;
66
using System.Web;
77
using Newtonsoft.Json;
8+
// </using>
89

910
namespace bing_custom_search_example_dotnet
1011
{
1112
class Program
1213
{
1314
static void Main(string[] args)
1415
{
15-
// Add your Azure Bing Custom Search subscription key to your environment variables.
16+
// <vars>
17+
// Add your Azure Bing Custom Search subscription key and endpoint to your environment variables.
18+
// Your endpoint will have the form: https://<your-custom-subdomain>.cognitiveservices.azure.com/bingcustomsearch/v7.0
1619
var subscriptionKey = Environment.GetEnvironmentVariable("BING_CUSTOM_SEARCH_SUBSCRIPTION_KEY");
17-
var customConfigId = "YOUR-CUSTOM-CONFIG-ID";
18-
var searchTerm = args.Length > 0 ? args[0]: "microsoft";
19-
20-
// Add your Azure Bing Custom Search endpoint to your environment variables.
21-
var url = Environment.GetEnvironmentVariable("BING_CUSTOM_SEARCH_ENDPOINT") + "/bingcustomsearch/v7.0/search?" +
22-
"q=" + searchTerm +
23-
"&customconfig=" + customConfigId;
20+
var endpoint = Environment.GetEnvironmentVariable("BING_CUSTOM_SEARCH_ENDPOINT");
2421

22+
var customConfigId = "YOUR-CUSTOM-CONFIG-ID"; // you can also use "1"
23+
var searchTerm = args.Length > 0 ? args[0]: "microsoft";
24+
// </vars>
25+
// </url>
26+
// Use your Azure Bing Custom Search endpoint to create the full request URL.
27+
var url = endpoint + "/search?" + "q=" + searchTerm + "&customconfig=" + customConfigId;
28+
// </url>
29+
// <client>
2530
var client = new HttpClient();
2631
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
32+
// </client>
33+
// <sendRequest>
2734
var httpResponseMessage = client.GetAsync(url).Result;
2835
var responseContent = httpResponseMessage.Content.ReadAsStringAsync().Result;
2936
BingCustomSearchResponse response = JsonConvert.DeserializeObject<BingCustomSearchResponse>(responseContent);
30-
37+
// </sendRequest>
38+
// <iterateResponse>
3139
for(int i = 0; i < response.webPages.value.Length; i++)
3240
{
3341
var webPage = response.webPages.value[i];
@@ -38,10 +46,11 @@ static void Main(string[] args)
3846
Console.WriteLine("snippet: " + webPage.snippet);
3947
Console.WriteLine("dateLastCrawled: " + webPage.dateLastCrawled);
4048
Console.WriteLine();
41-
}
49+
}
50+
//</iterateResponse>
4251
}
4352
}
44-
53+
// <repsonseClasses>
4554
public class BingCustomSearchResponse
4655
{
4756
public string _type{ get; set; }
@@ -72,4 +81,5 @@ public class OpenGraphImage
7281
public int width { get; set; }
7382
public int height { get; set; }
7483
}
84+
// <repsonseClasses>
7585
}

java/Search/BingCustomSearchv7.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*Copyright (c) Microsoft Corporation. All rights reserved.
22
Licensed under the MIT License.*/
3-
3+
// <imports>
4+
import javax.net.ssl.HttpsURLConnection;
45
import java.io.InputStream;
56
import java.net.URL;
67
import java.net.URLEncoder;
@@ -9,25 +10,27 @@
910
import java.util.Map;
1011
import java.util.Scanner;
1112

12-
import javax.net.ssl.HttpsURLConnection;
13-
1413
import com.google.gson.Gson;
1514
import com.google.gson.GsonBuilder;
1615
import com.google.gson.JsonObject;
1716
import com.google.gson.JsonParser;
17+
// </imports>
1818

19-
public class CustomSrchJava {
19+
public class BingCustomSearchv7 {
2020

21-
// Add your Bing Custom Search endpoint to your environment variables.
21+
// <vars>
22+
// Add your Bing Custom Search endpoint and key to your environment variables.
23+
// Your endpoint will have the form:
24+
// https://<your-custom-subdomain>.cognitiveservices.azure.com/bingcustomsearch/v7.0
2225
static String host = System.getenv("BING_CUSTOM_SEARCH_ENDPOINT");
23-
static String path = "/bingcustomsearch/v7.0/search";
24-
// Add your Bing Custom Search subscription key to your environment variables.
2526
static String subscriptionKey = System.getenv("BING_CUSTOM_SEARCH_SUBSCRIPTION_KEY");
26-
static String customConfigId = "YOUR-CUSTOM-CONFIG-ID";
27-
27+
28+
static String path = "/search";
29+
static String customConfigId = "YOUR-CUSTOM-CONFIG-ID"; //you can also use "1"
2830
static String searchTerm = "Microsoft"; // Replace with search term specific to your defined sources.
29-
30-
public static SearchResults SearchImages (String searchQuery) throws Exception {
31+
// </vars>
32+
// <searchWeb>
33+
public static SearchResults SearchWeb(String searchQuery) throws Exception {
3134
// construct URL of search request (endpoint + query string)
3235
URL url = new URL(host + path + "?q=" + URLEncoder.encode(searchTerm, "UTF-8") + "&CustomConfig=" + customConfigId);
3336
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
@@ -52,15 +55,17 @@ public static SearchResults SearchImages (String searchQuery) throws Exception {
5255
stream.close();
5356
return results;
5457
}
55-
58+
// </searchWeb>
59+
// <prettify>
5660
// pretty-printer for JSON; uses GSON parser to parse and re-serialize
5761
public static String prettify(String json_text) {
5862
JsonParser parser = new JsonParser();
5963
JsonObject json = parser.parse(json_text).getAsJsonObject();
6064
Gson gson = new GsonBuilder().setPrettyPrinting().create();
6165
return gson.toJson(json);
6266
}
63-
67+
// </prettify>
68+
// <main>
6469
public static void main (String[] args) {
6570
if (subscriptionKey.length() != 32) {
6671
System.out.println("Invalid Bing Search API subscription key!");
@@ -71,7 +76,7 @@ public static void main (String[] args) {
7176
try {
7277
System.out.println("Searching the Web for: " + searchTerm);
7378

74-
SearchResults result = SearchImages(searchTerm);
79+
SearchResults result = SearchWeb(searchTerm);
7580

7681
System.out.println("\nRelevant HTTP Headers:\n");
7782
for (String header : result.relevantHeaders.keySet())
@@ -85,8 +90,13 @@ public static void main (String[] args) {
8590
System.exit(1);
8691
}
8792
}
93+
// </main>
8894
}
8995

96+
// <searchResultsClass>
97+
// put this in a seperate .java file.
98+
import java.util.HashMap;
99+
90100
// Container class for search results encapsulates relevant headers and JSON data
91101
class SearchResults{
92102
HashMap<String, String> relevantHeaders;
@@ -97,3 +107,5 @@ class SearchResults{
97107
}
98108

99109
}
110+
// </searchResultsClass>
111+
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
//Copyright (c) Microsoft Corporation. All rights reserved.
22
//Licensed under the MIT License.
3-
3+
// <vars>
44
var request = require("request");
5-
6-
var subscriptionKey = process.env['BING_CUSTOM_SEARCH_SUBSCRIPTION_KEY']
7-
var customConfigId = 'YOUR-CUSTOM-CONFIG-ID';
5+
// Add your Bing Custom Search subscription key to your environment variables.
6+
// Your endpoint will have the form:
7+
// https://<your-custom-subdomain>.cognitiveservices.azure.com/bingcustomsearch/v7.0
8+
var subscriptionKey = process.env['BING_CUSTOM_SEARCH_SUBSCRIPTION_KEY'];
9+
var endpoint = process.env['BING_CUSTOM_SEARCH_ENDPOINT'];
10+
var customConfigId = 'YOUR-CUSTOM-CONFIG-ID'; //you can also use "1"
811
var searchTerm = 'microsoft';
9-
12+
// </vars>
13+
// <requestOptions>
1014
var options = {
11-
url: process.env['BING_CUSTOM_SEARCH_ENDPOINT'] + "/bingcustomsearch/v7.0/search?' +
15+
url: endpoint + "/search?" +
1216
'q=' + searchTerm +
1317
'&customconfig=' + customConfigId,
1418
headers: {
1519
'Ocp-Apim-Subscription-Key' : subscriptionKey
1620
}
1721
}
18-
22+
// </requestOptions>
23+
// <requestMethod>
1924
request(options, function(error, response, body){
2025
var searchResponse = JSON.parse(body);
2126
for(var i = 0; i < searchResponse.webPages.value.length; ++i){
@@ -28,3 +33,4 @@ request(options, function(error, response, body){
2833
console.log();
2934
}
3035
})
36+
// </requestMethod>

python/Search/BingCustomSearchv7.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,23 @@
44
# You may need the below as well
55
#pip install pipenv
66
#pipenv install requests
7-
7+
# <importsAndVars>
88
import json
99
import requests
10+
import os
1011

1112
# Add your Bing Custom Search subscription key to your environment variables.
13+
# Your endpoint will have the form: https://<your-custom-subdomain>.cognitiveservices.azure.com/bingcustomsearch/v7.0
1214
subscriptionKey = os.environ['BING_CUSTOM_SEARCH_SUBSCRIPTION_KEY']
13-
customConfigId = "YOUR-CUSTOM-CONFIG-ID"
15+
endpoint = os.environ['BING_CUSTOM_SEARCH_ENDPOINT']
16+
customConfigId = "1" #you can also use "1"
1417
searchTerm = "microsoft"
15-
18+
# </importsAndVars>
19+
# <url>
1620
# Add your Bing Custom Search endpoint to your environment variables.
17-
url = os.environ['BING_CUSTOM_SEARCH_ENDPOINT'] + "/bingcustomsearch/v7.0/search?q=' + searchTerm + '&customconfig=' + customConfigId
21+
url = endpoint + "/search?q=" + searchTerm + "&customconfig=" + customConfigId
22+
# </url>
23+
# <request>
1824
r = requests.get(url, headers={'Ocp-Apim-Subscription-Key': subscriptionKey})
1925
print(r.text)
26+
# </request>

0 commit comments

Comments
 (0)