fix IEnumerable issue in parameter converter

This commit is contained in:
2014-02-02 12:37:37 +01:00
parent 1a66f6f760
commit 056d43f4d6
11 changed files with 83 additions and 45 deletions
+8 -8
View File
@@ -14,7 +14,7 @@ namespace UptimeSharp.Tests
[Fact]
public async void AddHTTPMonitor()
{
Assert.True(await client.AddMonitor(
Assert.NotNull(await client.AddMonitor(
name: "test_1",
target: "http://test1.com"
));
@@ -24,7 +24,7 @@ namespace UptimeSharp.Tests
[Fact]
public async Task AddKeywordMonitor()
{
Assert.True(await client.AddMonitor(
Assert.NotNull(await client.AddMonitor(
name: "test_2",
target: "http://test2.com",
type: Models.Type.Keyword,
@@ -37,7 +37,7 @@ namespace UptimeSharp.Tests
[Fact]
public async Task AddPingMonitor()
{
Assert.True(await client.AddMonitor(
Assert.NotNull(await client.AddMonitor(
name: "test_3",
target: "http://test3.com",
type: Models.Type.Ping
@@ -48,7 +48,7 @@ namespace UptimeSharp.Tests
[Fact]
public async Task AddPortMonitor()
{
Assert.True(await client.AddMonitor(
Assert.NotNull(await client.AddMonitor(
name: "test_4",
target: "127.0.0.1",
type: Models.Type.Port,
@@ -82,10 +82,10 @@ namespace UptimeSharp.Tests
[Fact]
public async Task ModifyAMonitor()
{
Assert.True(await client.AddMonitor(
name: "test_6",
target: "http://test6.com"
));
//Assert.NotNull(await client.AddMonitor(
// name: "test_6",
// target: "http://test6.com"
//));
List<Monitor> items = await client.GetMonitors();
Monitor monitor = items.SingleOrDefault(item => item.Name == "test_6");
+1 -17
View File
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UptimeSharp.Models;
using Xunit;
namespace UptimeSharp.Tests
@@ -10,9 +8,6 @@ namespace UptimeSharp.Tests
{
protected UptimeClient client;
protected List<int> alertsToDelete = new List<int>();
protected List<int> monitorsToDelete = new List<int>();
// this API key is associated with the test account uptimesharp@outlook.com
// please don't abuse it and create your own if you want to test the project!
protected string APIKey = "u97240-a24c634b3b84f1af602628e8";
@@ -26,18 +21,7 @@ namespace UptimeSharp.Tests
// teardown
public async void Dispose()
{
List<Monitor> monitors = await client.GetMonitors();
alertsToDelete.ForEach(async id =>
{
await client.DeleteAlert(id.ToString());
});
monitors.ForEach(async id =>
{
await client.DeleteMonitor(id);
});
}
public void Dispose() { }
// async throws
+24 -9
View File
@@ -15,7 +15,7 @@ namespace UptimeSharp
/// <summary>
/// Retrieves specified monitors from UptimeRobot
/// </summary>
/// <param name="monitorIDs">The monitor i ds.</param>
/// <param name="monitorIDs">The monitor IDs.</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>
@@ -25,7 +25,7 @@ namespace UptimeSharp
/// </returns>
/// <exception cref="UptimeSharpException"></exception>
public async Task<List<Models.Monitor>> GetMonitors(
int[] monitorIDs = null,
string[] monitorIDs = null,
float[] customUptimeRatio = null,
bool showLog = false,
bool showAlerts = true,
@@ -58,13 +58,14 @@ namespace UptimeSharp
/// </returns>
/// <exception cref="UptimeSharpException"></exception>
public async Task<Models.Monitor> GetMonitor(
int monitorId,
string 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);
List<Models.Monitor> monitors = await GetMonitors(new string[] { monitorId }, customUptimeRatio, showLog, showAlerts, cancellationToken);
return monitors != null && monitors.Count > 0 ? monitors[0] : null;
}
@@ -78,11 +79,11 @@ namespace UptimeSharp
/// Success state
/// </returns>
/// <exception cref="UptimeSharpException"></exception>
public async Task<bool> DeleteMonitor(int monitorId, CancellationToken cancellationToken = default(CancellationToken))
public async Task<bool> DeleteMonitor(string monitorId, CancellationToken cancellationToken = default(CancellationToken))
{
return (await Request<DefaultResponse>("getMonitors", cancellationToken, new Dictionary<string, string>()
{
{ "monitorID", monitorId.ToString() }
{ "monitorID", monitorId }
})).Success;
}
@@ -116,10 +117,10 @@ namespace UptimeSharp
/// <param name="HTTPPassword">The HTTP password.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// Success state
/// New Monitor (without details)
/// </returns>
/// <exception cref="UptimeSharpException"></exception>
public async Task<bool> AddMonitor(
public async Task<Models.Monitor> AddMonitor(
string name,
string target,
Type type = Type.HTTP,
@@ -146,7 +147,21 @@ namespace UptimeSharp
HTTPUsername = HTTPUsername
};
return (await Request<DefaultResponse>("newMonitor", cancellationToken, parameters.Convert())).Success;
Models.Monitor monitor = (await Request<AddMonitorResponse>("newMonitor", cancellationToken, parameters.Convert())).Monitor;
if (monitor != null)
{
monitor.Name = name;
monitor.Type = type;
monitor.Subtype = subtype;
monitor.Port = port;
monitor.KeywordType = keywordType;
monitor.KeywordValue = keywordValue;
monitor.HTTPPassword = HTTPPassword;
monitor.HTTPUsername = HTTPUsername;
}
return monitor;
}
+1 -1
View File
@@ -20,7 +20,7 @@ namespace UptimeSharp.Models
/// The ID.
/// </value>
[JsonProperty("id")]
public int ID { get; set; }
public string ID { get; set; }
/// <summary>
/// Gets or sets the name.
+5 -4
View File
@@ -39,12 +39,13 @@ namespace UptimeSharp.Models
continue;
}
// convert array to comma-seperated list
if (value is Array && (value as Array).Length > 0)
// empty array
if (value is Array && (value as Array).Length == 0)
{
value = String.Join("-", value);
continue;
}
else if (value is IEnumerable && value.GetType().GetElementType() == typeof(string))
// convert array to comma-seperated list
else if (value is IEnumerable)
{
value = String.Join("-", ((IEnumerable)value).Cast<object>().Select(x => x.ToString()).ToArray());
}
@@ -16,7 +16,7 @@ namespace UptimeSharp.Models
/// The monitors.
/// </value>
[DataMember(Name = "monitors")]
public int[] Monitors { get; set; }
public string[] Monitors { get; set; }
/// <summary>
/// Defines the number of days to calculate the uptime ratio
@@ -3,17 +3,16 @@
namespace UptimeSharp.Models
{
/// <summary>
/// Alert Response
/// Add Alert Response
/// </summary>
[JsonObject]
internal class AddAlertResponse : Response
{
/// <summary>
/// Gets or sets the item dictionary.
/// The list is 2 layers deep, so this one is necessary :-/
/// Gets or sets the alert.
/// </summary>
/// <value>
/// The item dictionary.
/// The alert.
/// </value>
[JsonProperty("alertcontact")]
public Alert Alert { get; set; }
@@ -0,0 +1,20 @@
using Newtonsoft.Json;
namespace UptimeSharp.Models
{
/// <summary>
/// Add Monitor Response
/// </summary>
[JsonObject]
internal class AddMonitorResponse : Response
{
/// <summary>
/// Gets or sets the monitor.
/// </summary>
/// <value>
/// The monitor.
/// </value>
[JsonProperty("monitor")]
public Monitor Monitor { get; set; }
}
}
+2 -1
View File
@@ -168,7 +168,8 @@ namespace UptimeSharp
{
new BoolConverter(),
new UnixDateTimeConverter(),
new UriConverter()
new UriConverter(),
new EnumConverter()
}
}
);
+1
View File
@@ -49,6 +49,7 @@
<Compile Include="Models\Parameters\Parameters.cs" />
<Compile Include="Models\Parameters\RetrieveParameters.cs" />
<Compile Include="Models\Response\AddAlertResonse.cs" />
<Compile Include="Models\Response\AddMonitorResponse.cs" />
<Compile Include="Models\Response\AlertResponse.cs" />
<Compile Include="Models\Response\DefaultResponse.cs" />
<Compile Include="Models\Response\IResponse.cs" />
+17
View File
@@ -77,4 +77,21 @@ namespace UptimeSharp
return objectType.Equals(typeof(Uri));
}
}
public class EnumConverter : StringEnumConverter
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
string value = reader.Value.ToString();
if (String.IsNullOrEmpty(value))
{
return Enum.ToObject(objectType, 0);
}
return base.ReadJson(reader, objectType, existingValue, serializer);
}
}
}