4 Commits

Author SHA1 Message Date
swcs 295867b84b add tests for monitors; fix MonitorParameters bug to not include subtype on port listening 2013-08-26 11:45:57 +02:00
swcs e4f6fe2928 swap NUnit with xUnit.NET! 2013-08-26 01:07:19 +02:00
swcs 6f29cadd26 doc fixes 2013-08-25 22:33:06 +02:00
swcs 4ce3e726c5 documentation 2013-08-25 22:27:20 +02:00
9 changed files with 380 additions and 81 deletions
+224 -3
View File
@@ -1,14 +1,235 @@
# UptimeSharp
> Under development - don't use yet!
**UptimeSharp** is a C#.NET class library that integrates the [UptimeRobot API](http://http://www.uptimerobot.com/api.asp).
**UptimeSharp** is a C#.NET class library, that integrates the [UptimeRobot API](http://http://www.uptimerobot.com/api.asp).
The wrapper consists of 2 parts:
- Get and modify monitors
- Get and modify alert contacts
[uptimesharp.frontendplay.com](http://uptimesharp.frontendplay.com/) (coming soon)
## Install using NuGet
```
Install-Package UptimeSharp
```
[nuget.org/packages/UptimeSharp](https://www.nuget.org/packages/UptimeSharp/) (coming soon)
## Usage Example
Get your [API Key UptimeRobot](http://uptimerobot.com/mySettings.asp) (left section under "API Information")
Include the UptimeSharp namespace and it's associated models:
```csharp
using UptimeSharp;
using UptimeSharp.Models;
```
Initialize UptimeClient with:
```csharp
UptimeClient _client = new UptimeClient("[YOUR_API_KEY]");
```
Do a simple request - e.g. get all your monitors:
```csharp
_client.GetMonitors().ForEach(
item => Console.WriteLine(item.Name + " | " + item.Type)
);
```
Which will output:
frontendplay | HTTP
google | Keyword
localhost | Ping
...
## Constructor
```csharp
UptimeClient(string apiKey)
```
Get your [API Key UptimeRobot](http://uptimerobot.com/mySettings.asp) (left section under "API Information")
## Retrieve
Get list of all monitors:
```csharp
List<Monitor> items = _client.GetMonitors();
```
Get monitors by ID - or a single monitor:
```csharp
List<Monitor> items = _client.GetMonitors(new int[]{ 12891, 98711 });
// or
Monitor item = _client.GetMonitor(12891);
```
Provide additional params for more data:
```csharp
List<Monitor> items = _client.GetMonitors(
monitorIDs: new int[]{ 12891, 98711 },
customUptimeRatio: new float[] { 7, 30, 45 },
showLog: true,
showAlerts: true
);
```
`monitorIDs`: You can remove this parameter if you want to retrieve all monitors _(default: null)_
<br>
`customUptimeRatio`: the number of days to calculate the uptime ratio(s) for _(default: null)_
<br>
`showLog`: include log, if true _(default: false)_
<br>
`showAlerts`: include alerts, if true _(default: true)_
<br>
## Add
Adds/creates a new monitor.
```csharp
bool AddMonitor(
string name,
string uri,
Type type = Type.HTTP,
Subtype subtype = Subtype.Unknown,
int? port = null,
string keywordValue = null,
KeywordType keywordType = KeywordType.Unknown,
int[] alerts = null,
string HTTPUsername = null,
string HTTPPassword = null
)
```
Example - Watch an SMTP Server:
```csharp
bool isSuccess = _client.AddMonitor(
name: "cee",
uri: "127.0.0.1",
type: Type.Port,
subtype: Subtype.SMTP
);
```
`name`: A friendly name for the new monitor
<br>
`uri`: The URI or IP to watch
<br>
`type`: The type of the monitor (see [# Monitor Types](#monitor-types))
<br>
`subtype`: The subtype of the monitor (only for Type.Port) (see [# Monitor Types](#monitor-types))
<br>
`port`: The port (only for Subtype.Custom)
<br>
`keywordValue`: The keyword value (for Type.Keyword)
<br>
`keywordType`: Type of the keyword (for Type.Keyword)
<br>
`alerts`: An ID list of existing alerts to notify
<br>
`HTTPUsername`: The HTTP username
<br>
`HTTPPassword`: The HTTP password
As you can see, a lot of these parameters are only available if you've specified the correct `Type`.
<br>
If you've selected `Type.Port` for example, UptimeSharp will ignore the `keywordValue` and `keywordType` parameters, even if you submitted valid ones.
## Delete
Delete a monitor by ID:
```csharp
bool isSuccess = _client.DeleteMonitor(12891);
```
Delete a monitor by a Monitor instance:
```csharp
// Monitor myMonitor = ...
bool isSuccess = _client.DeleteMonitor(myMonitor);
```
## Modify
In order to modify an existing monitor, just alter the properties of the Monitor instance and call the `ModifyMonitor` method:
```csharp
// Monitor myMonitor = ...
myMonitor.Name = "my new name :-)";
bool isSuccess = _client.ModifyMonitor(myMonitor);
```
**Important:** It is not possible to alter the `Type` of a monitor after its creation! In case you want to do this, you have to delete the monitor and create a new one with the changed type.
### Modify Alerts
Retrieve all alerts:
```csharp
List<Alert> items = _client.GetAlerts();
```
Retrieve alerts by IDs:
```csharp
List<Alert> items = _client.GetAlerts(new int[]{ 12897, 98711 });
```
Retrieve a specific alert:
```csharp
Alert item = _client.GetAlert(12897);
```
Adds an alert _(Due to UptimeRobot API limitations SMS and Twitter alert contact types are not supported yet)_:
```csharp
bool isSuccess = _client.AddAlert(AlertType.Email, "uptimesharp@outlook.com");
```
Adds an alert from instance:
```csharp
// Alert myAlert = ...
bool isSuccess = _client.AddAlert(myAlert);
```
Deletes an alert:
```csharp
bool isSuccess = _client.DeleteAlert(12897);
```
Deletes an alert from instance:
```csharp
// Alert myAlert = ...
bool isSuccess = _client.DeleteAlert(myAlert);
```
---
## Release History
- 2013-08-13 v0.1.0 initial
- 2013-08-25 v0.1.0 Monitor and Alert APIs
## Dependencies
+27 -29
View File
@@ -1,11 +1,11 @@
using NUnit.Framework;
using Xunit;
using System.Collections.Generic;
using UptimeSharp.Models;
using System;
namespace UptimeSharp.Tests
{
[TestFixture]
public class AlertsTest
public class AlertsTest : IDisposable
{
UptimeClient client;
@@ -14,76 +14,74 @@ namespace UptimeSharp.Tests
string APIKey = "u97240-a24c634b3b84f1af602628e8";
[SetUp]
public void Setup()
// setup
public AlertsTest()
{
client = new UptimeClient(APIKey);
}
[TearDown]
public void Teardown()
// teardown
public void Dispose()
{
List<Alert> alerts = client.GetAlerts();
alerts.ForEach(alert => client.DeleteAlert(alert));
}
[Test]
[ExpectedException(typeof(APIException))]
[Fact]
public void AddInvalidAlertWithTypeSms()
{
client.AddAlert(Models.AlertType.SMS, "+436601289172");
Assert.Throws<APIException>(() =>
{
client.AddAlert(Models.AlertType.SMS, "+436601289172");
});
}
[Test]
[ExpectedException(typeof(APIException))]
[Fact]
public void AddInvalidAlertWithTypeTwitter()
{
client.AddAlert(Models.AlertType.Twitter, "artistandsocial");
Assert.Throws<APIException>(() =>
{
client.AddAlert(Models.AlertType.Twitter, "artistandsocial");
});
}
[Test]
[Fact]
public void AddAndRemoveAlerts()
{
string email = "example@ceecore.com";
Assert.IsTrue(client.AddAlert(AlertType.Email, email), "Response should be true for adding a new alert");
Assert.True(client.AddAlert(AlertType.Email, email));
Alert origin = GetOriginAlert(email);
Assert.AreEqual(email, origin.Value, "Retrieved alert should have the value (example@ceecore.com) which was submitted");
Assert.AreEqual(AlertType.Email, origin.Type, "Retrieved alert should have the type (email) which was submitted");
Assert.Equal(email, origin.Value);
Assert.Equal(AlertType.Email, origin.Type);
client.DeleteAlert(origin);
origin = GetOriginAlert(email);
Assert.IsNull(origin, "Alert should have been deleted");
Assert.Null(origin);
}
[Test]
[Fact]
public void AddAndRetrieveSpecificAlerts()
{
Assert.IsTrue(
client.AddAlert(AlertType.Email, "example1@ceecore.com"),
"Response should be true for adding a new alert (email 1)"
);
Assert.IsTrue(
client.AddAlert(AlertType.Boxcar, "example@ceecore.com"),
"Response should be true for adding a new alert (bocar 4)"
);
Assert.True(client.AddAlert(AlertType.Email, "example1@ceecore.com"));
Assert.True(client.AddAlert(AlertType.Boxcar, "example@ceecore.com"));
List<Alert> alerts = client.GetAlerts();
Assert.GreaterOrEqual(alerts.ToArray().Length, 2, "Alerts length should be at least 2", alerts);
Assert.InRange(alerts.ToArray().Length, 2, 100);
List<Alert> specificAlerts = client.GetAlerts(new int[] { alerts[0].ID, alerts[1].ID });
Assert.AreEqual(2, specificAlerts.ToArray().Length, "Specific alerts length should be 2", specificAlerts);
Assert.Equal(2, specificAlerts.ToArray().Length);
}
+6 -7
View File
@@ -1,10 +1,9 @@
using NUnit.Framework;
using Xunit;
using System.Collections.Generic;
using UptimeSharp.Models;
namespace UptimeSharp.Tests
{
[TestFixture]
public class ClientTest
{
UptimeClient client;
@@ -14,19 +13,19 @@ namespace UptimeSharp.Tests
string APIKey = "u97240-a24c634b3b84f1af602628e8";
[SetUp]
public void Setup()
// setup
public ClientTest()
{
client = new UptimeClient(APIKey);
}
[Test]
[Fact]
public void Initialize()
{
Assert.IsNull(client.LastRequestData, "LastRequestData should be null on init");
Assert.Null(client.LastRequestData);
Assert.AreEqual(APIKey, client.ApiKey, "API Key should be correctly assigned");
Assert.Equal(APIKey, client.ApiKey);
}
}
}
+110 -29
View File
@@ -1,11 +1,11 @@
using NUnit.Framework;
using Xunit;
using System;
using System.Collections.Generic;
using UptimeSharp.Models;
namespace UptimeSharp.Tests
{
[TestFixture]
public class MonitorsTest
public class MonitorsTest : IDisposable
{
UptimeClient client;
@@ -14,44 +14,125 @@ namespace UptimeSharp.Tests
string APIKey = "u97240-a24c634b3b84f1af602628e8";
[SetUp]
public void Setup()
// setup
public MonitorsTest()
{
//client = new UptimeClient(APIKey);
client = new UptimeClient(APIKey);
}
[TearDown]
public void Teardown()
// teardown
public void Dispose()
{
//List<Alert> alerts = client.RetrieveAlerts();
//alerts.ForEach(alert => client.DeleteAlert(alert));
List<Monitor> monitors = client.GetMonitors();
monitors.ForEach(monitor => client.DeleteMonitor(monitor));
}
//bool result = client.Delete(775853599);
[Fact]
public void AddHTTPMonitor()
{
Assert.True(client.AddMonitor(
name: "test_1",
uri: "http://test1.com"
));
}
//bool result = client.Create("apitest", "http://pocketsharp.com", Models.Type.HTTP);
//client.Add("apiKeywordTest", "http://frontendplay.com", "frontendplay", KeywordType.NotExists);
//client.DeleteAlert(2014599);
//bool result = client.AddAlert(AlertType.Email, "klika@live.at");
[Fact]
public void AddKeywordMonitor()
{
Assert.True(client.AddMonitor(
name: "test_2",
uri: "http://test2.com",
type: Models.Type.Keyword,
keywordType: KeywordType.Exists,
keywordValue: "test"
));
}
//bool x = client.Add(
// name: "testx",
// uri: "http://google.at",
// type: Models.Type.Keyword,
// keywordType: KeywordType.Exists,
// keywordValue: "goog"
//);
//List<Monitor> monitors = client.Retrieve(showLog: true);
//List<Alert> alerts = client.RetrieveAlerts();
//System.Console.WriteLine(monitor.ID + " " + monitor.Name);
//alerts.ForEach(item => System.Console.WriteLine(item.ID + " " + item.Type.ToString() + " " + item.Value));
//monitor.Name = "HALLOOO";
//bool result = client.Modify(monitor);
[Fact]
public void AddPingMonitor()
{
Assert.True(client.AddMonitor(
name: "test_3",
uri: "http://test3.com",
type: Models.Type.Ping
));
}
//monitors.ForEach(item => System.Console.WriteLine(item.ID + " " + item.Name + " " + item.Status));
[Fact]
public void AddPortMonitor()
{
Assert.True(client.AddMonitor(
name: "test_4",
uri: "127.0.0.1",
type: Models.Type.Port,
subtype: Subtype.Custom,
port: 50004
));
}
[Fact]
public void GetMonitors()
{
Assert.True(client.AddMonitor(
name: "test_5",
uri: "255.0.0.1",
type: Models.Type.Port,
subtype: Subtype.HTTP
));
List<Monitor> items = client.GetMonitors();
Monitor monitor = null;
items.ForEach(item =>
{
if(item.Name == "test_5")
{
monitor = item;
}
});
Assert.True(
monitor != null
&& monitor.UriString == "255.0.0.1"
&& monitor.Type == Models.Type.Port
&& monitor.Subtype == Subtype.HTTP);
}
[Fact]
public void ModifyAMonitor()
{
Assert.True(client.AddMonitor(
name: "test_6",
uri: "http://test6.com"
));
List<Monitor> items = client.GetMonitors();
Monitor monitor = null;
items.ForEach(item =>
{
if (item.Name == "test_6")
{
monitor = item;
}
});
Assert.NotNull(monitor);
monitor.Name = "updated_test_6";
Assert.True(client.ModifyMonitor(monitor));
monitor = client.GetMonitor(monitor.ID);
Assert.Equal(monitor.Name, "updated_test_6");
}
}
}
+3 -3
View File
@@ -30,9 +30,6 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework">
<HintPath>..\packages\NUnit.2.6.2\lib\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="RestSharp, Version=104.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\RestSharp.104.1\lib\net4\RestSharp.dll</HintPath>
@@ -48,6 +45,9 @@
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="xunit">
<HintPath>..\packages\xunit.1.9.2\lib\net20\xunit.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="AlertsTest.cs" />
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="2.6.2" targetFramework="net40" />
<package id="RestSharp" version="104.1" targetFramework="net40" />
<package id="ServiceStack.Text" version="3.9.58" targetFramework="net40" />
<package id="xunit" version="1.9.2" targetFramework="net40" />
</packages>
+6 -6
View File
@@ -20,11 +20,11 @@ namespace UptimeSharp
/// <returns>
/// Monitor List
/// </returns>
public List<Monitor> GetMonitors(int[] monitors = null, float[] customUptimeRatio = null, bool showLog = false, bool showAlerts = true)
public List<Monitor> GetMonitors(int[] monitorIDs = null, float[] customUptimeRatio = null, bool showLog = false, bool showAlerts = true)
{
RetrieveParameters parameters = new RetrieveParameters()
{
Monitors = monitors,
Monitors = monitorIDs,
CustomUptimeRatio = customUptimeRatio,
ShowAlerts = showAlerts,
ShowLog = showLog
@@ -84,19 +84,19 @@ namespace UptimeSharp
/// <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 port.</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">A ID list of existing alerts to notify.</param>
/// <param name="HTTPPassword">The HTTP password.</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>
/// <returns>
/// Success state
/// </returns>
public bool AddMonitor(string name, string uri, Type type = Type.HTTP, Subtype subtype = Subtype.Unknown,
int? port = null, string keywordValue = null, KeywordType keywordType = KeywordType.Unknown,
int[] alerts = null, string HTTPPassword = null, string HTTPUsername = null)
int[] alerts = null, string HTTPUsername = null, string HTTPPassword = null)
{
MonitorParameters parameters = new MonitorParameters()
@@ -108,11 +108,11 @@ namespace UptimeSharp.Models
}
// special params for port listener
if (Type == Type.Port && Subtype != Subtype.Unknown && Port.HasValue)
if (Type == Type.Port && Subtype != Subtype.Unknown)
{
parameters.Add(UptimeClient.Parameter("monitorSubType", (int)Subtype));
if (Subtype == Subtype.Custom)
if (Subtype == Subtype.Custom && Port.HasValue)
{
parameters.Add(UptimeClient.Parameter("monitorPort", Port));
}
@@ -30,7 +30,7 @@ namespace UptimeSharp.Models
{
get
{
return ItemDictionary["monitor"];
return ItemDictionary != null ? ItemDictionary["monitor"] : null;
}
}
}