Skip to content

Commit f6b482f

Browse files
author
mwatson
committed
New API methods
1 parent e74e84b commit f6b482f

File tree

7 files changed

+258
-30
lines changed

7 files changed

+258
-30
lines changed

Dlls/StackifyLib/StackifyLib.dll

-6.5 KB
Binary file not shown.

Src/StackifyLib/API.cs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net;
5+
using System.Text;
6+
using Newtonsoft.Json;
7+
using StackifyLib.Utils;
8+
9+
namespace StackifyLib
10+
{
11+
public class API
12+
{
13+
private static Utils.HttpClient client;
14+
static API()
15+
{
16+
client = new HttpClient(null, null);
17+
}
18+
19+
public static Models.Device[] GetDeviceList()
20+
{
21+
22+
var response = client.SendJsonAndGetResponse(System.Web.VirtualPathUtility.AppendTrailingSlash(client.BaseAPIUrl) +
23+
"API/Device/GetAll",
24+
null);
25+
26+
if (response.StatusCode == HttpStatusCode.OK)
27+
{
28+
return JsonConvert.DeserializeObject<Models.Device[]>(response.ResponseText);
29+
}
30+
else
31+
{
32+
return null;
33+
}
34+
}
35+
36+
public static bool RemoveServerByID(int id, bool uninstallAgent = false)
37+
{
38+
var response = client.POSTAndGetResponse(System.Web.VirtualPathUtility.AppendTrailingSlash(client.BaseAPIUrl) +
39+
"API/Device/RemoveServerByID/?id=" + id + "&uninstallAgent=" + uninstallAgent, null);
40+
41+
return response.StatusCode == HttpStatusCode.OK;
42+
}
43+
44+
public static bool RemoveServerByName(string name, bool uninstallAgent = false)
45+
{
46+
var response = client.POSTAndGetResponse(System.Web.VirtualPathUtility.AppendTrailingSlash(client.BaseAPIUrl) +
47+
"API/Device/RemoveServerByID/?name=" + name + "&uninstallAgent=" + uninstallAgent,
48+
null);
49+
50+
return response.StatusCode == HttpStatusCode.OK;
51+
}
52+
53+
public static bool UninstallAgentByID(int id)
54+
{
55+
var response = client.POSTAndGetResponse(System.Web.VirtualPathUtility.AppendTrailingSlash(client.BaseAPIUrl) +
56+
"API/Device/UninstallAgentByID/?id=" + id,
57+
null);
58+
59+
return response.StatusCode == HttpStatusCode.OK;
60+
}
61+
62+
public static bool UninstallAgentByName(string name)
63+
{
64+
var response = client.POSTAndGetResponse(System.Web.VirtualPathUtility.AppendTrailingSlash(client.BaseAPIUrl) +
65+
"API/Device/UninstallAgentByName/?name=" + name,
66+
null);
67+
68+
return response.StatusCode == HttpStatusCode.OK;
69+
}
70+
71+
public static bool? ConfigureAlerts(Dictionary<int, string> devices)
72+
{
73+
var response = client.SendJsonAndGetResponse(System.Web.VirtualPathUtility.AppendTrailingSlash(client.BaseAPIUrl) +
74+
"API/Device/ConfigureAlerts",
75+
JsonConvert.SerializeObject(devices));
76+
77+
if (response.StatusCode == HttpStatusCode.OK)
78+
{
79+
return JsonConvert.DeserializeObject<bool>(response.ResponseText);
80+
}
81+
else
82+
{
83+
return null;
84+
}
85+
}
86+
87+
public static Models.Monitor[] GetDeviceMonitors(int clientDeviceId)
88+
{
89+
var response = client.SendJsonAndGetResponse(System.Web.VirtualPathUtility.AppendTrailingSlash(client.BaseAPIUrl) +
90+
"API/Device/Monitors/" + clientDeviceId.ToString(),
91+
null);
92+
93+
if (response.StatusCode == HttpStatusCode.OK)
94+
{
95+
return JsonConvert.DeserializeObject<Models.Monitor[]>(response.ResponseText);
96+
}
97+
else
98+
{
99+
return null;
100+
}
101+
}
102+
103+
public static Models.MonitorMetric GetMetrics(int monitorID, DateTimeOffset startDateUtc, DateTimeOffset endDateUtc, short pointSizeInMinutes = 5)
104+
{
105+
var response = client.SendJsonAndGetResponse(System.Web.VirtualPathUtility.AppendTrailingSlash(client.BaseAPIUrl) +
106+
"API/Device/MonitorMetrics/" + string.Format("?monitorID={0}&startDateUtc={1}&endDateUtc={2}&pointSizeInMinutes={3}", monitorID, startDateUtc.UtcDateTime.ToString("o"), endDateUtc.UtcDateTime.ToString("o"), pointSizeInMinutes),
107+
null);
108+
109+
if (response.StatusCode == HttpStatusCode.OK)
110+
{
111+
return JsonConvert.DeserializeObject<Models.MonitorMetric>(response.ResponseText);
112+
}
113+
else
114+
{
115+
return null;
116+
}
117+
}
118+
}
119+
}

Src/StackifyLib/Internal/Logs/LogClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ private Models.LogMsgGroup CreateDefaultMsgGroup()
349349

