-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
59 lines (48 loc) · 2.39 KB
/
Program.cs
File metadata and controls
59 lines (48 loc) · 2.39 KB
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
using System;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Dynamic;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace WebAPIClient
{
class Program
{
private static readonly HttpClient _client = new HttpClient();
private static readonly String RESTAPI_URL = "<Your ACI URL>.azurecontainer.io/score";
private static readonly String RESTAPI_KEY = "<Your Key>";
private static readonly string httpPostBody = "{\"data\": [ {\"age\": 24,\"job\": \"technician\",\"marital\": \"single\",\"education\": \"university.degree\",\"default\": \"no\",\"housing\": \"no\",\"loan\": \"yes\",\"contact\": \"cellular\",\"month\": \"jul\",\"duration\": 109, \"campaign\": 3,\"pdays\": 999,\"previous\": 0,\"poutcome\": \"nonexistent\",\"emp.var.rate\": 1.4,\"cons.price.idx\": 93.918,\"cons.conf.idx\": -42.7,\"euribor3m\": 4.963,\"nr.employed\": 5228.1}]}";
static async Task Main(string[] args)
{
await ProcessRepositories();
Console.ReadLine();
}
private static async Task ProcessRepositories()
{
// REST API 組み立て
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(RESTAPI_URL)
};
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", RESTAPI_KEY);
request.Content = new StringContent(httpPostBody, Encoding.UTF8, "application/json");
// REST API 呼び出し
var response = await _client.SendAsync(request);
var responseBody = response.Content.ReadAsStringAsync().Result;
string result = "";
if (response.IsSuccessStatusCode) {
// 文字列整形
var responseBodyClearnuped = responseBody.Replace("\"{", "{").Replace("\"}", "}").Replace("}\"", "}").Replace("\\\"","\"");
// JSON オブジェクト作成
dynamic responseBodyJSON = JsonConvert.DeserializeObject<ExpandoObject>(responseBodyClearnuped, new ExpandoObjectConverter());
result = responseBodyJSON?.result[0];
} else {
result = $"Error: {response.ReasonPhrase} \r {responseBody}";
}
Console.WriteLine($"result: {result}");
}
}
}