using RestSharp; using System.Collections.Generic; using UptimeSharp.Models; namespace UptimeSharp { /// /// UptimeClient /// public partial class UptimeClient { /// /// Retrieves specified monitors from UptimeRobot /// /// monitor list /// The custom uptime ratio. /// if set to true [show log]. /// if set to true [show alerts]. /// /// Monitor List /// public List GetMonitors(int[] monitorIDs = null, float[] customUptimeRatio = null, bool showLog = false, bool showAlerts = true) { RetrieveParameters parameters = new RetrieveParameters() { Monitors = monitorIDs, CustomUptimeRatio = customUptimeRatio, ShowAlerts = showAlerts, ShowLog = showLog }; return Get("getMonitors", parameters.Convert()).Items; } /// /// Retrieves a monitor from UptimeRobot /// /// a specific monitor ID /// The custom uptime ratio. /// if set to true [show log]. /// if set to true [show alerts]. /// /// The Monitor /// public Monitor GetMonitor(int monitorId, float[] customUptimeRatio = null, bool showLog = false, bool showAlerts = true) { List monitors = GetMonitors(new int[] { monitorId }, customUptimeRatio, showLog, showAlerts); return monitors.ToArray().Length > 0 ? monitors[0] : null; } /// /// Deletes a monitor /// /// a specific monitor ID /// /// Success state /// public bool DeleteMonitor(int monitorId) { return Get("deleteMonitor", Parameter("monitorID", monitorId)).Status; } /// /// Deletes a monitor /// /// The monitor. /// /// Success state /// public bool DeleteMonitor(Monitor monitor) { return DeleteMonitor(monitor.ID); } /// /// Creates a monitor. /// /// The name of the new monitor. /// The URI or IP to watch. /// The type of the monitor. /// The subtype of the monitor (if port). /// The port (only for Subtype.Custom). /// The keyword value. /// Type of the keyword. /// An ID list of existing alerts to notify. /// The HTTP username. /// The HTTP password. /// /// Success state /// public bool AddMonitor(string name, string uri, Type type = Type.HTTP, Subtype subtype = Subtype.Unknown, int? port = null, string keywordValue = null, KeywordType keywordType = KeywordType.Unknown, string[] alerts = null, string HTTPUsername = null, string HTTPPassword = null) { MonitorParameters parameters = new MonitorParameters() { Name = name, Uri = uri, Type = type, Subtype = subtype, Port = port, KeywordType = keywordType, KeywordValue = keywordValue, Alerts = alerts, HTTPPassword = HTTPPassword, HTTPUsername = HTTPUsername }; return Get("newMonitor", parameters.Convert()).Status; } /// /// Edits a monitor. /// /// The monitor. /// /// Success state /// public bool ModifyMonitor(Monitor monitor) { List alerts = null; if (monitor.Alerts != null) { monitor.Alerts.ForEach(item => alerts.Add(item.ID)); } MonitorParameters parameters = new MonitorParameters() { Name = monitor.Name, Uri = monitor.UriString != null ? monitor.UriString : null, Port = monitor.Port, HTTPPassword = monitor.HTTPPassword, HTTPUsername = monitor.HTTPUsername, KeywordType = monitor.KeywordType, KeywordValue = monitor.KeywordValue, Subtype = monitor.Subtype, Alerts = alerts != null ? alerts.ToArray() : null }; List paramList = parameters.Convert(); // fix bad behaviour in API if no subtype is submitted if(parameters.Subtype == Subtype.Unknown) { paramList.Add(Parameter("monitorSubType", 0)); } paramList.Add(Parameter("monitorID", monitor.ID)); return Get("editMonitor", paramList).Status; } } }