350350
StackifyAPILogger.Log("Sending " + messages.Length.ToString() + " log messages");
351351
var task =
352-
_HttpClient.SendAndGetResponseAsync(
352+
_HttpClient.SendJsonAndGetResponseAsync(
353353
urlToUse,
354354
jsonData, jsonData.Length > 5000);
355355

@@ -411,7 +411,7 @@ private Models.LogMsgGroup CreateDefaultMsgGroup()
411411

412412
StackifyAPILogger.Log("Sending " + messages.Length.ToString() + " log messages via send multi groups");
413413
var task =
414-
_HttpClient.SendAndGetResponseAsync(
414+
_HttpClient.SendJsonAndGetResponseAsync(
415415
urlToUse,
416416
jsonData, jsonData.Length > 5000);
417417

Src/StackifyLib/Internal/Metrics/MetricClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ private static bool UploadMetrics(List<MetricAggregate> metrics)
524524

525525
string jsonData = JsonConvert.SerializeObject(records);
526526

527-
var response = _httpClient.SendAndGetResponse(
527+
var response = _httpClient.SendJsonAndGetResponse(
528528
System.Web.VirtualPathUtility.AppendTrailingSlash(_httpClient.BaseAPIUrl) +
529529
"Metrics/SubmitMetricsByID",
530530
jsonData);
@@ -574,7 +574,7 @@ private static GetMetricResponse GetMonitorInfo(MetricAggregate metric)
574574

575575

576576
var response =
577-
_httpClient.SendAndGetResponse(
577+
_httpClient.SendJsonAndGetResponse(
578578
System.Web.VirtualPathUtility.AppendTrailingSlash(_httpClient.BaseAPIUrl) +
579579
"Metrics/GetMetricInfo",
580580
jsonData);

Src/StackifyLib/Monitors.cs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,12 @@ namespace StackifyLib
1313
/// </summary>
1414
public class Monitors
1515
{
16-
public Models.Device[] GetDeviceList()
17-
{
18-
Utils.HttpClient client = new HttpClient(null, null);
19-
var response = client.SendAndGetResponse(System.Web.VirtualPathUtility.AppendTrailingSlash(client.BaseAPIUrl) +
20-
"API/Device/GetAll",
21-
null);
22-
23-
if (response.StatusCode == HttpStatusCode.OK)
24-
{
25-
return JsonConvert.DeserializeObject<Models.Device[]>(response.ResponseText);
26-
}
27-
else
28-
{
29-
return null;
30-
}
31-
}
3216

17+
[Obsolete("Use API class instead", true)]
3318
public bool? ConfigureAlerts(Dictionary<int, string> devices)
3419
{
3520
Utils.HttpClient client = new HttpClient(null, null);
36-
var response = client.SendAndGetResponse(System.Web.VirtualPathUtility.AppendTrailingSlash(client.BaseAPIUrl) +
21+
var response = client.SendJsonAndGetResponse(System.Web.VirtualPathUtility.AppendTrailingSlash(client.BaseAPIUrl) +
3722
"API/Device/ConfigureAlerts",
3823
JsonConvert.SerializeObject(devices));
3924

@@ -47,10 +32,11 @@ public Models.Device[] GetDeviceList()
4732
}
4833
}
4934

35+
[Obsolete("Use API class instead", true)]
5036
public Models.Monitor[] GetDeviceMonitors(int clientDeviceId)
5137
{
5238
Utils.HttpClient client = new HttpClient(null, null);
53-
var response = client.SendAndGetResponse(System.Web.VirtualPathUtility.AppendTrailingSlash(client.BaseAPIUrl) +
39+
var response = client.SendJsonAndGetResponse(System.Web.VirtualPathUtility.AppendTrailingSlash(client.BaseAPIUrl) +
5440
"API/Device/Monitors/" + clientDeviceId.ToString(),
5541
null);
5642

@@ -64,10 +50,11 @@ public Models.Monitor[] GetDeviceMonitors(int clientDeviceId)
6450
}
6551
}
6652

53+
[Obsolete("Use API class instead", true)]
6754
public Models.MonitorMetric GetMetrics(int monitorID, DateTimeOffset startDateUtc, DateTimeOffset endDateUtc, short pointSizeInMinutes = 5)
6855
{
6956
Utils.HttpClient client = new HttpClient(null, null);
70-
var response = client.SendAndGetResponse(System.Web.VirtualPathUtility.AppendTrailingSlash(client.BaseAPIUrl) +
57+
var response = client.SendJsonAndGetResponse(System.Web.VirtualPathUtility.AppendTrailingSlash(client.BaseAPIUrl) +
7158
"API/Device/MonitorMetrics/" + string.Format("?monitorID={0}&startDateUtc={1}&endDateUtc={2}&pointSizeInMinutes={3}", monitorID, startDateUtc.UtcDateTime.ToString("o"), endDateUtc.UtcDateTime.ToString("o"), pointSizeInMinutes),
7259
null);
7360

Src/StackifyLib/StackifyLib.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<Reference Include="System.Data" />
5353
</ItemGroup>
5454
<ItemGroup>
55+
<Compile Include="API.cs" />
5556
<Compile Include="Config.cs" />
5657
<Compile Include="Internal\Logs\LogClient.cs" />
5758
<Compile Include="Internal\Logs\LogQueue.cs" />

0 commit comments

Comments
 (0)