refactor parameter creation and simplify Alerts
This commit is contained in:
@@ -14,9 +14,11 @@ namespace UptimeSharp
|
||||
/// </summary>
|
||||
/// <param name="IDs">Retrieve specified alert contacts by supplying IDs for them.</param>
|
||||
/// <returns></returns>
|
||||
public List<Alert> RetrieveAlerts(int[] alertIDs = null)
|
||||
public List<Alert> RetrieveAlerts(params int[] alertIDs)
|
||||
{
|
||||
return Get<AlertResponse>("getAlertContacts").Items;
|
||||
Parameter alerts = Parameter("alertcontacts", alertIDs.Length > 0 ? string.Join("-", alertIDs) : null);
|
||||
|
||||
return Get<AlertResponse>("getAlertContacts", alerts).Items;
|
||||
}
|
||||
|
||||
|
||||
@@ -29,13 +31,15 @@ namespace UptimeSharp
|
||||
/// <returns></returns>
|
||||
public bool AddAlert(AlertType type, string value)
|
||||
{
|
||||
List<Parameter> parameters = new List<Parameter>()
|
||||
if (type == AlertType.SMS)
|
||||
{
|
||||
Utilities.CreateParam("alertContactType", (int)type),
|
||||
Utilities.CreateParam("alertContactValue", value)
|
||||
};
|
||||
throw new APIException("AlertType.SMS is not supported by the UptimeRobot API");
|
||||
}
|
||||
|
||||
return Get<DefaultResponse>("newAlertContact", parameters).Status;
|
||||
return Get<DefaultResponse>("newAlertContact",
|
||||
Parameter("alertContactType", (int)type),
|
||||
Parameter("alertContactValue", value)
|
||||
).Status;
|
||||
}
|
||||
|
||||
|
||||
@@ -47,13 +51,7 @@ namespace UptimeSharp
|
||||
/// <returns></returns>
|
||||
public bool AddAlert(Alert alert)
|
||||
{
|
||||
List<Parameter> parameters = new List<Parameter>()
|
||||
{
|
||||
Utilities.CreateParam("alertContactType", (int)alert.Type),
|
||||
Utilities.CreateParam("alertContactValue", alert.Value)
|
||||
};
|
||||
|
||||
return Get<DefaultResponse>("newAlertContact", parameters).Status;
|
||||
return AddAlert(alert.Type, alert.Value);
|
||||
}
|
||||
|
||||
|
||||
@@ -65,12 +63,7 @@ namespace UptimeSharp
|
||||
/// <returns></returns>
|
||||
public bool DeleteAlert(int alertID)
|
||||
{
|
||||
List<Parameter> parameters = new List<Parameter>()
|
||||
{
|
||||
Utilities.CreateParam("alertContactID", alertID)
|
||||
};
|
||||
|
||||
return Get<DefaultResponse>("deleteAlertContact", parameters).Status;
|
||||
return Get<DefaultResponse>("deleteAlertContact", Parameter("alertContactID", alertID)).Status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -187,7 +187,7 @@ namespace UptimeSharp
|
||||
public bool Modify(int monitorID, MonitorParameters parameters)
|
||||
{
|
||||
List<Parameter> paramList = parameters.Convert();
|
||||
paramList.Add(Utilities.CreateParam("monitorID", monitorID));
|
||||
paramList.Add(Parameter("monitorID", monitorID));
|
||||
|
||||
return Get<DefaultResponse>("editorMonitor", paramList).Status;
|
||||
}
|
||||
@@ -227,10 +227,10 @@ namespace UptimeSharp
|
||||
// fix bad behaviour in API if no subtype is submitted
|
||||
if(parameters.Subtype == Subtype.Unknown)
|
||||
{
|
||||
paramList.Add(Utilities.CreateParam("monitorSubType", 0));
|
||||
paramList.Add(Parameter("monitorSubType", 0));
|
||||
}
|
||||
|
||||
paramList.Add(Utilities.CreateParam("monitorID", monitor.ID));
|
||||
paramList.Add(Parameter("monitorID", monitor.ID));
|
||||
|
||||
return Get<DefaultResponse>("editMonitor", paramList).Status;
|
||||
}
|
||||
|
||||
@@ -99,53 +99,40 @@ namespace UptimeSharp.Models
|
||||
{
|
||||
List<Parameter> parameters = new List<Parameter>();
|
||||
|
||||
if(!string.IsNullOrEmpty(Name))
|
||||
{
|
||||
parameters.Add(Utilities.CreateParam("monitorFriendlyName", Name));
|
||||
}
|
||||
|
||||
if(!string.IsNullOrEmpty(Uri))
|
||||
{
|
||||
parameters.Add(Utilities.CreateParam("monitorURL", Uri));
|
||||
}
|
||||
parameters.Add(UptimeClient.Parameter("monitorFriendlyName", Name));
|
||||
parameters.Add(UptimeClient.Parameter("monitorURL", Uri));
|
||||
|
||||
if(Type != null && (int)Type != 0)
|
||||
{
|
||||
parameters.Add(Utilities.CreateParam("monitorType", (int)Type));
|
||||
parameters.Add(UptimeClient.Parameter("monitorType", (int)Type));
|
||||
}
|
||||
|
||||
// special params for port listener
|
||||
if (Type == Type.Port && Subtype != Subtype.Unknown && Port.HasValue)
|
||||
{
|
||||
parameters.Add(Utilities.CreateParam("monitorSubType", (int)Subtype));
|
||||
parameters.Add(UptimeClient.Parameter("monitorSubType", (int)Subtype));
|
||||
|
||||
if (Subtype == Subtype.Custom)
|
||||
{
|
||||
parameters.Add(Utilities.CreateParam("monitorPort", Port));
|
||||
parameters.Add(UptimeClient.Parameter("monitorPort", Port));
|
||||
}
|
||||
}
|
||||
|
||||
// keyword listener
|
||||
if (Type == Type.Keyword && !string.IsNullOrEmpty(KeywordValue))
|
||||
{
|
||||
parameters.Add(Utilities.CreateParam("monitorKeywordType", (int)KeywordType));
|
||||
parameters.Add(Utilities.CreateParam("monitorKeywordValue", KeywordValue));
|
||||
parameters.Add(UptimeClient.Parameter("monitorKeywordType", (int)KeywordType));
|
||||
parameters.Add(UptimeClient.Parameter("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));
|
||||
}
|
||||
parameters.Add(UptimeClient.Parameter("monitorHTTPUsername", HTTPUsername));
|
||||
parameters.Add(UptimeClient.Parameter("monitorHTTPPassword", HTTPPassword));
|
||||
|
||||
// alert notifications
|
||||
if (Alerts != null && Alerts.Length > 0)
|
||||
{
|
||||
parameters.Add(Utilities.CreateParam("monitorAlertContacts", string.Join("-", Alerts)));
|
||||
parameters.Add(UptimeClient.Parameter("monitorAlertContacts", string.Join("-", Alerts)));
|
||||
}
|
||||
|
||||
return parameters;
|
||||
|
||||
@@ -50,24 +50,16 @@ namespace UptimeSharp.Models
|
||||
{
|
||||
List<Parameter> parameters = new List<Parameter>();
|
||||
|
||||
if (Monitors != null)
|
||||
{
|
||||
parameters.Add(Utilities.CreateParam("monitors", String.Join("-", Monitors)));
|
||||
}
|
||||
if (CustomUptimeRatio != null)
|
||||
{
|
||||
parameters.Add(Utilities.CreateParam("customUptimeRatio", String.Join("-", CustomUptimeRatio)));
|
||||
}
|
||||
parameters.Add(UptimeClient.Parameter("monitors", String.Join("-", Monitors)));
|
||||
parameters.Add(UptimeClient.Parameter("customUptimeRatio", String.Join("-", CustomUptimeRatio)));
|
||||
parameters.Add(UptimeClient.Parameter("showMonitorAlertContacts", (bool)ShowAlerts ? "1" : "0"));
|
||||
|
||||
if (ShowLog.HasValue)
|
||||
{
|
||||
string showLog = (bool)ShowLog ? "1" : "0";
|
||||
parameters.Add(Utilities.CreateParam("logs", showLog));
|
||||
parameters.Add(Utilities.CreateParam("alertContacts", showLog));
|
||||
parameters.Add(Utilities.CreateParam("showTimezone", showLog));
|
||||
}
|
||||
if (ShowAlerts.HasValue)
|
||||
{
|
||||
parameters.Add(Utilities.CreateParam("showMonitorAlertContacts", (bool)ShowAlerts ? "1" : "0"));
|
||||
parameters.Add(UptimeClient.Parameter("logs", showLog));
|
||||
parameters.Add(UptimeClient.Parameter("alertContacts", showLog));
|
||||
parameters.Add(UptimeClient.Parameter("showTimezone", showLog));
|
||||
}
|
||||
|
||||
return parameters;
|
||||
|
||||
@@ -7,8 +7,5 @@ namespace UptimeSharp.Models
|
||||
/// Default Response
|
||||
/// </summary>
|
||||
[DataContract]
|
||||
internal class DefaultResponse : ResponseBase
|
||||
{
|
||||
|
||||
}
|
||||
internal class DefaultResponse : ResponseBase {}
|
||||
}
|
||||
|
||||
+26
-27
@@ -117,7 +117,12 @@ namespace UptimeSharp
|
||||
if (parameters != null)
|
||||
{
|
||||
parameters.ForEach(
|
||||
param => request.AddParameter(param)
|
||||
param => {
|
||||
if(param.Value != null)
|
||||
{
|
||||
request.AddParameter(param);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -126,37 +131,31 @@ namespace UptimeSharp
|
||||
}
|
||||
|
||||
|
||||
protected string Get(string resource, List<Parameter> parameters = null)
|
||||
/// <summary>
|
||||
/// Fetches a typed resource
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="resource">Requested resource</param>
|
||||
/// <param name="parameter">The parameters.</param>
|
||||
/// <returns></returns>
|
||||
protected T Get<T>(string resource, params Parameter[] parameters) where T : class, new()
|
||||
{
|
||||
var request = new RestRequest(resource, Method.GET);
|
||||
List<Parameter> parameterList = new List<Parameter>();
|
||||
parameterList.AddRange(parameters);
|
||||
|
||||
// enumeration for params
|
||||
if (parameters != null)
|
||||
{
|
||||
parameters.ForEach(
|
||||
param => request.AddParameter(param)
|
||||
);
|
||||
}
|
||||
|
||||
// do the request
|
||||
return Request(request);
|
||||
return Get<T>(resource, parameterList);
|
||||
}
|
||||
|
||||
|
||||
///// <summary>
|
||||
///// Puts an action
|
||||
///// </summary>
|
||||
///// <param name="actionParameter">The action parameter.</param>
|
||||
///// <returns></returns>
|
||||
//protected bool PutSendAction(ActionParameter actionParameter)
|
||||
//{
|
||||
// ModifyParameters parameters = new ModifyParameters()
|
||||
// {
|
||||
// Actions = new List<ActionParameter>() { actionParameter }
|
||||
// };
|
||||
|
||||
// return Get<Modify>("send", parameters.Convert(), true).Status;
|
||||
//}
|
||||
public static Parameter Parameter(string name, object value)
|
||||
{
|
||||
return new Parameter()
|
||||
{
|
||||
Name = name,
|
||||
Value = value,
|
||||
Type = ParameterType.GetOrPost
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
///// <summary>
|
||||
|
||||
@@ -66,7 +66,6 @@
|
||||
<Compile Include="Models\Response\RetrieveResponse.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="UptimeClient.cs" />
|
||||
<Compile Include="Utilities.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
|
||||
@@ -1,90 +0,0 @@
|
||||
using RestSharp;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UptimeSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// General utilities
|
||||
/// </summary>
|
||||
internal class Utilities
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// converts a bool to a string for GET param
|
||||
/// </summary>
|
||||
/// <param name="value">The value.</param>
|
||||
/// <returns>A string representation of the boolean</returns>
|
||||
public static string Bool(bool? value)
|
||||
{
|
||||
return value != null ? ((bool)value ? "1" : "0") : null;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// converts DateTime to an UNIX timestamp
|
||||
/// </summary>
|
||||
/// <param name="dateTime">The date.</param>
|
||||
/// <returns>UNIX timestamp</returns>
|
||||
public static int? GetUnixTimestamp(DateTime? dateTime)
|
||||
{
|
||||
if (dateTime == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return (int)((DateTime)dateTime - new DateTime(1970, 1, 1)).TotalSeconds;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Creates a Parameter object.
|
||||
/// </summary>
|
||||
/// <param name="name">The name.</param>
|
||||
/// <param name="value">The value.</param>
|
||||
/// <param name="type">The type.</param>
|
||||
/// <returns></returns>
|
||||
public static Parameter CreateParam(string name, object value, ParameterType type = ParameterType.GetOrPost)
|
||||
{
|
||||
return new Parameter() { Name = name, Value = value, Type = type };
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Creates a Parameter object within a list.
|
||||
/// </summary>
|
||||
/// <param name="name">The name.</param>
|
||||
/// <param name="value">The value.</param>
|
||||
/// <param name="type">The type.</param>
|
||||
/// <returns></returns>
|
||||
public static List<Parameter> CreateParamInList(string name, object value, ParameterType type = ParameterType.GetOrPost)
|
||||
{
|
||||
return new List<Parameter>() { CreateParam(name, value, type) };
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Convert a dictionary to a list
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="items">The items.</param>
|
||||
/// <returns></returns>
|
||||
public static List<T> DictionaryToList<T>(Dictionary<string, T> items) where T : new()
|
||||
{
|
||||
if (items == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var itemEnumerator = items.GetEnumerator();
|
||||
List<T> list = new List<T>();
|
||||
|
||||
while (itemEnumerator.MoveNext())
|
||||
{
|
||||
list.Add(itemEnumerator.Current.Value);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user