add APIException and universal JsonDeserializer
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace UptimeSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// custom UptimeRobot API Exceptions
|
||||
/// </summary>
|
||||
public class APIException : Exception
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the UptimeRobot error code.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The pocket error code.
|
||||
/// </value>
|
||||
public int? UptimeErrorCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the UptimeRobot error.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The pocket error.
|
||||
/// </value>
|
||||
public string UptimeError { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="APIException"/> class.
|
||||
/// </summary>
|
||||
public APIException()
|
||||
: base() { }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="APIException"/> class.
|
||||
/// </summary>
|
||||
/// <param name="message">The message that describes the error.</param>
|
||||
public APIException(string message)
|
||||
: base(message) { }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="APIException"/> class.
|
||||
/// </summary>
|
||||
/// <param name="message">The error message that explains the reason for the exception.</param>
|
||||
/// <param name="innerException">The exception that is the cause of the current exception, or a null reference (Nothing in Visual Basic) if no inner exception is specified.</param>
|
||||
public APIException(string message, Exception innerException)
|
||||
: base(message, innerException) { }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="APIException"/> class.
|
||||
/// </summary>
|
||||
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo" /> that holds the serialized object data about the exception being thrown.</param>
|
||||
/// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext" /> that contains contextual information about the source or destination.</param>
|
||||
protected APIException(SerializationInfo info, StreamingContext context)
|
||||
: base(info, context) { }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
using RestSharp;
|
||||
using RestSharp.Deserializers;
|
||||
using ServiceStack.Text;
|
||||
using System;
|
||||
|
||||
namespace UptimeSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// Custom JSON Deserializer which implements ServiceStack.Text
|
||||
/// </summary>
|
||||
internal class JsonDeserializer : IDeserializer
|
||||
{
|
||||
public const string JsonContentType = "application/json";
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes the specified response.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="response">The response.</param>
|
||||
/// <returns></returns>
|
||||
public T Deserialize<T>(IRestResponse response)
|
||||
{
|
||||
return JsonSerializer.DeserializeFromString<T>(response.Content);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Adds custom deserialization for specific types.
|
||||
/// </summary>
|
||||
public static void AddCustomDeserialization()
|
||||
{
|
||||
// generate correct Uri format
|
||||
JsConfig<Uri>.DeSerializeFn = value => new Uri(value);
|
||||
|
||||
// create DateTime from UNIX timestamp input
|
||||
JsConfig<DateTime?>.DeSerializeFn = value =>
|
||||
{
|
||||
if (value == "0") return null;
|
||||
return new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(Convert.ToDouble(value)).ToLocalTime();
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the date format.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The date format.
|
||||
/// </value>
|
||||
public string DateFormat { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the namespace.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The namespace.
|
||||
/// </value>
|
||||
public string Namespace { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the root element.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The root element.
|
||||
/// </value>
|
||||
public string RootElement { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type of the content.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The type of the content.
|
||||
/// </value>
|
||||
public string ContentType
|
||||
{
|
||||
get { return JsonContentType; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ using System.Runtime.InteropServices;
|
||||
[assembly: AssemblyTitle("UptimeSharp")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyCompany("cee")]
|
||||
[assembly: AssemblyProduct("UptimeSharp")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2013")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
|
||||
@@ -32,6 +32,12 @@
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="RestSharp">
|
||||
<HintPath>..\packages\RestSharp.104.1\lib\net4\RestSharp.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="ServiceStack.Text">
|
||||
<HintPath>..\packages\ServiceStack.Text.3.9.56\lib\net35\ServiceStack.Text.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
@@ -41,8 +47,14 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="APIException.cs" />
|
||||
<Compile Include="JsonDeserializer.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Utilities.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
|
||||
Reference in New Issue
Block a user