Add method with 3 overloads

This commit is contained in:
2013-08-18 14:26:01 +02:00
parent ef46303139
commit 5f707840c0
5 changed files with 240 additions and 1 deletions
+92
View File
@@ -69,6 +69,7 @@ namespace UptimeSharp
}
/// <summary>
/// Deletes a monitor
/// </summary>
@@ -94,5 +95,96 @@ namespace UptimeSharp
{
return Delete(monitor.ID);
}
/// <summary>
/// Creates a HTTP/IP monitor.
/// </summary>
/// <param name="name">The name of the new monitor.</param>
/// <param name="uri">The URI or IP to watch.</param>
/// <param name="alerts">A ID list of existing alerts to notify.</param>
/// <returns>
/// success state
/// </returns>
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<Default>("newMonitor", parameters.Convert()).Status;
}
/// <summary>
/// Creates a Port monitor.
/// </summary>
/// <param name="name">The name of the new monitor.</param>
/// <param name="uri">The URI or IP to watch.</param>
/// <param name="subtype">The subtype of the port.</param>
/// <param name="port">The port (only for Subtype.Custom).</param>
/// <param name="alerts">A ID list of existing alerts to notify.</param>
/// <returns>
/// success state
/// </returns>
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<Default>("newMonitor", parameters.Convert()).Status;
}
/// <summary>
/// Creates a Keyword monitor.
/// </summary>
/// <param name="name">The name of the new monitor.</param>
/// <param name="uri">The URI or IP to watch.</param>
/// <param name="keywordType">Type of the keyword.</param>
/// <param name="keywordValue">The keyword value.</param>
/// <param name="alerts">A ID list of existing alerts to notify.</param>
/// <returns>
/// success state
/// </returns>
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<Default>("newMonitor", parameters.Convert()).Status;
}
/// <summary>
/// Creates a monitor.
/// </summary>
/// <param name="monitor">The monitor.</param>
/// <returns>
/// success state
/// </returns>
public bool Add(MonitorParameters parameters)
{
return Get<Default>("newMonitor", parameters.Convert()).Status;
}
}
}
+1
View File
@@ -5,6 +5,7 @@ using System.Text;
using System.Runtime.Serialization;
using System.Net;
using ServiceStack.Text;
using RestSharp;
namespace UptimeSharp.Models
{
@@ -0,0 +1,144 @@
using RestSharp;
using System;
using System.Collections.Generic;
namespace UptimeSharp.Models
{
/// <summary>
/// All parameters which can be passed for monitor modifications
/// </summary>
public class MonitorParameters
{
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
public string Name { get; set; }
/// <summary>
/// Gets or sets the URI.
/// </summary>
/// <value>
/// The URI.
/// </value>
public string Uri { get; set; }
/// <summary>
/// Gets or sets the port.
/// Only for port monitoring.
/// </summary>
/// <value>
/// The port.
/// </value>
public int? Port { get; set; }
/// <summary>
/// Gets or sets the HTTP password.
/// </summary>
/// <value>
/// The HTTP password.
/// </value>
public string HTTPPassword { get; set; }
/// <summary>
/// Gets or sets the HTTP username.
/// </summary>
/// <value>
/// The HTTP username.
/// </value>
public string HTTPUsername { get; set; }
/// <summary>
/// Gets or sets the type of the keyword.
/// </summary>
/// <value>
/// The type of the keyword.
/// </value>
public KeywordType KeywordType { get; set; }
/// <summary>
/// Gets or sets the keyword value.
/// </summary>
/// <value>
/// The keyword value.
/// </value>
public string KeywordValue { get; set; }
/// <summary>
/// Gets or sets the type.
/// </summary>
/// <value>
/// The type.
/// </value>
public Type Type { get; set; }
/// <summary>
/// Gets or sets the subtype.
/// </summary>
/// <value>
/// The subtype.
/// </value>
public Subtype Subtype { get; set; }
/// <summary>
/// Gets or sets the alerts.
/// </summary>
/// <value>
/// The alert contacts.
/// </value>
public int[] Alerts { get; set; }
/// <summary>
/// Converts this instance to a parameter list.
/// </summary>
/// <returns>Parameter list</returns>
public List<Parameter> Convert()
{
List<Parameter> parameters = new List<Parameter>()
{
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;
}
}
}
+2 -1
View File
@@ -28,7 +28,8 @@ namespace UptimeSharp.Models
[IgnoreDataMember]
public List<Monitor> Items
{
get {
get
{
return ItemDictionary["monitor"];
}
}
+1
View File
@@ -58,6 +58,7 @@
<Compile Include="Models\Log.cs" />
<Compile Include="Models\Monitor.cs" />
<Compile Include="Models\Parameters\GetParameters.cs" />
<Compile Include="Models\Parameters\MonitorParameters.cs" />
<Compile Include="Models\Response\Default.cs" />
<Compile Include="Models\Response\Get.cs" />
<Compile Include="Models\Response\ResponseBase.cs" />