From 5f707840c0522e664dbbf6ad2e34e0c436122c24 Mon Sep 17 00:00:00 2001 From: ceee Date: Sun, 18 Aug 2013 14:26:01 +0200 Subject: [PATCH] Add method with 3 overloads --- UptimeSharp/Components/Monitor.cs | 92 +++++++++++ UptimeSharp/Models/Monitor.cs | 1 + .../Models/Parameters/MonitorParameters.cs | 144 ++++++++++++++++++ UptimeSharp/Models/Response/Get.cs | 3 +- UptimeSharp/UptimeSharp.csproj | 1 + 5 files changed, 240 insertions(+), 1 deletion(-) create mode 100644 UptimeSharp/Models/Parameters/MonitorParameters.cs diff --git a/UptimeSharp/Components/Monitor.cs b/UptimeSharp/Components/Monitor.cs index 7ae7259..a4ae1f4 100644 --- a/UptimeSharp/Components/Monitor.cs +++ b/UptimeSharp/Components/Monitor.cs @@ -69,6 +69,7 @@ namespace UptimeSharp } + /// /// Deletes a monitor /// @@ -94,5 +95,96 @@ namespace UptimeSharp { return Delete(monitor.ID); } + + + + /// + /// Creates a HTTP/IP monitor. + /// + /// The name of the new monitor. + /// The URI or IP to watch. + /// A ID list of existing alerts to notify. + /// + /// success state + /// + public bool Add(string name, string uri, int[] alerts = null) + { + MonitorParameters parameters = new MonitorParameters() + { + Name = name, + Uri = uri, + Type = Type.HTTP, + Alerts = alerts + }; + + return Get("newMonitor", parameters.Convert()).Status; + } + + + /// + /// Creates a Port monitor. + /// + /// The name of the new monitor. + /// The URI or IP to watch. + /// The subtype of the port. + /// The port (only for Subtype.Custom). + /// A ID list of existing alerts to notify. + /// + /// success state + /// + public bool Add(string name, string uri, Subtype subtype, int? port = null, int[] alerts = null) + { + MonitorParameters parameters = new MonitorParameters() + { + Name = name, + Uri = uri, + Type = Type.Port, + Subtype = subtype, + Port = port, + Alerts = alerts + }; + + return Get("newMonitor", parameters.Convert()).Status; + } + + + /// + /// Creates a Keyword monitor. + /// + /// The name of the new monitor. + /// The URI or IP to watch. + /// Type of the keyword. + /// The keyword value. + /// A ID list of existing alerts to notify. + /// + /// success state + /// + public bool Add(string name, string uri, string keyword, KeywordType keywordType = KeywordType.Exists, int[] alerts = null) + { + MonitorParameters parameters = new MonitorParameters() + { + Name = name, + Uri = uri, + Type = Type.Keyword, + KeywordValue = keyword, + KeywordType = keywordType, + Alerts = alerts + }; + + return Get("newMonitor", parameters.Convert()).Status; + } + + + /// + /// Creates a monitor. + /// + /// The monitor. + /// + /// success state + /// + public bool Add(MonitorParameters parameters) + { + return Get("newMonitor", parameters.Convert()).Status; + } } } \ No newline at end of file diff --git a/UptimeSharp/Models/Monitor.cs b/UptimeSharp/Models/Monitor.cs index f81085f..a28cd66 100644 --- a/UptimeSharp/Models/Monitor.cs +++ b/UptimeSharp/Models/Monitor.cs @@ -5,6 +5,7 @@ using System.Text; using System.Runtime.Serialization; using System.Net; using ServiceStack.Text; +using RestSharp; namespace UptimeSharp.Models { diff --git a/UptimeSharp/Models/Parameters/MonitorParameters.cs b/UptimeSharp/Models/Parameters/MonitorParameters.cs new file mode 100644 index 0000000..24d604c --- /dev/null +++ b/UptimeSharp/Models/Parameters/MonitorParameters.cs @@ -0,0 +1,144 @@ +using RestSharp; +using System; +using System.Collections.Generic; + +namespace UptimeSharp.Models +{ + /// + /// All parameters which can be passed for monitor modifications + /// + public class MonitorParameters + { + /// + /// Gets or sets the name. + /// + /// + /// The name. + /// + public string Name { get; set; } + + /// + /// Gets or sets the URI. + /// + /// + /// The URI. + /// + public string Uri { get; set; } + + /// + /// Gets or sets the port. + /// Only for port monitoring. + /// + /// + /// The port. + /// + public int? Port { get; set; } + + /// + /// Gets or sets the HTTP password. + /// + /// + /// The HTTP password. + /// + public string HTTPPassword { get; set; } + + /// + /// Gets or sets the HTTP username. + /// + /// + /// The HTTP username. + /// + public string HTTPUsername { get; set; } + + /// + /// Gets or sets the type of the keyword. + /// + /// + /// The type of the keyword. + /// + public KeywordType KeywordType { get; set; } + + /// + /// Gets or sets the keyword value. + /// + /// + /// The keyword value. + /// + public string KeywordValue { get; set; } + + /// + /// Gets or sets the type. + /// + /// + /// The type. + /// + public Type Type { get; set; } + + /// + /// Gets or sets the subtype. + /// + /// + /// The subtype. + /// + public Subtype Subtype { get; set; } + + /// + /// Gets or sets the alerts. + /// + /// + /// The alert contacts. + /// + public int[] Alerts { get; set; } + + + /// + /// Converts this instance to a parameter list. + /// + /// Parameter list + public List Convert() + { + List parameters = new List() + { + Utilities.CreateParam("monitorFriendlyName", Name), + Utilities.CreateParam("monitorURL", Uri), + Utilities.CreateParam("monitorType", (int)Type) + }; + + // special params for port listener + if (Type == Type.Port && Subtype != Subtype.Unknown && Port.HasValue) + { + parameters.Add(Utilities.CreateParam("monitorSubType", (int)Subtype)); + + if (Subtype == Subtype.Custom) + { + parameters.Add(Utilities.CreateParam("monitorPort", Port)); + } + } + + // keyword listener + if (Type == Type.Keyword && !string.IsNullOrEmpty(KeywordValue)) + { + parameters.Add(Utilities.CreateParam("monitorKeywordType", (int)KeywordType)); + parameters.Add(Utilities.CreateParam("monitorKeywordValue", KeywordValue)); + } + + // HTTP basic auth credentials + if (!string.IsNullOrEmpty(HTTPUsername)) + { + parameters.Add(Utilities.CreateParam("monitorHTTPUsername", HTTPUsername)); + } + if (!string.IsNullOrEmpty(HTTPPassword)) + { + parameters.Add(Utilities.CreateParam("monitorHTTPPassword", HTTPPassword)); + } + + // alert notifications + if (Alerts != null && Alerts.Length > 0) + { + parameters.Add(Utilities.CreateParam("monitorAlertContacts", string.Join("-", Alerts))); + } + + return parameters; + } + } +} diff --git a/UptimeSharp/Models/Response/Get.cs b/UptimeSharp/Models/Response/Get.cs index 3e4233c..4a29ba0 100644 --- a/UptimeSharp/Models/Response/Get.cs +++ b/UptimeSharp/Models/Response/Get.cs @@ -28,7 +28,8 @@ namespace UptimeSharp.Models [IgnoreDataMember] public List Items { - get { + get + { return ItemDictionary["monitor"]; } } diff --git a/UptimeSharp/UptimeSharp.csproj b/UptimeSharp/UptimeSharp.csproj index 3d507c9..ff05dd8 100644 --- a/UptimeSharp/UptimeSharp.csproj +++ b/UptimeSharp/UptimeSharp.csproj @@ -58,6 +58,7 @@ +