update parameters and add back components

This commit is contained in:
2013-12-10 22:01:20 +01:00
parent 9ecb00fceb
commit a901e53972
8 changed files with 417 additions and 8 deletions
+111
View File
@@ -0,0 +1,111 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using UptimeSharp.Models;
namespace UptimeSharp
{
/// <summary>
/// UptimeClient
/// </summary>
public partial class UptimeClient
{
/// <summary>
/// Retrieves alerts from UptimeRobot
/// </summary>
/// <param name="alertIDs">Retrieve specified alert contacts by supplying IDs for them.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
public async Task<List<Alert>> GetAlerts(string[] alertIDs = null, CancellationToken cancellationToken = default(CancellationToken))
{
AlertResponse response = await Request<AlertResponse>("getAlertContacts", cancellationToken, new Dictionary<string, string>()
{
{ "alertcontacts", alertIDs != null ? string.Join("-", alertIDs) : null }
});
return response.Items;
}
/// <summary>
/// Retrieves an alert from UptimeRobot
/// </summary>
/// <param name="alertID">The alertID.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
public async Task<Alert> GetAlert(string alertID, CancellationToken cancellationToken = default(CancellationToken))
{
List<Alert> alerts = await GetAlerts(new string[] { alertID }, cancellationToken);
return alerts != null && alerts.Count > 0 ? alerts[0] : null;
}
/// <summary>
/// Adds an alert.
/// mobile/SMS alert contacts are not supported yet, see: http://www.uptimerobot.com/api.asp#methods
/// </summary>
/// <param name="type">The type.</param>
/// <param name="value">The value.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
/// <exception cref="UptimeSharpException">AlertType.SMS and AlertType.Twitter are not supported by the UptimeRobot API</exception>
public async Task<bool> 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<DefaultResponse>("newAlertContact", cancellationToken, new Dictionary<string, string>()
{
{ "alertContactType", ((int)type).ToString() },
{ "alertContactValue", value }
});
return response.Status;
}
/// <summary>
/// Adds an alert.
/// mobile/SMS alert contacts are not supported yet, see: http://www.uptimerobot.com/api.asp#methods
/// </summary>
/// <param name="alert">The alert.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
public async Task<bool> AddAlert(Alert alert, CancellationToken cancellationToken = default(CancellationToken))
{
return await AddAlert(alert.Type, alert.Value, cancellationToken);
}
/// <summary>
/// Removes an alert.
/// </summary>
/// <param name="alertID">The alert ID.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
public async Task<bool> DeleteAlert(string alertID, CancellationToken cancellationToken = default(CancellationToken))
{
DefaultResponse response = await Request<DefaultResponse>("deleteAlertContact", cancellationToken, new Dictionary<string, string>()
{
{ "alertContactID", alertID }
});
return response.Status;
}
/// <summary>
/// Removes an alert.
/// </summary>
/// <param name="alert">The alert.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
public async Task<bool> DeleteAlert(Alert alert, CancellationToken cancellationToken = default(CancellationToken))
{
return await DeleteAlert(alert.ID, cancellationToken);
}
}
}
+195
View File
@@ -0,0 +1,195 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using UptimeSharp.Models;
namespace UptimeSharp
{
/// <summary>
/// UptimeClient
/// </summary>
public partial class UptimeClient
{
/// <summary>
/// Retrieves specified monitors from UptimeRobot
/// </summary>
/// <param name="monitorIDs">The monitor i ds.</param>
/// <param name="customUptimeRatio">The custom uptime ratio.</param>
/// <param name="showLog">if set to <c>true</c> [show log].</param>
/// <param name="showAlerts">if set to <c>true</c> [show alerts].</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// Monitor List
/// </returns>
public async Task<List<Models.Monitor>> 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<RetrieveResponse>("getMonitors", cancellationToken, parameters.Convert());
return response.Items;
}
/// <summary>
/// Retrieves a monitor from UptimeRobot
/// </summary>
/// <param name="monitorId">a specific monitor ID</param>
/// <param name="customUptimeRatio">The custom uptime ratio.</param>
/// <param name="showLog">if set to <c>true</c> [show log].</param>
/// <param name="showAlerts">if set to <c>true</c> [show alerts].</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// The Monitor
/// </returns>
public async Task<Models.Monitor> GetMonitor(
int monitorId,
float[] customUptimeRatio = null,
bool showLog = false,
bool showAlerts = true,
CancellationToken cancellationToken = default(CancellationToken))
{
List<Models.Monitor> monitors = await GetMonitors(new int[] { monitorId }, customUptimeRatio, showLog, showAlerts, cancellationToken);
return monitors != null && monitors.Count > 0 ? monitors[0] : null;
}
/// <summary>
/// Deletes a monitor
/// </summary>
/// <param name="monitorId">a specific monitor ID</param>
/// <returns>
/// Success state
/// </returns>
public async Task<bool> DeleteMonitor(int monitorId, CancellationToken cancellationToken = default(CancellationToken))
{
DefaultResponse response = await Request<DefaultResponse>("getMonitors", cancellationToken, new Dictionary<string, string>()
{
{ "monitorID", monitorId.ToString() }
});
return response.Status;
}
/// <summary>
/// Deletes a monitor
/// </summary>
/// <param name="monitor">The monitor.</param>
/// <returns>
/// Success state
/// </returns>
public async Task<bool> DeleteMonitor(Models.Monitor monitor, CancellationToken cancellationToken = default(CancellationToken))
{
return await DeleteMonitor(monitor.ID, cancellationToken);
}
/// <summary>
/// Creates a monitor.
/// </summary>
/// <param name="name">The name of the new monitor.</param>
/// <param name="uri">The URI or IP to watch.</param>
/// <param name="type">The type of the monitor.</param>
/// <param name="subtype">The subtype of the monitor (if port).</param>
/// <param name="port">The port (only for Subtype.Custom).</param>
/// <param name="keywordValue">The keyword value.</param>
/// <param name="keywordType">Type of the keyword.</param>
/// <param name="alerts">An ID list of existing alerts to notify.</param>
/// <param name="HTTPUsername">The HTTP username.</param>
/// <param name="HTTPPassword">The HTTP password.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// Success state
/// </returns>
public async Task<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,
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<DefaultResponse>("getMonitors", cancellationToken, parameters.Convert());
return response.Status;
}
/// <summary>
/// Edits a monitor.
/// </summary>
/// <param name="monitor">The monitor.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// Success state
/// </returns>
public async Task<bool> ModifyMonitor(Models.Monitor monitor, CancellationToken cancellationToken = default(CancellationToken))
{
List<string> 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<string, string> 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<DefaultResponse>("editMonitor", cancellationToken, paramList);
return response.Status;
}
}
}
+1 -2
View File
@@ -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.
/// </value>
[JsonIgnore]
public Uri Uri { get; set; }
public string Uri { get; set; }
/// <summary>
/// Gets or sets the port.
@@ -1,12 +1,12 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace UptimeSharp.Models
{
/// <summary>
/// All parameters which can be passed for monitor modifications
/// </summary>
internal class MonitorParameters
[DataContract]
internal class MonitorParameters : Parameters
{
/// <summary>
/// Gets or sets the name.
@@ -14,6 +14,7 @@ namespace UptimeSharp.Models
/// <value>
/// The name.
/// </value>
[DataMember(Name = "monitorFriendlyName")]
public string Name { get; set; }
/// <summary>
@@ -22,6 +23,7 @@ namespace UptimeSharp.Models
/// <value>
/// The URI.
/// </value>
[DataMember(Name = "monitorURL")]
public string Uri { get; set; }
/// <summary>
@@ -31,6 +33,7 @@ namespace UptimeSharp.Models
/// <value>
/// The port.
/// </value>
[DataMember(Name = "monitorPort")]
public int? Port { get; set; }
/// <summary>
@@ -39,6 +42,7 @@ namespace UptimeSharp.Models
/// <value>
/// The HTTP password.
/// </value>
[DataMember(Name = "monitorHTTPPassword")]
public string HTTPPassword { get; set; }
/// <summary>
@@ -47,6 +51,7 @@ namespace UptimeSharp.Models
/// <value>
/// The HTTP username.
/// </value>
[DataMember(Name = "monitorHTTPUsername")]
public string HTTPUsername { get; set; }
/// <summary>
@@ -55,6 +60,7 @@ namespace UptimeSharp.Models
/// <value>
/// The type of the keyword.
/// </value>
[DataMember(Name = "monitorKeywordType")]
public KeywordType KeywordType { get; set; }
/// <summary>
@@ -63,6 +69,7 @@ namespace UptimeSharp.Models
/// <value>
/// The keyword value.
/// </value>
[DataMember(Name = "monitorKeywordValue")]
public string KeywordValue { get; set; }
/// <summary>
@@ -71,6 +78,7 @@ namespace UptimeSharp.Models
/// <value>
/// The type.
/// </value>
[DataMember(Name = "monitorType")]
public Type Type { get; set; }
/// <summary>
@@ -79,6 +87,7 @@ namespace UptimeSharp.Models
/// <value>
/// The subtype.
/// </value>
[DataMember(Name = "monitorSubType")]
public Subtype Subtype { get; set; }
/// <summary>
@@ -87,6 +96,7 @@ namespace UptimeSharp.Models
/// <value>
/// The alert contacts.
/// </value>
[DataMember(Name = "monitorAlertContacts")]
public string[] Alerts { get; set; }
}
}
@@ -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
{
/// <summary>
/// Parameter
/// </summary>
internal class Parameters
{
/// <summary>
/// Converts an object to a list of HTTP Get parameters.
/// </summary>
/// <returns></returns>
public Dictionary<string, string> Convert()
{
// store HTTP parameters here
Dictionary<string, string> parameterDict = new Dictionary<string, string>();
// get object properties
IEnumerable<PropertyInfo> 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<object>().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;
}
}
}
@@ -1,10 +1,13 @@
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace UptimeSharp.Models
{
/// <summary>
/// All parameters which can be passed for monitor retrieval
/// </summary>
internal class RetrieveParameters
[DataContract]
internal class RetrieveParameters : Parameters
{
/// <summary>
/// List of monitor ids
@@ -12,6 +15,7 @@ namespace UptimeSharp.Models
/// <value>
/// The monitors.
/// </value>
[DataMember(Name = "monitors")]
public int[] Monitors { get; set; }
/// <summary>
@@ -20,6 +24,7 @@ namespace UptimeSharp.Models
/// <value>
/// The custom uptime ratio.
/// </value>
[DataMember(Name = "customUptimeRatio")]
public float[] CustomUptimeRatio { get; set; }
/// <summary>
@@ -28,6 +33,7 @@ namespace UptimeSharp.Models
/// <value>
/// The log bool.
/// </value>
[DataMember(Name = "logs")]
public bool? ShowLog { get; set; }
/// <summary>
@@ -36,6 +42,25 @@ namespace UptimeSharp.Models
/// <value>
/// The alert contacts bool.
/// </value>
[DataMember(Name = "showMonitorAlertContacts")]
public bool? ShowAlerts { get; set; }
/// <summary>
/// Converts an object to a list of HTTP Get parameters.
/// </summary>
/// <returns></returns>
public Dictionary<string, string> Convert()
{
Dictionary<string, string> parameters = base.Convert();
if (ShowLog.HasValue)
{
parameters.Add("alertContacts", parameters["showLog"]);
parameters.Add("showTimezone", parameters["showLog"]);
}
return parameters;
}
}
}
+1 -1
View File
@@ -13,7 +13,7 @@ namespace UptimeSharp
/// <summary>
/// UptimeClient
/// </summary>
public class UptimeClient : IUptimeClient
public partial class UptimeClient : IUptimeClient
{
/// <summary>
/// REST client used for the API communication
+3
View File
@@ -39,11 +39,14 @@
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Compile Include="Components\Alert.cs" />
<Compile Include="Components\Monitor.cs" />
<Compile Include="IUptimeClient.cs" />
<Compile Include="Models\Alert.cs" />
<Compile Include="Models\Log.cs" />
<Compile Include="Models\Monitor.cs" />
<Compile Include="Models\Parameters\MonitorParameters.cs" />
<Compile Include="Models\Parameters\Parameters.cs" />
<Compile Include="Models\Parameters\RetrieveParameters.cs" />
<Compile Include="Models\Response\AlertResponse.cs" />
<Compile Include="Models\Response\DefaultResponse.cs" />