Skip to content

Commit

Permalink
Refactor API model
Browse files Browse the repository at this point in the history
  • Loading branch information
fahminlb33 committed Feb 28, 2021
1 parent 4f3ccda commit 4fa189d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 48 deletions.
11 changes: 11 additions & 0 deletions src/KFlearning.Core/API/DeviceIdentificationModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace KFlearning.Core.API
{
public class DeviceIdentificationModel
{
public string DeviceId { get; set; }
public double RAM { get; set; }
public string CPU { get; set; }
public string OS { get; set; }
public string Architecture { get; set; }
}
}
58 changes: 10 additions & 48 deletions src/KFlearning.Core/API/TelemetryClient.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
// 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 Newtonsoft.Json;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -17,61 +7,33 @@ 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);
Task SendTelemetry(UserEngagementModel model);
Task SendIdentification(DeviceIdentificationModel model);
}

public class TelemetryClient : ITelemetryClient
{
private const string BaseUri = "https://kflearning.kodesiana.com";
private static HttpClient Client = new HttpClient();

static TelemetryClient()
{
Client.DefaultRequestHeaders.Add("X-API-Key", ApplicationConstants.ApiKey);
}

public async Task SendAppStart(string appName, string deviceId)
{
await SendTelemetry(appName, "started", deviceId);
Client.DefaultRequestHeaders.Add("X-API-Key", AppRes.ApiKey);
}

public async Task SendAppExit(string appName, string deviceId)
public async Task SendIdentification(DeviceIdentificationModel model)
{
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"))
using (var content = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json"))
{
var result = await Client.PostAsync(BaseUri + "/telemetry/device", content);
var result = await Client.PostAsync(AppRes.ApiBaseUri + "/telemetry/device", content);
result.EnsureSuccessStatusCode();
}
}

private async Task SendTelemetry(string appName, string appEvent, string deviceId)
public async Task SendTelemetry(UserEngagementModel model)
{
var body = new
{
deviceId,
appName,
intent = appEvent
};

using (var content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json"))
using (var content = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json"))
{
var result = await Client.PostAsync(BaseUri + "/telemetry/intent", content);
var result = await Client.PostAsync(AppRes.ApiBaseUri + "/telemetry/intent", content);
result.EnsureSuccessStatusCode();
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/KFlearning.Core/API/UserEngagementModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace KFlearning.Core.API
{
public class UserEngagementModel
{
public string DeviceId { get; set; }
public string AppName { get; set; }
public string Event { get; set; }
}
}

0 comments on commit 4fa189d

Please sign in to comment.