Skip to content

Commit ffe2cd8

Browse files
authored
Merge pull request #115339 from dksimpson/DKS-US1712683-bing-video-search
Refresh Bing Video Search REST API quickstarts
2 parents dcef01a + 9cbb62f commit ffe2cd8

File tree

7 files changed

+151
-143
lines changed

7 files changed

+151
-143
lines changed

articles/cognitive-services/Bing-Video-Search/quickstarts/csharp.md

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,26 @@ manager: nitinme
99
ms.service: cognitive-services
1010
ms.subservice: bing-video-search
1111
ms.topic: quickstart
12-
ms.date: 12/09/2019
12+
ms.date: 05/22/2020
1313
ms.author: aahi
1414
---
1515

1616
# Quickstart: Search for videos using the Bing Video Search REST API and C#
1717

18-
Use this quickstart to make your first call to the Bing Video Search API and view a search result from the JSON response. This simple C# application sends an HTTP video search query to the API, and displays the response. While this application is written in C#, the API is a RESTful Web service compatible with most programming languages.
18+
Use this quickstart to make your first call to the Bing Video Search API. This simple C# application sends an HTTP video search query to the API, and displays the JSON response. Although this application is written in C#, the API is a RESTful Web service compatible with most programming languages.
1919

2020
The source code for this sample is available [on GitHub](https://github.com/Azure-Samples/cognitive-services-REST-api-samples/blob/master/dotnet/Search/BingVideoSearchv7.cs) with additional error handling, features, and code annotations.
2121

2222
## Prerequisites
2323
* Any edition of [Visual Studio 2017 or later](https://www.visualstudio.com/downloads/).
2424
* The [Json.NET](https://www.newtonsoft.com/json) framework, available as a NuGet package.
25-
* If you are using Linux/MacOS, this application can be run using [Mono](https://www.mono-project.com/).
25+
* If you're using Linux/MacOS, you can run this application by using [Mono](https://www.mono-project.com/).
2626

2727
[!INCLUDE [cognitive-services-bing-video-search-signup-requirements](../../../../includes/cognitive-services-bing-video-search-signup-requirements.md)]
2828

2929
## Create and initialize a project
3030

31-
1. Create a new console solution in Visual Studio. Then add the following namespaces into the main code file.
31+
1. Create a new console solution in Visual Studio. Then, add the following namespaces to the main code file:
3232

3333
```csharp
3434
using System;
@@ -38,41 +38,42 @@ The source code for this sample is available [on GitHub](https://github.com/Azur
3838
using System.Collections.Generic;
3939
```
4040

41-
2. Add variables for your subscription key, endpoint, and search term. `uriBase` can be the global endpoint below, or the [custom subdomain](../../../cognitive-services/cognitive-services-custom-subdomains.md) endpoint displayed in the Azure portal for your resource.
41+
2. Add variables for your subscription key, endpoint, and search term. For the `uriBase` value, you can use the global endpoint in the following code, or use the [custom subdomain](../../../cognitive-services/cognitive-services-custom-subdomains.md) endpoint displayed in the Azure portal for your resource.
4242

4343
```csharp
4444
const string accessKey = "enter your key here";
4545
const string uriBase = "https://api.cognitive.microsoft.com/bing/v7.0/videos/search";
4646
const string searchTerm = "kittens";
4747
```
4848

49-
### Create a struct to format the Bing Video Search API response
49+
## Create a struct to format the Bing Video Search API response
5050

51-
1. Define a `SearchResult` struct to contain the image search results, and JSON header information.
51+
Define a `SearchResult` struct to contain the image search results, and JSON header information.
5252

53-
```csharp
54-
struct SearchResult
55-
{
56-
public String jsonResult;
57-
public Dictionary<String, String> relevantHeaders;
58-
}
59-
```
53+
```csharp
54+
struct SearchResult
55+
{
56+
public String jsonResult;
57+
public Dictionary<String, String> relevantHeaders;
58+
}
59+
```
6060

6161
## Create and handle a video search request
6262

63-
Create a method named `BingVideoSearch` to perform the call to the API, and set the return type to the `SearchResult` struct created earlier. In the method, perform the following steps:
63+
1. Create a method named `BingVideoSearch` to perform the call to the API, and set the return type to the `SearchResult` struct created previously.
6464

65-
1. Construct the URI for the search request. Note that the search term toSearch must be formatted before being appended to the string.
65+
Add code to this method in the steps that follow.
6666

67-
```csharp
68-
67+
1. Construct the URI for the search request. Format the search term `toSearch` before you append it to the string.
68+
69+
```csharp
6970
static SearchResult BingVideoSearch(string toSearch){
7071

7172
var uriQuery = uriBase + "?q=" + Uri.EscapeDataString(toSearch);
7273
//...
7374
```
7475

75-
2. Perform the web request by adding your key to the `Ocp-Acpim-Subscription-Key` header, and using a `HttpWebResponse` object to store the API response. Then use a `StreamReader` to get the JSON string.
76+
2. Perform the web request by adding your key to the `Ocp-Acpim-Subscription-Key` header, and using a `HttpWebResponse` object to store the API response. Then, use a `StreamReader` to get the JSON string.
7677

7778
```csharp
7879
//...
@@ -85,7 +86,7 @@ Create a method named `BingVideoSearch` to perform the call to the API, and set
8586

8687
## Process the result
8788

88-
1. Create the search result object, and extract the Bing HTTP headers. Then return the `searchResult` object.
89+
1. Create the search result object, and extract the Bing HTTP headers. Then, return the `searchResult` object.
8990

9091
```csharp
9192
var searchResult = new SearchResult();
@@ -101,7 +102,7 @@ Create a method named `BingVideoSearch` to perform the call to the API, and set
101102
return searchResult;
102103
```
103104

104-
2. You can then print the response.
105+
2. Print the response.
105106

106107
```csharp
107108
Console.WriteLine(result.jsonResult);

articles/cognitive-services/Bing-Video-Search/quickstarts/java.md

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ manager: nitinme
99
ms.service: cognitive-services
1010
ms.subservice: bing-video-search
1111
ms.topic: quickstart
12-
ms.date: 12/09/2019
12+
ms.date: 05/22/2020
1313
ms.author: aahi
1414
---
1515
# Quickstart: Search for videos using the Bing Video Search REST API and Java
1616

17-
Use this quickstart to make your first call to the Bing Video Search API and view a search result from the JSON response. This simple Java application sends an HTTP video search query to the API, and displays the response. While this application is written in Java, the API is a RESTful Web service compatible with most programming languages. The source code for this sample is available [on GitHub](https://github.com/Azure-Samples/cognitive-services-REST-api-samples/blob/master/java/Search/BingVideoSearchv7.java) with additional error handling, features, and code annotations.
17+
Use this quickstart to make your first call to the Bing Video Search API. This simple Java application sends an HTTP video search query to the API and displays the JSON response. Although this application is written in Java, the API is a RESTful Web service compatible with most programming languages.
18+
19+
The source code for this sample is available [on GitHub](https://github.com/Azure-Samples/cognitive-services-REST-api-samples/blob/master/java/Search/BingVideoSearchv7.java) with additional error handling, features, and code annotations.
1820

1921
## Prerequisites
2022

21-
* The [Java Development Kit(JDK)](https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html)
23+
* The [Java Development Kit (JDK)](https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html)
2224

2325
* The [Gson library](https://github.com/google/gson)
2426

@@ -27,7 +29,7 @@ Use this quickstart to make your first call to the Bing Video Search API and vie
2729

2830
## Create and initialize a project
2931

30-
1. Create a new Java project in your favorite IDE or editor, and import the following libraries.
32+
1. Create a new Java project in your favorite IDE or editor, and import the following libraries:
3133

3234
```java
3335
import java.net.*;
@@ -54,7 +56,7 @@ Use this quickstart to make your first call to the Bing Video Search API and vie
5456
}
5557
```
5658

57-
3. Create a new method named `SearchVideos()` with variables for your API endpoint host and path, your subscription key, and a search term. It will return a `SearchResults` object. `host` can be the global endpoint below, or the [custom subdomain](../../../cognitive-services/cognitive-services-custom-subdomains.md) endpoint displayed in the Azure portal for your resource.
59+
3. Create a new method named `SearchVideos()` with variables for your API endpoint host and path, your subscription key, and search term. This method returns a `SearchResults` object. For the `host` value, you can use the global endpoint in the following code, or use the [custom subdomain](../../../cognitive-services/cognitive-services-custom-subdomains.md) endpoint displayed in the Azure portal for your resource.
5860

5961
```java
6062
public static SearchResults SearchVideos (String searchQuery) throws Exception {
@@ -67,66 +69,66 @@ Use this quickstart to make your first call to the Bing Video Search API and vie
6769

6870
## Construct and send the search request
6971

70-
1. In `SearchVideos()`, perform the following steps:
72+
In the `SearchVideos()` method, perform the following steps:
7173

72-
1. construct the URL for your request by combining your API host, path, and encoding your search query. Then use `openConnection()` to create a connection, and add your subscription key to the `Ocp-Apim-Subscription-Key` header.
74+
1. Construct the URL for your request by combining your API host, path, and encoded search query. Use `openConnection()` to create a connection, and then add your subscription key to the `Ocp-Apim-Subscription-Key` header.
7375

74-
```java
75-
URL url = new URL(host + path + "?q=" + URLEncoder.encode(searchQuery, "UTF-8"));
76-
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
77-
connection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);
78-
```
76+
```java
77+
URL url = new URL(host + path + "?q=" + URLEncoder.encode(searchQuery, "UTF-8"));
78+
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
79+
connection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);
80+
```
7981

80-
2. Get the response from the API and store the JSON string.
82+
2. Get the response from the API and store the JSON string.
8183

82-
```java
83-
InputStream stream = connection.getInputStream();
84-
String response = new Scanner(stream).useDelimiter("\\A").next();
85-
```
84+
```java
85+
InputStream stream = connection.getInputStream();
86+
String response = new Scanner(stream).useDelimiter("\\A").next();
87+
```
8688

87-
3. Use `getHeaderFields();` to extract the HTTP headers from the response, and store the Bing-related ones in the `results` object. Then close the stream and return the result.
89+
3. Use `getHeaderFields()` to extract the HTTP headers from the response, and store the Bing-related ones in the `results` object. Then, close the stream and return the result.
8890

89-
```java
90-
// extract Bing-related HTTP headers
91-
Map<String, List<String>> headers = connection.getHeaderFields();
92-
for (String header : headers.keySet()) {
93-
if (header == null) continue; // may have null key
94-
if (header.startsWith("BingAPIs-") || header.startsWith("X-MSEdge-")) {
95-
results.relevantHeaders.put(header, headers.get(header).get(0));
96-
}
97-
}
98-
stream.close();
99-
return results;
100-
```
91+
```java
92+
// extract Bing-related HTTP headers
93+
Map<String, List<String>> headers = connection.getHeaderFields();
94+
for (String header : headers.keySet()) {
95+
if (header == null) continue; // may have null key
96+
if (header.startsWith("BingAPIs-") || header.startsWith("X-MSEdge-")) {
97+
results.relevantHeaders.put(header, headers.get(header).get(0));
98+
}
99+
}
100+
stream.close();
101+
return results;
102+
```
101103

102104
## Format the response
103105

104-
1. Create a method named `prettify()` to format the response returned from the Bing Video API. Use the Gson library's `JsonParser` to take in a JSON string and convert it into an object. Then use `GsonBuilder()` and `toJson()` to create the formatted string.
106+
Create a method named `prettify()` to format the response returned from the Bing Video API. Use the Gson library's `JsonParser` to convert a JSON string to an object. Then, use `GsonBuilder()` and `toJson()` to create the formatted string.
105107

106-
```java
107-
// pretty-printer for JSON; uses GSON parser to parse and re-serialize
108-
public static String prettify(String json_text) {
109-
JsonParser parser = new JsonParser();
110-
JsonObject json = parser.parse(json_text).getAsJsonObject();
111-
Gson gson = new GsonBuilder().setPrettyPrinting().create();
112-
return gson.toJson(json);
113-
}
114-
```
108+
```java
109+
// pretty-printer for JSON; uses GSON parser to parse and re-serialize
110+
public static String prettify(String json_text) {
111+
JsonParser parser = new JsonParser();
112+
JsonObject json = parser.parse(json_text).getAsJsonObject();
113+
Gson gson = new GsonBuilder().setPrettyPrinting().create();
114+
return gson.toJson(json);
115+
}
116+
```
115117

116118
## Send the request and print the response
117119

118-
1. In the main method of your application, call `SearchVideos` with your search term. you can then print the HTTP headers stored in the response, as well as the JSON string returned by the API.
120+
In the main method of your application, call `SearchVideos` with your search term. Then, print the HTTP headers stored in the response and the JSON string returned by the API.
119121

120-
```java
121-
public static void main (String[] args) {
122+
```java
123+
public static void main (String[] args) {
122124

123-
SearchResults result = SearchVideos(searchTerm);
124-
//print the Relevant HTTP Headers
125-
for (String header : result.relevantHeaders.keySet())
126-
System.out.println(header + ": " + result.relevantHeaders.get(header));
127-
System.out.println(prettify(result.jsonResponse));
128-
}
129-
```
125+
SearchResults result = SearchVideos(searchTerm);
126+
//print the Relevant HTTP Headers
127+
for (String header : result.relevantHeaders.keySet())
128+
System.out.println(header + ": " + result.relevantHeaders.get(header));
129+
System.out.println(prettify(result.jsonResponse));
130+
}
131+
```
130132

131133
## JSON response
132134

0 commit comments

Comments
 (0)