|
| 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 | +} |
0 commit comments