diff --git a/UptimeSharp.Tests/MonitorsTest.cs b/UptimeSharp.Tests/MonitorsTest.cs index 9ece31e..e1d98ba 100644 --- a/UptimeSharp.Tests/MonitorsTest.cs +++ b/UptimeSharp.Tests/MonitorsTest.cs @@ -11,19 +11,15 @@ namespace UptimeSharp.Tests public MonitorsTest() : base() { } - public async Task Dispose() - { - List monitors = await client.GetMonitors(); - monitors.ForEach(item => monitorsToDelete.Add(item.ID)); - } + public async Task Dispose() { } [Fact] - public async Task AddHTTPMonitor() + public async void AddHTTPMonitor() { Assert.True(await client.AddMonitor( name: "test_1", - uri: "http://test1.com" + target: "http://test1.com" )); } @@ -33,7 +29,7 @@ namespace UptimeSharp.Tests { Assert.True(await client.AddMonitor( name: "test_2", - uri: "http://test2.com", + target: "http://test2.com", type: Models.Type.Keyword, keywordType: KeywordType.Exists, keywordValue: "test" @@ -46,7 +42,7 @@ namespace UptimeSharp.Tests { Assert.True(await client.AddMonitor( name: "test_3", - uri: "http://test3.com", + target: "http://test3.com", type: Models.Type.Ping )); } @@ -57,7 +53,7 @@ namespace UptimeSharp.Tests { Assert.True(await client.AddMonitor( name: "test_4", - uri: "127.0.0.1", + target: "127.0.0.1", type: Models.Type.Port, subtype: Subtype.Custom, port: 50004 @@ -70,7 +66,7 @@ namespace UptimeSharp.Tests { //Assert.True(await client.AddMonitor( // name: "test_5", - // uri: "255.0.0.1", + // target: "255.0.0.1", // type: Models.Type.Port, // subtype: Subtype.HTTP //)); @@ -91,7 +87,7 @@ namespace UptimeSharp.Tests { Assert.True(await client.AddMonitor( name: "test_6", - uri: "http://test6.com" + target: "http://test6.com" )); List items = await client.GetMonitors(); diff --git a/UptimeSharp.Tests/TestsBase.cs b/UptimeSharp.Tests/TestsBase.cs index ba5c02c..0bb563c 100644 --- a/UptimeSharp.Tests/TestsBase.cs +++ b/UptimeSharp.Tests/TestsBase.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; +using UptimeSharp.Models; using Xunit; namespace UptimeSharp.Tests @@ -25,13 +26,14 @@ namespace UptimeSharp.Tests // teardown - public void Dispose() + public async void Dispose() { + List monitors = await client.GetMonitors(); alertsToDelete.ForEach(async id => { await client.DeleteAlert(id.ToString()); }); - monitorsToDelete.ForEach(async id => + monitors.ForEach(async id => { await client.DeleteMonitor(id); }); @@ -42,7 +44,7 @@ namespace UptimeSharp.Tests public static async Task ThrowsAsync(Func func) { var expected = typeof(TException); - Type actual = null; + System.Type actual = null; try { await func(); diff --git a/UptimeSharp/Components/Monitor.cs b/UptimeSharp/Components/Monitor.cs index b577b3a..5c15300 100644 --- a/UptimeSharp/Components/Monitor.cs +++ b/UptimeSharp/Components/Monitor.cs @@ -107,7 +107,7 @@ namespace UptimeSharp /// Creates a monitor. /// /// The name of the new monitor. - /// The URI or IP to watch. + /// The URI or IP to watch. /// The type of the monitor. /// The subtype of the monitor (if port). /// The port (only for Subtype.Custom). @@ -123,7 +123,7 @@ namespace UptimeSharp /// public async Task AddMonitor( string name, - string uri, + string target, Type type = Type.HTTP, Subtype subtype = Subtype.Unknown, int? port = null, string keywordValue = null, @@ -137,7 +137,7 @@ namespace UptimeSharp MonitorParameters parameters = new MonitorParameters() { Name = name, - Uri = uri, + Target = target, Type = type, Subtype = subtype, Port = port, @@ -174,7 +174,7 @@ namespace UptimeSharp MonitorParameters parameters = new MonitorParameters() { Name = monitor.Name, - Uri = monitor.Target != null ? monitor.Target : null, + Target = monitor.Target != null ? monitor.Target : null, Port = monitor.Port, HTTPPassword = monitor.HTTPPassword, HTTPUsername = monitor.HTTPUsername, diff --git a/UptimeSharp/Models/Parameters/MonitorParameters.cs b/UptimeSharp/Models/Parameters/MonitorParameters.cs index 0d760e8..28bbbee 100644 --- a/UptimeSharp/Models/Parameters/MonitorParameters.cs +++ b/UptimeSharp/Models/Parameters/MonitorParameters.cs @@ -1,4 +1,6 @@ -using System.Runtime.Serialization; +using System; +using System.Collections.Generic; +using System.Runtime.Serialization; namespace UptimeSharp.Models { @@ -24,7 +26,7 @@ namespace UptimeSharp.Models /// The URI. /// [DataMember(Name = "monitorURL")] - public string Uri { get; set; } + public string Target { get; set; } /// /// Gets or sets the port. @@ -98,5 +100,55 @@ namespace UptimeSharp.Models /// [DataMember(Name = "monitorAlertContacts")] public string[] Alerts { get; set; } + + /// + /// Converts an object to a list of HTTP Get parameters. + /// + /// + public Dictionary Convert() + { + Dictionary parameters = new Dictionary(); + + parameters.Add("monitorFriendlyName", Name); + parameters.Add("monitorURL", Target); + + if ((int)Type != 0) + { + parameters.Add("monitorType", Type.ToString()); + } + + // special params for port listener + if (Type == Type.Port && Subtype != Subtype.Unknown) + { + parameters.Add("monitorSubType", Subtype.ToString()); + + if (Subtype == Subtype.Custom && Port.HasValue) + { + parameters.Add("monitorPort", Port.ToString()); + } + } + + // keyword listener + if (Type == Type.Keyword && !String.IsNullOrEmpty(KeywordValue)) + { + parameters.Add("monitorKeywordType", KeywordType.ToString()); + parameters.Add("monitorKeywordValue", KeywordValue); + } + + // HTTP basic auth credentials + if (!String.IsNullOrEmpty(HTTPUsername)) + { + parameters.Add("monitorHTTPUsername", HTTPUsername); + parameters.Add("monitorHTTPPassword", HTTPPassword); + } + + // alert notifications + if (Alerts != null && Alerts.Length > 0) + { + parameters.Add("monitorAlertContacts", String.Join("-", Alerts)); + } + + return parameters; + } } } diff --git a/UptimeSharp/UptimeClient.cs b/UptimeSharp/UptimeClient.cs index c95fc7b..c558ff8 100644 --- a/UptimeSharp/UptimeClient.cs +++ b/UptimeSharp/UptimeClient.cs @@ -83,7 +83,7 @@ namespace UptimeSharp _restClient.Timeout = TimeSpan.FromSeconds(timeout.Value); } - // set base uri + // set base target _restClient.BaseAddress = baseUri; // defines the response format