Skip to content

Commit 3ac41d3

Browse files
authored
Shortened, clarified sample
1 parent 94d88a6 commit 3ac41d3

File tree

1 file changed

+16
-86
lines changed

1 file changed

+16
-86
lines changed

dotnet/Search/BingAutosuggestv7.cs

Lines changed: 16 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
1-
//Copyright (c) Microsoft Corporation. All rights reserved.
2-
//Licensed under the MIT License.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
33

4+
using Newtonsoft.Json;
45
using System;
5-
using System.Collections.Generic;
66
using System.Net.Http;
7-
using System.Net.Http.Headers;
8-
using System.Text;
97

10-
namespace AutosuggestSample1
8+
namespace BingAutosuggest
119
{
1210
class Program
1311
{
12+
// Add your Azure Bing Autosuggest subscription key to your environment variables.
13+
static string key = Environment.GetEnvironmentVariable("BING_AUTOSUGGEST_SUBSCRIPTION_KEY");
1414
// Add your Azure Bing Autosuggest endpoint to your environment variables.
15-
static string host = Environment.GetEnvironmentVariable("BING_AUTOSUGGEST_ENDPOINT");
16-
static string path = "/bing/v7.0/Suggestions";
17-
15+
static string endpoint = Environment.GetEnvironmentVariable("BING_AUTOSUGGEST_ENDPOINT");
16+
static string path = "/bing/v7.0/Suggestions/";
17+
1818
// For a list of available markets, go to:
1919
// https://docs.microsoft.com/rest/api/cognitiveservices/bing-autosuggest-api-v7-reference#market-codes
2020
static string market = "en-US";
2121

22-
// Add your Azure Bing Autosuggest subscription key to your environment variables.
23-
static string key = Environment.GetEnvironmentVariable("BING_AUTOSUGGEST_SUBSCRIPTION_KEY");
24-
2522
static string query = "sail";
2623

2724
// These properties are used for optional headers (see below).
28-
// static string ClientId = "<Client ID from Previous Response Goes Here>";
29-
// static string ClientIp = "999.999.999.999";
30-
// static string ClientLocation = "+90.0000000000000;long: 00.0000000000000;re:100.000000000000";
25+
//static string ClientId = "<Client ID from Previous Response Goes Here>";
26+
//static string ClientIp = "999.999.999.999";
27+
//static string ClientLocation = "+90.0000000000000;long: 00.0000000000000;re:100.000000000000";
3128

3229
async static void Autosuggest()
3330
{
@@ -40,86 +37,19 @@ async static void Autosuggest()
4037
//client.DefaultRequestHeaders.Add("X-MSEdge-ClientID", ClientId);
4138
//client.DefaultRequestHeaders.Add("X-MSEdge-ClientIP", ClientIp);
4239

43-
string uri = host + path + "?mkt=" + market + "&query=" + System.Net.WebUtility.UrlEncode (query);
40+
string uri = endpoint + path + "?mkt=" + market + "&query=" + System.Net.WebUtility.UrlEncode(query);
4441

4542
HttpResponseMessage response = await client.GetAsync(uri);
4643

47-
string contentString = await response.Content.ReadAsStringAsync();
48-
Console.WriteLine(JsonPrettyPrint(contentString));
44+
string contentString = await response.Content.ReadAsStringAsync();
45+
dynamic parsedJson = JsonConvert.DeserializeObject(contentString);
46+
Console.WriteLine(JsonConvert.SerializeObject(parsedJson, Formatting.Indented));
4947
}
5048

5149
static void Main(string[] args)
5250
{
5351
Autosuggest();
5452
Console.ReadLine();
5553
}
56-
57-
static string JsonPrettyPrint(string json)
58-
{
59-
if (string.IsNullOrEmpty(json)) {
60-
return string.Empty;
61-
}
62-
63-
json = json.Replace(Environment.NewLine, "").Replace("\t", "");
64-
65-
StringBuilder sb = new StringBuilder();
66-
bool quote = false;
67-
bool ignore = false;
68-
char last = ' ';
69-
int offset = 0;
70-
int indentLength = 3;
71-
72-
foreach (char ch in json)
73-
{
74-
switch (ch)
75-
{
76-
case '"':
77-
if (!ignore) quote = !quote;
78-
break;
79-
case '\\':
80-
if (quote && last != '\\') ignore = true;
81-
break;
82-
}
83-
84-
if (quote)
85-
{
86-
sb.Append(ch);
87-
if (last == '\\' && ignore) ignore = false;
88-
}
89-
else
90-
{
91-
switch (ch)
92-
{
93-
case '{':
94-
case '[':
95-
sb.Append(ch);
96-
sb.Append(Environment.NewLine);
97-
sb.Append(new string(' ', ++offset * indentLength));
98-
break;
99-
case '}':
100-
case ']':
101-
sb.Append(Environment.NewLine);
102-
sb.Append(new string(' ', --offset * indentLength));
103-
sb.Append(ch);
104-
break;
105-
case ',':
106-
sb.Append(ch);
107-
sb.Append(Environment.NewLine);
108-
sb.Append(new string(' ', offset * indentLength));
109-
break;
110-
case ':':
111-
sb.Append(ch);
112-
sb.Append(' ');
113-
break;
114-
default:
115-
if (quote || ch != ' ') sb.Append(ch);
116-
break;
117-
}
118-
}
119-
last = ch;
120-
}
121-
122-
return sb.ToString().Trim();
123-
}
12454
}
12555
}

0 commit comments

Comments
 (0)