diff --git a/UptimeSharp/Components/Alert.cs b/UptimeSharp/Components/Alert.cs new file mode 100644 index 0000000..55e5b0e --- /dev/null +++ b/UptimeSharp/Components/Alert.cs @@ -0,0 +1,58 @@ +using RestSharp; +using System.Collections.Generic; +using UptimeSharp.Models; + +namespace UptimeSharp +{ + /// + /// UptimeClient + /// + public partial class UptimeClient + { + /// + /// Retrieves alerts from UptimeRobot + /// + /// Retrieve specified alert contacts by supplying IDs for them. + /// + public List RetrieveAlerts(int[] alertIDs = null) + { + return Get("getAlertContacts").Items; + } + + + /// + /// Adds an alert. + /// mobile/SMS alert contacts are not supported yet, see: http://www.uptimerobot.com/api.asp#methods + /// + /// The type. + /// The value. + /// + public bool AddAlert(AlertType type, string value) + { + List parameters = new List() + { + Utilities.CreateParam("alertContactType", (int)type), + Utilities.CreateParam("alertContactValue", value) + }; + + return Get("newAlertContact", parameters).Status; + } + + + /// + /// Removes an alert. + /// + /// The type. + /// The value. + /// + public bool DeleteAlert(int alertID) + { + List parameters = new List() + { + Utilities.CreateParam("alertContactID", alertID) + }; + + return Get("deleteAlertContact", parameters).Status; + } + } +} \ No newline at end of file diff --git a/UptimeSharp/Models/Alert.cs b/UptimeSharp/Models/Alert.cs index 2688595..1f790b1 100644 --- a/UptimeSharp/Models/Alert.cs +++ b/UptimeSharp/Models/Alert.cs @@ -26,6 +26,15 @@ namespace UptimeSharp.Models [DataMember(Name = "type")] public AlertType Type { get; set; } + /// + /// Gets or sets the alert status. + /// + /// + /// The status. + /// + [DataMember(Name = "status")] + public AlertStatus Status { get; set; } + /// /// Gets or sets the alert value. /// @@ -60,4 +69,28 @@ namespace UptimeSharp.Models /// Boxcar = 4 } + + + /// + /// The status of the alert contact. + /// + public enum AlertStatus + { + /// + /// Unknown + /// + Unknown, + /// + /// Not activated + /// + NotActicated = 0, + /// + /// Paused + /// + Paused = 1, + /// + /// Active + /// + Active = 2 + } } diff --git a/UptimeSharp/Models/Response/AlertResponse.cs b/UptimeSharp/Models/Response/AlertResponse.cs new file mode 100644 index 0000000..7227c97 --- /dev/null +++ b/UptimeSharp/Models/Response/AlertResponse.cs @@ -0,0 +1,37 @@ +using System.Collections.Generic; +using System.Runtime.Serialization; + +namespace UptimeSharp.Models +{ + /// + /// Alert Response + /// + [DataContract] + internal class AlertResponse : ResponseBase + { + /// + /// Gets or sets the item dictionary. + /// The list is 2 layers deep, so this one is necessary :-/ + /// + /// + /// The item dictionary. + /// + [DataMember(Name = "alertcontacts")] + public Dictionary> ItemDictionary { get; set; } + + /// + /// Gets the items. + /// + /// + /// The items. + /// + [IgnoreDataMember] + public List Items + { + get + { + return ItemDictionary["alertcontact"]; + } + } + } +} diff --git a/UptimeSharp/UptimeClient.cs b/UptimeSharp/UptimeClient.cs index 7a15a9c..506acaa 100644 --- a/UptimeSharp/UptimeClient.cs +++ b/UptimeSharp/UptimeClient.cs @@ -89,6 +89,9 @@ namespace UptimeSharp /// protected T Request(RestRequest request) where T : new() { + // fix content type if wrong determined by RestSharp + request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; }; + IRestResponse response = _restClient.Execute(request); LastRequestData = response; diff --git a/UptimeSharp/UptimeSharp.csproj b/UptimeSharp/UptimeSharp.csproj index 00d8f66..b6dd36d 100644 --- a/UptimeSharp/UptimeSharp.csproj +++ b/UptimeSharp/UptimeSharp.csproj @@ -52,6 +52,7 @@ + @@ -59,6 +60,7 @@ +