forked from Azure-Samples/cognitive-services-qnamaker-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish-knowledge-base.cs
62 lines (52 loc) · 1.86 KB
/
publish-knowledge-base.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Net.Http;
// NOTE: Install the Newtonsoft.Json NuGet package.
using Newtonsoft.Json;
namespace QnAMakerQuickstart
{
class Program
{
static string host = "https://westus.api.cognitive.microsoft.com";
static string service = "/qnamaker/v4.0";
static string method = "/knowledgebases/";
// NOTE: Replace this with a valid subscription key.
static string key = "ADD KEY HERE";
// NOTE: Replace this with a valid knowledge base ID.
static string kb = "ADD KB ID HERE";
static string PrettyPrint(string s)
{
return JsonConvert.SerializeObject(JsonConvert.DeserializeObject(s), Formatting.Indented);
}
async static void PublishKB()
{
string responseText;
var uri = host + service + method + kb;
Console.WriteLine("Calling " + uri + ".");
using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
{
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(uri);
request.Headers.Add("Ocp-Apim-Subscription-Key", key);
var response = await client.SendAsync(request);
// successful status doesn't return an JSON so create one
if (response.IsSuccessStatusCode)
{
responseText = "{'result' : 'Success.'}";
}
else
{
responseText = await response.Content.ReadAsStringAsync();
}
}
Console.WriteLine(PrettyPrint(responseText));
Console.WriteLine("Press any key to continue.");
}
static void Main(string[] args)
{
PublishKB();
Console.ReadLine();
}
}
}