From 76542c2e54dbe93eb01273b50b627a70a3569f92 Mon Sep 17 00:00:00 2001 From: ceee Date: Thu, 15 Aug 2013 00:45:55 +0200 Subject: [PATCH] add UptimeClient with default parameters; --- UptimeSharp.sln | 6 ++ UptimeSharp/Program.cs | 14 ----- UptimeSharp/UptimeClient.cs | 102 +++++++++++++++++++++++++++++++++ UptimeSharp/UptimeSharp.csproj | 7 ++- 4 files changed, 113 insertions(+), 16 deletions(-) delete mode 100644 UptimeSharp/Program.cs create mode 100644 UptimeSharp/UptimeClient.cs diff --git a/UptimeSharp.sln b/UptimeSharp.sln index 5d73f1d..ede18fa 100644 --- a/UptimeSharp.sln +++ b/UptimeSharp.sln @@ -3,6 +3,8 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 2012 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UptimeSharp", "UptimeSharp\UptimeSharp.csproj", "{AF900ACF-DC2A-40AC-9614-B7C8C12E114C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UptimeSharp.Console", "UptimeSharp.Console\UptimeSharp.Console.csproj", "{CDA2493E-AF18-4D75-84B6-CDA4CCBE733F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -13,6 +15,10 @@ Global {AF900ACF-DC2A-40AC-9614-B7C8C12E114C}.Debug|Any CPU.Build.0 = Debug|Any CPU {AF900ACF-DC2A-40AC-9614-B7C8C12E114C}.Release|Any CPU.ActiveCfg = Release|Any CPU {AF900ACF-DC2A-40AC-9614-B7C8C12E114C}.Release|Any CPU.Build.0 = Release|Any CPU + {CDA2493E-AF18-4D75-84B6-CDA4CCBE733F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CDA2493E-AF18-4D75-84B6-CDA4CCBE733F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CDA2493E-AF18-4D75-84B6-CDA4CCBE733F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CDA2493E-AF18-4D75-84B6-CDA4CCBE733F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/UptimeSharp/Program.cs b/UptimeSharp/Program.cs deleted file mode 100644 index d42fea2..0000000 --- a/UptimeSharp/Program.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace UptimeSharp -{ - class Program - { - static void Main(string[] args) - { - } - } -} diff --git a/UptimeSharp/UptimeClient.cs b/UptimeSharp/UptimeClient.cs new file mode 100644 index 0000000..b88fe27 --- /dev/null +++ b/UptimeSharp/UptimeClient.cs @@ -0,0 +1,102 @@ +using RestSharp; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace UptimeSharp +{ + /// + /// UptimeClient + /// + public partial class UptimeClient + { + /// + /// REST client used for the API communication + /// + protected readonly RestClient _restClient; + + /// + /// The base URL for the UptimeRobot API + /// + protected static Uri baseUri = new Uri("http://api.uptimerobot.com/"); + + /// + /// callback URL for API calls + /// + protected string CallbackUri { get; set; } + + /// + /// Accessor for the UptimeRebot API key + /// see: http://http://www.uptimerobot.com/api.asp + /// + public string ApiKey { get; set; } + + /// + /// Returns all associated data from the last request + /// + public IRestResponse LastRequestData { get; private set; } + + + /// + /// Initializes a new instance of the class. + /// + /// The API key + public UptimeClient(string apiKey) + { + // assign public properties + ApiKey = apiKey; + + // initialize REST client + _restClient = new RestClient(baseUri.ToString()); + + // add default parameters to each request + _restClient.AddDefaultParameter("apiKey", ApiKey); + + // defines the response format (according to the UptimeRobot docs) + _restClient.AddDefaultParameter("format", "json"); + + // UptimeRobot returns by default JSON-P when json-formatting is set + // with this param it can return raw JSON + _restClient.AddDefaultParameter("noJsonCallback", "1"); + + // custom JSON deserializer (ServiceStack.Text) + _restClient.AddHandler("application/json", new JsonDeserializer()); + + // add custom deserialization lambdas + JsonDeserializer.AddCustomDeserialization(); + } + + + /// + /// Makes a HTTP REST request to the API + /// + /// The request. + /// + public string Request(RestRequest request) + { + IRestResponse response = _restClient.Execute(request); + + LastRequestData = response; + //ValidateResponse(response); + + return response.Content; + } + + /// + /// Makes a typed HTTP REST request to the API + /// + /// + /// The request. + /// + protected T Request(RestRequest request) where T : new() + { + IRestResponse response = _restClient.Execute(request); + + LastRequestData = response; + //ValidateResponse(response); + + return response.Data; + } + } +} diff --git a/UptimeSharp/UptimeSharp.csproj b/UptimeSharp/UptimeSharp.csproj index 0b4f974..42cea7f 100644 --- a/UptimeSharp/UptimeSharp.csproj +++ b/UptimeSharp/UptimeSharp.csproj @@ -5,7 +5,7 @@ Debug AnyCPU {AF900ACF-DC2A-40AC-9614-B7C8C12E114C} - Exe + Library Properties UptimeSharp UptimeSharp @@ -31,6 +31,9 @@ prompt 4 + + + ..\packages\RestSharp.104.1\lib\net4\RestSharp.dll @@ -49,8 +52,8 @@ - +