add Monitor model; allow retrievel of monitors with additional overloads;

This commit is contained in:
2013-08-15 11:13:15 +02:00
parent 317605debf
commit ea882dfc42
7 changed files with 248 additions and 4 deletions
+63
View File
@@ -0,0 +1,63 @@
using System.Collections.Generic;
using UptimeSharp.Models;
namespace UptimeSharp
{
/// <summary>
/// UptimeClient
/// </summary>
public partial class UptimeClient
{
/// <summary>
/// Retrieves all monitors from UptimeRobot
/// </summary>
/// <returns></returns>
public List<Monitor> Get()
{
return Get<Get>("getMonitors").Items;
}
/// <summary>
/// Retrieves a monitor from UptimeRobot
/// </summary>
/// <param name="monitorId">a specific monitor ID</param>
/// <returns></returns>
public Monitor Get(int monitorId)
{
GetParameters parameters = new GetParameters()
{
Monitors = new int[] { monitorId }
};
return Get<Get>("getMonitors", parameters.Convert()).Items[0];
}
/// <summary>
/// Retrieves specified monitors from UptimeRobot
/// </summary>
/// <param name="monitors">monitor list</param>
/// <returns></returns>
public List<Monitor> Get(int[] monitors)
{
GetParameters parameters = new GetParameters()
{
Monitors = monitors
};
return Get<Get>("getMonitors", parameters.Convert()).Items;
}
/// <summary>
/// Retrieves all monitors from UptimeRobot
/// </summary>
/// <param name="parameters">parameters, which are mapped to the officials from http://www.uptimerobot.com/api.asp#methods </param>
/// <returns></returns>
public List<Monitor> Get(GetParameters parameters)
{
return Get<Get>("getMonitors", parameters.Convert()).Items;
}
}
}
+30
View File
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace UptimeSharp.Models
{
[DataContract]
public class Monitor
{
/// <summary>
/// Gets or sets the ID.
/// </summary>
/// <value>
/// The ID.
/// </value>
[DataMember(Name = "id")]
public int ID { get; set; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>
/// The name.
/// </value>
[DataMember(Name = "friendlyname")]
public string Name { get; set; }
}
}
+36
View File
@@ -0,0 +1,36 @@
using System.Collections.Generic;
using System.Runtime.Serialization;
namespace UptimeSharp.Models
{
/// <summary>
/// Monitor Response
/// </summary>
[DataContract]
internal class Get : ResponseBase
{
/// <summary>
/// Gets or sets the item dictionary.
/// The list is 2 layers deep, so this one is necessary :-/
/// </summary>
/// <value>
/// The item dictionary.
/// </value>
[DataMember(Name = "monitors")]
public Dictionary<string, List<Monitor>> ItemDictionary { get; set; }
/// <summary>
/// Gets the items.
/// </summary>
/// <value>
/// The items.
/// </value>
[IgnoreDataMember]
public List<Monitor> Items
{
get {
return ItemDictionary["monitor"];
}
}
}
}
@@ -0,0 +1,20 @@
using System.Runtime.Serialization;
namespace UptimeSharp.Models
{
/// <summary>
/// Base for Responses
/// </summary>
[DataContract]
internal class ResponseBase
{
/// <summary>
/// Gets or sets a value indicating whether this <see cref="ResponseBase"/> is status.
/// </summary>
/// <value>
/// <c>true</c> if status is OK; otherwise, <c>false</c>.
/// </value>
[DataMember(Name = "stat")]
public bool Status { get; set; }
}
}
+91 -3
View File
@@ -1,8 +1,6 @@
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UptimeSharp
{
@@ -73,7 +71,7 @@ namespace UptimeSharp
/// </summary>
/// <param name="request">The request.</param>
/// <returns></returns>
public string Request(RestRequest request)
protected string Request(RestRequest request)
{
IRestResponse response = _restClient.Execute(request);
@@ -98,5 +96,95 @@ namespace UptimeSharp
return response.Data;
}
/// <summary>
/// Fetches a typed resource
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="resource">Requested resource</param>
/// <param name="parameters">Additional GET parameters</param>
/// <returns></returns>
protected T Get<T>(string resource, List<Parameter> parameters = null) where T : class, new()
{
// UptimeRobot uses GET for all of its endpoints
var request = new RestRequest(resource, Method.GET);
// enumeration for params
if (parameters != null)
{
parameters.ForEach(
param => request.AddParameter(param)
);
}
// do the request
return Request<T>(request);
}
///// <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;
//}
///// <summary>
///// Validates the response.
///// </summary>
///// <param name="response">The response.</param>
///// <returns></returns>
///// <exception cref="APIException">
///// Error retrieving response
///// </exception>
//protected void ValidateResponse(IRestResponse response)
//{
// if (response.StatusCode != HttpStatusCode.OK)
// {
// // get pocket error headers
// Parameter error = response.Headers[1];
// Parameter errorCode = response.Headers[2];
// string exceptionString = response.Content;
// bool isPocketError = error.Name == "X-Error";
// // update message to include pocket response data
// if (isPocketError)
// {
// exceptionString = exceptionString + "\nPocketResponse: (" + errorCode.Value + ") " + error.Value;
// }
// // create exception
// APIException exception = new APIException(exceptionString, response.ErrorException);
// if (isPocketError)
// {
// // add custom pocket fields
// exception.PocketError = error.Value.ToString();
// exception.PocketErrorCode = Convert.ToInt32(errorCode.Value);
// // add to generic exception data
// exception.Data.Add(error.Name, error.Value);
// exception.Data.Add(errorCode.Name, errorCode.Value);
// }
// throw exception;
// }
// else if (response.ErrorException != null)
// {
// throw new APIException("Error retrieving response", response.ErrorException);
// }
//}
}
}
+7
View File
@@ -43,6 +43,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
@@ -51,7 +52,12 @@
</ItemGroup>
<ItemGroup>
<Compile Include="APIException.cs" />
<Compile Include="Components\Get.cs" />
<Compile Include="JsonDeserializer.cs" />
<Compile Include="Models\Monitor.cs" />
<Compile Include="Models\Parameters\GetParameters.cs" />
<Compile Include="Models\Response\Get.cs" />
<Compile Include="Models\Response\ResponseBase.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UptimeClient.cs" />
<Compile Include="Utilities.cs" />
@@ -59,6 +65,7 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
+1 -1
View File
@@ -15,7 +15,7 @@ namespace UptimeSharp
/// </summary>
/// <param name="value">The value.</param>
/// <returns>A string representation of the boolean</returns>
public static string? Bool(bool? value)
public static string Bool(bool? value)
{
return value != null ? ((bool)value ? "1" : "0") : null;
}