/
TelemetryClient.cs
82 lines (72 loc) · 2.59 KB
/
TelemetryClient.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// SOLUTION : KFlearning
// PROJECT : KFlearning.Core
// FILENAME : TelemetryClient.cs
// AUTHOR : Fahmi Noor Fiqri, Kodesiana.com
// WEBSITE : https://kodesiana.com
// REPO : https://github.com/Kodesiana or https://github.com/fahminlb33
//
// This file is part of KFlearning, see LICENSE.
// See this code in repository URL above!
using Newtonsoft.Json;
using System;
using System.Globalization;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace KFlearning.Core.API
{
public interface ITelemetryClient
{
Task SendAppStart(string appName, string deviceId);
Task SendAppExit(string appName, string deviceId);
Task SendIdentification(string deviceId, string cpu, double ram, string os, string bitness);
}
public class TelemetryClient : ITelemetryClient
{
private const string BaseUri = "https://kflearning.kodesiana.com";
private readonly CultureInfo _culture = new CultureInfo("en-US");
private static HttpClient Client = new HttpClient();
static TelemetryClient()
{
Client.DefaultRequestHeaders.Add("Authorization", "fahmi-kodesiana");
}
public async Task SendAppStart(string appName, string deviceId)
{
await SendTelemetry(appName, "started", deviceId);
}
public async Task SendAppExit(string appName, string deviceId)
{
await SendTelemetry(appName, "exit", deviceId);
}
public async Task SendIdentification(string deviceId, string cpu, double ram, string os, string bitness)
{
var body = new
{
deviceId,
cpu,
os,
ram,
bitness
};
using (var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json"))
{
var result = await Client.PostAsync(BaseUri + "/telemetry/device", content);
result.EnsureSuccessStatusCode();
}
}
private async Task SendTelemetry(string appName, string appEvent, string deviceId)
{
var body = new
{
deviceId,
appName,
intent = appEvent
};
using (var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json"))
{
var result = await Client.PostAsync(BaseUri + "/telemetry/intent", content);
result.EnsureSuccessStatusCode();
}
}
}
}