From a901e5397207b3c75f8011a876d600685e968896 Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Tue, 10 Dec 2013 22:01:20 +0100 Subject: [PATCH] update parameters and add back components --- UptimeSharp/Components/Alert.cs | 111 ++++++++++ UptimeSharp/Components/Monitor.cs | 195 ++++++++++++++++++ UptimeSharp/Models/Monitor.cs | 3 +- .../Models/Parameters/MonitorParameters.cs | 18 +- UptimeSharp/Models/Parameters/Parameters.cs | 66 ++++++ .../Models/Parameters/RetrieveParameters.cs | 27 ++- UptimeSharp/UptimeClient.cs | 2 +- UptimeSharp/UptimeSharp.csproj | 3 + 8 files changed, 417 insertions(+), 8 deletions(-) create mode 100644 UptimeSharp/Components/Alert.cs create mode 100644 UptimeSharp/Components/Monitor.cs create mode 100644 UptimeSharp/Models/Parameters/Parameters.cs diff --git a/UptimeSharp/Components/Alert.cs b/UptimeSharp/Components/Alert.cs new file mode 100644 index 0000000..24f1f1f --- /dev/null +++ b/UptimeSharp/Components/Alert.cs @@ -0,0 +1,111 @@ +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using UptimeSharp.Models; + +namespace UptimeSharp +{ + /// + /// UptimeClient + /// + public partial class UptimeClient + { + /// + /// Retrieves alerts from UptimeRobot + /// + /// Retrieve specified alert contacts by supplying IDs for them. + /// The cancellation token. + /// + public async Task> GetAlerts(string[] alertIDs = null, CancellationToken cancellationToken = default(CancellationToken)) + { + AlertResponse response = await Request("getAlertContacts", cancellationToken, new Dictionary() + { + { "alertcontacts", alertIDs != null ? string.Join("-", alertIDs) : null } + }); + + return response.Items; + } + + + /// + /// Retrieves an alert from UptimeRobot + /// + /// The alertID. + /// The cancellation token. + /// + public async Task GetAlert(string alertID, CancellationToken cancellationToken = default(CancellationToken)) + { + List alerts = await GetAlerts(new string[] { alertID }, cancellationToken); + + return alerts != null && alerts.Count > 0 ? alerts[0] : null; + } + + + /// + /// Adds an alert. + /// mobile/SMS alert contacts are not supported yet, see: http://www.uptimerobot.com/api.asp#methods + /// + /// The type. + /// The value. + /// The cancellation token. + /// + /// AlertType.SMS and AlertType.Twitter are not supported by the UptimeRobot API + public async Task AddAlert(AlertType type, string value, CancellationToken cancellationToken = default(CancellationToken)) + { + if (type == AlertType.SMS || type == AlertType.Twitter) + { + throw new UptimeSharpException("AlertType.SMS and AlertType.Twitter are not supported by the UptimeRobot API"); + } + + DefaultResponse response = await Request("newAlertContact", cancellationToken, new Dictionary() + { + { "alertContactType", ((int)type).ToString() }, + { "alertContactValue", value } + }); + + return response.Status; + } + + + /// + /// Adds an alert. + /// mobile/SMS alert contacts are not supported yet, see: http://www.uptimerobot.com/api.asp#methods + /// + /// The alert. + /// The cancellation token. + /// + public async Task AddAlert(Alert alert, CancellationToken cancellationToken = default(CancellationToken)) + { + return await AddAlert(alert.Type, alert.Value, cancellationToken); + } + + + /// + /// Removes an alert. + /// + /// The alert ID. + /// The cancellation token. + /// + public async Task DeleteAlert(string alertID, CancellationToken cancellationToken = default(CancellationToken)) + { + DefaultResponse response = await Request("deleteAlertContact", cancellationToken, new Dictionary() + { + { "alertContactID", alertID } + }); + + return response.Status; + } + + + /// + /// Removes an alert. + /// + /// The alert. + /// The cancellation token. + /// + public async Task DeleteAlert(Alert alert, CancellationToken cancellationToken = default(CancellationToken)) + { + return await DeleteAlert(alert.ID, cancellationToken); + } + } +} \ No newline at end of file diff --git a/UptimeSharp/Components/Monitor.cs b/UptimeSharp/Components/Monitor.cs new file mode 100644 index 0000000..17fee65 --- /dev/null +++ b/UptimeSharp/Components/Monitor.cs @@ -0,0 +1,195 @@ +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using UptimeSharp.Models; + +namespace UptimeSharp +{ + /// + /// UptimeClient + /// + public partial class UptimeClient + { + + /// + /// Retrieves specified monitors from UptimeRobot + /// + /// The monitor i ds. + /// The custom uptime ratio. + /// if set to true [show log]. + /// if set to true [show alerts]. + /// The cancellation token. + /// + /// Monitor List + /// + public async Task> GetMonitors( + int[] monitorIDs = null, + float[] customUptimeRatio = null, + bool showLog = false, + bool showAlerts = true, + CancellationToken cancellationToken = default(CancellationToken)) + { + RetrieveParameters parameters = new RetrieveParameters() + { + Monitors = monitorIDs, + CustomUptimeRatio = customUptimeRatio, + ShowAlerts = showAlerts, + ShowLog = showLog + }; + + RetrieveResponse response = await Request("getMonitors", cancellationToken, parameters.Convert()); + + return response.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 cancellation token. + /// + /// The Monitor + /// + public async Task GetMonitor( + int monitorId, + float[] customUptimeRatio = null, + bool showLog = false, + bool showAlerts = true, + CancellationToken cancellationToken = default(CancellationToken)) + { + List monitors = await GetMonitors(new int[] { monitorId }, customUptimeRatio, showLog, showAlerts, cancellationToken); + + return monitors != null && monitors.Count > 0 ? monitors[0] : null; + } + + + /// + /// Deletes a monitor + /// + /// a specific monitor ID + /// + /// Success state + /// + public async Task DeleteMonitor(int monitorId, CancellationToken cancellationToken = default(CancellationToken)) + { + DefaultResponse response = await Request("getMonitors", cancellationToken, new Dictionary() + { + { "monitorID", monitorId.ToString() } + }); + + return response.Status; + } + + + /// + /// Deletes a monitor + /// + /// The monitor. + /// + /// Success state + /// + public async Task DeleteMonitor(Models.Monitor monitor, CancellationToken cancellationToken = default(CancellationToken)) + { + return await DeleteMonitor(monitor.ID, cancellationToken); + } + + + /// + /// 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. + /// The cancellation token. + /// + /// Success state + /// + public async Task 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, + CancellationToken cancellationToken = default(CancellationToken)) + { + + MonitorParameters parameters = new MonitorParameters() + { + Name = name, + Uri = uri, + Type = type, + Subtype = subtype, + Port = port, + KeywordType = keywordType, + KeywordValue = keywordValue, + Alerts = alerts, + HTTPPassword = HTTPPassword, + HTTPUsername = HTTPUsername + }; + + DefaultResponse response = await Request("getMonitors", cancellationToken, parameters.Convert()); + return response.Status; + } + + + /// + /// Edits a monitor. + /// + /// The monitor. + /// The cancellation token. + /// + /// Success state + /// + public async Task ModifyMonitor(Models.Monitor monitor, CancellationToken cancellationToken = default(CancellationToken)) + { + List alerts = null; + + if (monitor.Alerts != null) + { + alerts = monitor.Alerts.Select(item => item.ID).ToList(); + } + + MonitorParameters parameters = new MonitorParameters() + { + Name = monitor.Name, + Uri = monitor.Uri != null ? monitor.Uri : 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 + }; + + Dictionary paramList = parameters.Convert(); + + // fix bad behaviour in API if no subtype is submitted + if (parameters.Subtype == Subtype.Unknown) + { + paramList.Add("monitorSubType", "0"); + } + + paramList.Add("monitorID", monitor.ID.ToString()); + + DefaultResponse response = await Request("editMonitor", cancellationToken, paramList); + return response.Status; + } + } +} \ No newline at end of file diff --git a/UptimeSharp/Models/Monitor.cs b/UptimeSharp/Models/Monitor.cs index 444ae7e..451a692 100644 --- a/UptimeSharp/Models/Monitor.cs +++ b/UptimeSharp/Models/Monitor.cs @@ -1,6 +1,5 @@ using Newtonsoft.Json; using PropertyChanged; -using System; using System.Collections.Generic; namespace UptimeSharp.Models @@ -37,7 +36,7 @@ namespace UptimeSharp.Models /// The URI. /// [JsonIgnore] - public Uri Uri { get; set; } + public string Uri { get; set; } /// /// Gets or sets the port. diff --git a/UptimeSharp/Models/Parameters/MonitorParameters.cs b/UptimeSharp/Models/Parameters/MonitorParameters.cs index 9ac1185..0182aa3 100644 --- a/UptimeSharp/Models/Parameters/MonitorParameters.cs +++ b/UptimeSharp/Models/Parameters/MonitorParameters.cs @@ -1,12 +1,12 @@ -using System; -using System.Collections.Generic; - + +using System.Runtime.Serialization; namespace UptimeSharp.Models { /// /// All parameters which can be passed for monitor modifications /// - internal class MonitorParameters + [DataContract] + internal class MonitorParameters : Parameters { /// /// Gets or sets the name. @@ -14,6 +14,7 @@ namespace UptimeSharp.Models /// /// The name. /// + [DataMember(Name = "monitorFriendlyName")] public string Name { get; set; } /// @@ -22,6 +23,7 @@ namespace UptimeSharp.Models /// /// The URI. /// + [DataMember(Name = "monitorURL")] public string Uri { get; set; } /// @@ -31,6 +33,7 @@ namespace UptimeSharp.Models /// /// The port. /// + [DataMember(Name = "monitorPort")] public int? Port { get; set; } /// @@ -39,6 +42,7 @@ namespace UptimeSharp.Models /// /// The HTTP password. /// + [DataMember(Name = "monitorHTTPPassword")] public string HTTPPassword { get; set; } /// @@ -47,6 +51,7 @@ namespace UptimeSharp.Models /// /// The HTTP username. /// + [DataMember(Name = "monitorHTTPUsername")] public string HTTPUsername { get; set; } /// @@ -55,6 +60,7 @@ namespace UptimeSharp.Models /// /// The type of the keyword. /// + [DataMember(Name = "monitorKeywordType")] public KeywordType KeywordType { get; set; } /// @@ -63,6 +69,7 @@ namespace UptimeSharp.Models /// /// The keyword value. /// + [DataMember(Name = "monitorKeywordValue")] public string KeywordValue { get; set; } /// @@ -71,6 +78,7 @@ namespace UptimeSharp.Models /// /// The type. /// + [DataMember(Name = "monitorType")] public Type Type { get; set; } /// @@ -79,6 +87,7 @@ namespace UptimeSharp.Models /// /// The subtype. /// + [DataMember(Name = "monitorSubType")] public Subtype Subtype { get; set; } /// @@ -87,6 +96,7 @@ namespace UptimeSharp.Models /// /// The alert contacts. /// + [DataMember(Name = "monitorAlertContacts")] public string[] Alerts { get; set; } } } diff --git a/UptimeSharp/Models/Parameters/Parameters.cs b/UptimeSharp/Models/Parameters/Parameters.cs new file mode 100644 index 0000000..a436f67 --- /dev/null +++ b/UptimeSharp/Models/Parameters/Parameters.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Runtime.Serialization; + +namespace UptimeSharp.Models +{ + /// + /// Parameter + /// + internal class Parameters + { + /// + /// Converts an object to a list of HTTP Get parameters. + /// + /// + public Dictionary Convert() + { + // store HTTP parameters here + Dictionary parameterDict = new Dictionary(); + + // get object properties + IEnumerable properties = this.GetType() + .GetProperties(BindingFlags.Instance | BindingFlags.Public) + .Where(p => Attribute.IsDefined(p, typeof(DataMemberAttribute))); + + // gather attributes of object + foreach (PropertyInfo propertyInfo in properties) + { + DataMemberAttribute attribute = (DataMemberAttribute)propertyInfo.GetCustomAttributes(typeof(DataMemberAttribute‌), false).FirstOrDefault(); + string name = attribute.Name ?? propertyInfo.Name.ToLower(); + object value = propertyInfo.GetValue(this, null); + + // invalid parameter + if (value == null) + { + continue; + } + + // convert array to comma-seperated list + if (value is IEnumerable && value.GetType().GetElementType() == typeof(string)) + { + value = string.Join(",", ((IEnumerable)value).Cast().Select(x => x.ToString()).ToArray()); + } + + // convert booleans + if (value is bool) + { + value = System.Convert.ToBoolean(value) ? "1" : "0"; + } + + // convert DateTime to UNIX timestamp + if (value is DateTime) + { + value = (int)((DateTime)value - new DateTime(1970, 1, 1)).TotalSeconds; + } + + parameterDict.Add(name, value.ToString()); + } + + return parameterDict; + } + } +} \ No newline at end of file diff --git a/UptimeSharp/Models/Parameters/RetrieveParameters.cs b/UptimeSharp/Models/Parameters/RetrieveParameters.cs index 4aea8b1..d77b5ba 100644 --- a/UptimeSharp/Models/Parameters/RetrieveParameters.cs +++ b/UptimeSharp/Models/Parameters/RetrieveParameters.cs @@ -1,10 +1,13 @@  +using System.Collections.Generic; +using System.Runtime.Serialization; namespace UptimeSharp.Models { /// /// All parameters which can be passed for monitor retrieval /// - internal class RetrieveParameters + [DataContract] + internal class RetrieveParameters : Parameters { /// /// List of monitor ids @@ -12,6 +15,7 @@ namespace UptimeSharp.Models /// /// The monitors. /// + [DataMember(Name = "monitors")] public int[] Monitors { get; set; } /// @@ -20,6 +24,7 @@ namespace UptimeSharp.Models /// /// The custom uptime ratio. /// + [DataMember(Name = "customUptimeRatio")] public float[] CustomUptimeRatio { get; set; } /// @@ -28,6 +33,7 @@ namespace UptimeSharp.Models /// /// The log bool. /// + [DataMember(Name = "logs")] public bool? ShowLog { get; set; } /// @@ -36,6 +42,25 @@ namespace UptimeSharp.Models /// /// The alert contacts bool. /// + [DataMember(Name = "showMonitorAlertContacts")] public bool? ShowAlerts { get; set; } + + + /// + /// Converts an object to a list of HTTP Get parameters. + /// + /// + public Dictionary Convert() + { + Dictionary parameters = base.Convert(); + + if (ShowLog.HasValue) + { + parameters.Add("alertContacts", parameters["showLog"]); + parameters.Add("showTimezone", parameters["showLog"]); + } + + return parameters; + } } } diff --git a/UptimeSharp/UptimeClient.cs b/UptimeSharp/UptimeClient.cs index 1223b36..5845173 100644 --- a/UptimeSharp/UptimeClient.cs +++ b/UptimeSharp/UptimeClient.cs @@ -13,7 +13,7 @@ namespace UptimeSharp /// /// UptimeClient /// - public class UptimeClient : IUptimeClient + public partial class UptimeClient : IUptimeClient { /// /// REST client used for the API communication diff --git a/UptimeSharp/UptimeSharp.csproj b/UptimeSharp/UptimeSharp.csproj index 308c93c..05a7900 100644 --- a/UptimeSharp/UptimeSharp.csproj +++ b/UptimeSharp/UptimeSharp.csproj @@ -39,11 +39,14 @@ + + +