From ea882dfc42e36a71fa503c49dea124c361828d28 Mon Sep 17 00:00:00 2001 From: ceee Date: Thu, 15 Aug 2013 11:13:15 +0200 Subject: [PATCH] add Monitor model; allow retrievel of monitors with additional overloads; --- UptimeSharp/Components/Get.cs | 63 ++++++++++++++ UptimeSharp/Models/Monitor.cs | 30 +++++++ UptimeSharp/Models/Response/Get.cs | 36 ++++++++ UptimeSharp/Models/Response/ResponseBase.cs | 20 +++++ UptimeSharp/UptimeClient.cs | 94 ++++++++++++++++++++- UptimeSharp/UptimeSharp.csproj | 7 ++ UptimeSharp/Utilities.cs | 2 +- 7 files changed, 248 insertions(+), 4 deletions(-) create mode 100644 UptimeSharp/Components/Get.cs create mode 100644 UptimeSharp/Models/Monitor.cs create mode 100644 UptimeSharp/Models/Response/Get.cs create mode 100644 UptimeSharp/Models/Response/ResponseBase.cs diff --git a/UptimeSharp/Components/Get.cs b/UptimeSharp/Components/Get.cs new file mode 100644 index 0000000..98394e5 --- /dev/null +++ b/UptimeSharp/Components/Get.cs @@ -0,0 +1,63 @@ +using System.Collections.Generic; +using UptimeSharp.Models; + +namespace UptimeSharp +{ + /// + /// UptimeClient + /// + public partial class UptimeClient + { + /// + /// Retrieves all monitors from UptimeRobot + /// + /// + public List Get() + { + return Get("getMonitors").Items; + } + + + /// + /// Retrieves a monitor from UptimeRobot + /// + /// a specific monitor ID + /// + public Monitor Get(int monitorId) + { + GetParameters parameters = new GetParameters() + { + Monitors = new int[] { monitorId } + }; + + return Get("getMonitors", parameters.Convert()).Items[0]; + } + + + /// + /// Retrieves specified monitors from UptimeRobot + /// + /// monitor list + /// + public List Get(int[] monitors) + { + GetParameters parameters = new GetParameters() + { + Monitors = monitors + }; + + return Get("getMonitors", parameters.Convert()).Items; + } + + + /// + /// Retrieves all monitors from UptimeRobot + /// + /// parameters, which are mapped to the officials from http://www.uptimerobot.com/api.asp#methods + /// + public List Get(GetParameters parameters) + { + return Get("getMonitors", parameters.Convert()).Items; + } + } +} \ No newline at end of file diff --git a/UptimeSharp/Models/Monitor.cs b/UptimeSharp/Models/Monitor.cs new file mode 100644 index 0000000..58a4c39 --- /dev/null +++ b/UptimeSharp/Models/Monitor.cs @@ -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 + { + /// + /// Gets or sets the ID. + /// + /// + /// The ID. + /// + [DataMember(Name = "id")] + public int ID { get; set; } + + /// + /// Gets or sets the name. + /// + /// + /// The name. + /// + [DataMember(Name = "friendlyname")] + public string Name { get; set; } + } +} diff --git a/UptimeSharp/Models/Response/Get.cs b/UptimeSharp/Models/Response/Get.cs new file mode 100644 index 0000000..3e4233c --- /dev/null +++ b/UptimeSharp/Models/Response/Get.cs @@ -0,0 +1,36 @@ +using System.Collections.Generic; +using System.Runtime.Serialization; + +namespace UptimeSharp.Models +{ + /// + /// Monitor Response + /// + [DataContract] + internal class Get : ResponseBase + { + /// + /// Gets or sets the item dictionary. + /// The list is 2 layers deep, so this one is necessary :-/ + /// + /// + /// The item dictionary. + /// + [DataMember(Name = "monitors")] + public Dictionary> ItemDictionary { get; set; } + + /// + /// Gets the items. + /// + /// + /// The items. + /// + [IgnoreDataMember] + public List Items + { + get { + return ItemDictionary["monitor"]; + } + } + } +} diff --git a/UptimeSharp/Models/Response/ResponseBase.cs b/UptimeSharp/Models/Response/ResponseBase.cs new file mode 100644 index 0000000..c3f9834 --- /dev/null +++ b/UptimeSharp/Models/Response/ResponseBase.cs @@ -0,0 +1,20 @@ +using System.Runtime.Serialization; + +namespace UptimeSharp.Models +{ + /// + /// Base for Responses + /// + [DataContract] + internal class ResponseBase + { + /// + /// Gets or sets a value indicating whether this is status. + /// + /// + /// true if status is OK; otherwise, false. + /// + [DataMember(Name = "stat")] + public bool Status { get; set; } + } +} diff --git a/UptimeSharp/UptimeClient.cs b/UptimeSharp/UptimeClient.cs index b88fe27..c94d6aa 100644 --- a/UptimeSharp/UptimeClient.cs +++ b/UptimeSharp/UptimeClient.cs @@ -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 /// /// The request. /// - public string Request(RestRequest request) + protected string Request(RestRequest request) { IRestResponse response = _restClient.Execute(request); @@ -98,5 +96,95 @@ namespace UptimeSharp return response.Data; } + + + /// + /// Fetches a typed resource + /// + /// + /// Requested resource + /// Additional GET parameters + /// + protected T Get(string resource, List 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(request); + } + + + ///// + ///// Puts an action + ///// + ///// The action parameter. + ///// + //protected bool PutSendAction(ActionParameter actionParameter) + //{ + // ModifyParameters parameters = new ModifyParameters() + // { + // Actions = new List() { actionParameter } + // }; + + // return Get("send", parameters.Convert(), true).Status; + //} + + + ///// + ///// Validates the response. + ///// + ///// The response. + ///// + ///// + ///// Error retrieving response + ///// + //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); + // } + //} } } diff --git a/UptimeSharp/UptimeSharp.csproj b/UptimeSharp/UptimeSharp.csproj index 42cea7f..42bed90 100644 --- a/UptimeSharp/UptimeSharp.csproj +++ b/UptimeSharp/UptimeSharp.csproj @@ -43,6 +43,7 @@ + @@ -51,7 +52,12 @@ + + + + + @@ -59,6 +65,7 @@ +