add UptimeClient with default parameters;

This commit is contained in:
2013-08-15 00:45:55 +02:00
parent 985bde5e9c
commit 76542c2e54
4 changed files with 113 additions and 16 deletions
+6
View File
@@ -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
-14
View File
@@ -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)
{
}
}
}
+102
View File
@@ -0,0 +1,102 @@
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace UptimeSharp
{
/// <summary>
/// UptimeClient
/// </summary>
public partial class UptimeClient
{
/// <summary>
/// REST client used for the API communication
/// </summary>
protected readonly RestClient _restClient;
/// <summary>
/// The base URL for the UptimeRobot API
/// </summary>
protected static Uri baseUri = new Uri("http://api.uptimerobot.com/");
/// <summary>
/// callback URL for API calls
/// </summary>
protected string CallbackUri { get; set; }
/// <summary>
/// Accessor for the UptimeRebot API key
/// see: http://http://www.uptimerobot.com/api.asp
/// </summary>
public string ApiKey { get; set; }
/// <summary>
/// Returns all associated data from the last request
/// </summary>
public IRestResponse LastRequestData { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="UptimeClient"/> class.
/// </summary>
/// <param name="apiKey">The API key</param>
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();
}
/// <summary>
/// Makes a HTTP REST request to the API
/// </summary>
/// <param name="request">The request.</param>
/// <returns></returns>
public string Request(RestRequest request)
{
IRestResponse response = _restClient.Execute(request);
LastRequestData = response;
//ValidateResponse(response);
return response.Content;
}
/// <summary>
/// Makes a typed HTTP REST request to the API
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="request">The request.</param>
/// <returns></returns>
protected T Request<T>(RestRequest request) where T : new()
{
IRestResponse<T> response = _restClient.Execute<T>(request);
LastRequestData = response;
//ValidateResponse(response);
return response.Data;
}
}
}
+5 -2
View File
@@ -5,7 +5,7 @@
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{AF900ACF-DC2A-40AC-9614-B7C8C12E114C}</ProjectGuid>
<OutputType>Exe</OutputType>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>UptimeSharp</RootNamespace>
<AssemblyName>UptimeSharp</AssemblyName>
@@ -31,6 +31,9 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="RestSharp">
<HintPath>..\packages\RestSharp.104.1\lib\net4\RestSharp.dll</HintPath>
@@ -49,8 +52,8 @@
<ItemGroup>
<Compile Include="APIException.cs" />
<Compile Include="JsonDeserializer.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UptimeClient.cs" />
<Compile Include="Utilities.cs" />
</ItemGroup>
<ItemGroup>