diff --git a/UptimeSharp/Components/Alert.cs b/UptimeSharp.OldClassLib/Components/Alert.cs similarity index 100% rename from UptimeSharp/Components/Alert.cs rename to UptimeSharp.OldClassLib/Components/Alert.cs diff --git a/UptimeSharp/Components/Monitor.cs b/UptimeSharp.OldClassLib/Components/Monitor.cs similarity index 100% rename from UptimeSharp/Components/Monitor.cs rename to UptimeSharp.OldClassLib/Components/Monitor.cs diff --git a/UptimeSharp/JsonDeserializer.cs b/UptimeSharp.OldClassLib/JsonDeserializer.cs similarity index 100% rename from UptimeSharp/JsonDeserializer.cs rename to UptimeSharp.OldClassLib/JsonDeserializer.cs diff --git a/UptimeSharp/Models/Alert.cs b/UptimeSharp.OldClassLib/Models/Alert.cs similarity index 100% rename from UptimeSharp/Models/Alert.cs rename to UptimeSharp.OldClassLib/Models/Alert.cs diff --git a/UptimeSharp/Models/Log.cs b/UptimeSharp.OldClassLib/Models/Log.cs similarity index 100% rename from UptimeSharp/Models/Log.cs rename to UptimeSharp.OldClassLib/Models/Log.cs diff --git a/UptimeSharp/Models/Monitor.cs b/UptimeSharp.OldClassLib/Models/Monitor.cs similarity index 100% rename from UptimeSharp/Models/Monitor.cs rename to UptimeSharp.OldClassLib/Models/Monitor.cs diff --git a/UptimeSharp/Models/Parameters/MonitorParameters.cs b/UptimeSharp.OldClassLib/Models/Parameters/MonitorParameters.cs similarity index 100% rename from UptimeSharp/Models/Parameters/MonitorParameters.cs rename to UptimeSharp.OldClassLib/Models/Parameters/MonitorParameters.cs diff --git a/UptimeSharp/Models/Parameters/RetrieveParameters.cs b/UptimeSharp.OldClassLib/Models/Parameters/RetrieveParameters.cs similarity index 100% rename from UptimeSharp/Models/Parameters/RetrieveParameters.cs rename to UptimeSharp.OldClassLib/Models/Parameters/RetrieveParameters.cs diff --git a/UptimeSharp/Models/Response/AlertResponse.cs b/UptimeSharp.OldClassLib/Models/Response/AlertResponse.cs similarity index 100% rename from UptimeSharp/Models/Response/AlertResponse.cs rename to UptimeSharp.OldClassLib/Models/Response/AlertResponse.cs diff --git a/UptimeSharp/Models/Response/DefaultResponse.cs b/UptimeSharp.OldClassLib/Models/Response/DefaultResponse.cs similarity index 100% rename from UptimeSharp/Models/Response/DefaultResponse.cs rename to UptimeSharp.OldClassLib/Models/Response/DefaultResponse.cs diff --git a/UptimeSharp/Models/Response/ResponseBase.cs b/UptimeSharp.OldClassLib/Models/Response/ResponseBase.cs similarity index 100% rename from UptimeSharp/Models/Response/ResponseBase.cs rename to UptimeSharp.OldClassLib/Models/Response/ResponseBase.cs diff --git a/UptimeSharp/Models/Response/RetrieveResponse.cs b/UptimeSharp.OldClassLib/Models/Response/RetrieveResponse.cs similarity index 100% rename from UptimeSharp/Models/Response/RetrieveResponse.cs rename to UptimeSharp.OldClassLib/Models/Response/RetrieveResponse.cs diff --git a/UptimeSharp.OldClassLib/Properties/AssemblyInfo.cs b/UptimeSharp.OldClassLib/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..a28e5ad --- /dev/null +++ b/UptimeSharp.OldClassLib/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("UptimeSharp")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("cee")] +[assembly: AssemblyProduct("UptimeSharp")] +[assembly: AssemblyCopyright("Copyright © 2013")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("fe70c634-cebd-44b0-8287-1d75385058cf")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/UptimeSharp.OldClassLib/UptimeClient.cs b/UptimeSharp.OldClassLib/UptimeClient.cs new file mode 100644 index 0000000..75788ea --- /dev/null +++ b/UptimeSharp.OldClassLib/UptimeClient.cs @@ -0,0 +1,211 @@ +using RestSharp; +using System; +using System.Collections.Generic; +using System.Net; +using System.Reflection; + +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/"); + + /// + /// 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 typed HTTP REST request to the API + /// + /// + /// The request. + /// + protected T Request(RestRequest request) where T : new() + { + // fix content type if wrong determined by RestSharp + request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; }; + + IRestResponse response = _restClient.Execute(request); + + LastRequestData = response; + ValidateResponse(response); + + 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 => { + if(param.Value != null) + { + request.AddParameter(param); + } + } + ); + } + + // do the request + return Request(request); + } + + + /// + /// Fetches a typed resource + /// + /// + /// Requested resource + /// The parameters. + /// + protected T Get(string resource, params Parameter[] parameters) where T : class, new() + { + List parameterList = new List(); + parameterList.AddRange(parameters); + + return Get(resource, parameterList); + } + + + /// + /// Creates a RestSharp.Parameter instance. + /// + /// The name. + /// The value. + /// + internal static Parameter Parameter(string name, object value) + { + return new Parameter() + { + Name = name, + Value = value, + Type = ParameterType.GetOrPost + }; + } + + + /// + /// Validates the response. + /// + /// The response. + /// + /// + /// Error retrieving response + /// + protected void ValidateResponse(IRestResponse response) where T : new() + { + Type responseType = response.Data.GetType(); + + // get status property from ResponseBase POCO + PropertyInfo statusProp = responseType.GetProperty("Status"); + object status = statusProp.GetValue(response.Data, null); + + // Error from UptimeSharp + if (status.Equals(false)) + { + // get error properties from response + PropertyInfo errorProp = responseType.GetProperty("ErrorMessage"); + object error = errorProp.GetValue(response.Data, null); + + PropertyInfo errorCodeProp = responseType.GetProperty("ErrorCode"); + object errorCode = errorCodeProp.GetValue(response.Data, null); + + // create exception + UptimeSharpException exception = new UptimeSharpException( + "UptimeRobot Exception: {0} (Code: {1})\n{2}", + response.ErrorException, + error, + errorCode, + "http://uptimerobot.com/api.asp#errorMessages" + ); + + // add custom pocket fields + exception.Error = error.ToString(); + exception.ErrorCode = Convert.ToInt32(errorCode); + + // add to generic exception data + exception.Data.Add("Error", error); + exception.Data.Add("ErrorCode", errorCode); + + throw exception; + } + // HTTP Request Error + else if (response.StatusCode != HttpStatusCode.OK) + { + throw new UptimeSharpException( + "Request Exception: {0} (Code: {1})", + response.ErrorException, + response.ErrorMessage, + response.StatusCode + ); + } + // Exception by RestSharp + else if (response.ErrorException != null) + { + throw new UptimeSharpException("Error retrieving response", response.ErrorException); + } + } + } +} diff --git a/UptimeSharp.OldClassLib/UptimeSharp.OldClassLib.csproj b/UptimeSharp.OldClassLib/UptimeSharp.OldClassLib.csproj new file mode 100644 index 0000000..c3562c9 --- /dev/null +++ b/UptimeSharp.OldClassLib/UptimeSharp.OldClassLib.csproj @@ -0,0 +1,82 @@ + + + + + Debug + AnyCPU + {AF900ACF-DC2A-40AC-9614-B7C8C12E114C} + Library + Properties + UptimeSharp + UptimeSharp + v4.0 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + ..\packages\RestSharp.104.1\lib\net4\RestSharp.dll + + + ..\packages\ServiceStack.Text.3.9.56\lib\net35\ServiceStack.Text.dll + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/UptimeSharp/UptimeSharpException.cs b/UptimeSharp.OldClassLib/UptimeSharpException.cs similarity index 100% rename from UptimeSharp/UptimeSharpException.cs rename to UptimeSharp.OldClassLib/UptimeSharpException.cs diff --git a/UptimeSharp.OldClassLib/packages.config b/UptimeSharp.OldClassLib/packages.config new file mode 100644 index 0000000..e06ee85 --- /dev/null +++ b/UptimeSharp.OldClassLib/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/UptimeSharp.Tests/UptimeSharp.Tests.csproj b/UptimeSharp.Tests/UptimeSharp.Tests.csproj index a539405..8792965 100644 --- a/UptimeSharp.Tests/UptimeSharp.Tests.csproj +++ b/UptimeSharp.Tests/UptimeSharp.Tests.csproj @@ -30,6 +30,15 @@ 4 + + ..\packages\Microsoft.Bcl.Async.1.0.16\lib\net40\Microsoft.Threading.Tasks.dll + + + ..\packages\Microsoft.Bcl.Async.1.0.16\lib\net40\Microsoft.Threading.Tasks.Extensions.dll + + + ..\packages\Microsoft.Bcl.Async.1.0.16\lib\net40\Microsoft.Threading.Tasks.Extensions.Desktop.dll + False ..\packages\RestSharp.104.1\lib\net4\RestSharp.dll @@ -40,6 +49,13 @@ + + + ..\packages\Microsoft.Bcl.1.0.19\lib\net40\System.Runtime.dll + + + ..\packages\Microsoft.Bcl.1.0.19\lib\net40\System.Threading.Tasks.dll + @@ -60,9 +76,9 @@ - + {af900acf-dc2a-40ac-9614-b7c8c12e114c} - UptimeSharp + UptimeSharp.OldClassLib diff --git a/UptimeSharp.sln b/UptimeSharp.sln index e0e41a6..8a9d74f 100644 --- a/UptimeSharp.sln +++ b/UptimeSharp.sln @@ -1,7 +1,7 @@  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}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UptimeSharp.OldClassLib", "UptimeSharp.OldClassLib\UptimeSharp.OldClassLib.csproj", "{AF900ACF-DC2A-40AC-9614-B7C8C12E114C}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UptimeSharp.Tests", "UptimeSharp.Tests\UptimeSharp.Tests.csproj", "{7AB536B7-1A99-44A7-B5AA-E36E9C7F09F4}" EndProject diff --git a/UptimeSharp/Fody.targets b/UptimeSharp/Fody.targets new file mode 100644 index 0000000..a668a51 --- /dev/null +++ b/UptimeSharp/Fody.targets @@ -0,0 +1,89 @@ + + + + + + $(NCrunchOriginalSolutionDir) + + + + + $(SolutionDir) + + + + + $(MSBuildProjectDirectory)\..\ + + + + + + + $(KeyOriginatorFile) + + + + + $(AssemblyOriginatorKeyFile) + + + + + + + + + + $(ProjectDir)$(IntermediateOutputPath) + Low + $(SignAssembly) + $(MSBuildThisFileDirectory) + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/UptimeSharp/FodyWeavers.xml b/UptimeSharp/FodyWeavers.xml new file mode 100644 index 0000000..bb0f322 --- /dev/null +++ b/UptimeSharp/FodyWeavers.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/UptimeSharp/Properties/AssemblyInfo.cs b/UptimeSharp/Properties/AssemblyInfo.cs index a28e5ad..d71da8c 100644 --- a/UptimeSharp/Properties/AssemblyInfo.cs +++ b/UptimeSharp/Properties/AssemblyInfo.cs @@ -1,4 +1,5 @@ -using System.Reflection; +using System.Resources; +using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -8,19 +9,12 @@ using System.Runtime.InteropServices; [assembly: AssemblyTitle("UptimeSharp")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("cee")] +[assembly: AssemblyCompany("")] [assembly: AssemblyProduct("UptimeSharp")] [assembly: AssemblyCopyright("Copyright © 2013")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM -[assembly: Guid("fe70c634-cebd-44b0-8287-1d75385058cf")] +[assembly: NeutralResourcesLanguage("en")] // Version information for an assembly consists of the following four values: // diff --git a/UptimeSharp/UptimeClient.cs b/UptimeSharp/UptimeClient.cs index 75788ea..077f3d8 100644 --- a/UptimeSharp/UptimeClient.cs +++ b/UptimeSharp/UptimeClient.cs @@ -1,211 +1,11 @@ -using RestSharp; -using System; +using System; using System.Collections.Generic; -using System.Net; -using System.Reflection; +using System.Linq; +using System.Text; namespace UptimeSharp { - /// - /// UptimeClient - /// - public partial class UptimeClient + 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/"); - - /// - /// 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 typed HTTP REST request to the API - /// - /// - /// The request. - /// - protected T Request(RestRequest request) where T : new() - { - // fix content type if wrong determined by RestSharp - request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; }; - - IRestResponse response = _restClient.Execute(request); - - LastRequestData = response; - ValidateResponse(response); - - 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 => { - if(param.Value != null) - { - request.AddParameter(param); - } - } - ); - } - - // do the request - return Request(request); - } - - - /// - /// Fetches a typed resource - /// - /// - /// Requested resource - /// The parameters. - /// - protected T Get(string resource, params Parameter[] parameters) where T : class, new() - { - List parameterList = new List(); - parameterList.AddRange(parameters); - - return Get(resource, parameterList); - } - - - /// - /// Creates a RestSharp.Parameter instance. - /// - /// The name. - /// The value. - /// - internal static Parameter Parameter(string name, object value) - { - return new Parameter() - { - Name = name, - Value = value, - Type = ParameterType.GetOrPost - }; - } - - - /// - /// Validates the response. - /// - /// The response. - /// - /// - /// Error retrieving response - /// - protected void ValidateResponse(IRestResponse response) where T : new() - { - Type responseType = response.Data.GetType(); - - // get status property from ResponseBase POCO - PropertyInfo statusProp = responseType.GetProperty("Status"); - object status = statusProp.GetValue(response.Data, null); - - // Error from UptimeSharp - if (status.Equals(false)) - { - // get error properties from response - PropertyInfo errorProp = responseType.GetProperty("ErrorMessage"); - object error = errorProp.GetValue(response.Data, null); - - PropertyInfo errorCodeProp = responseType.GetProperty("ErrorCode"); - object errorCode = errorCodeProp.GetValue(response.Data, null); - - // create exception - UptimeSharpException exception = new UptimeSharpException( - "UptimeRobot Exception: {0} (Code: {1})\n{2}", - response.ErrorException, - error, - errorCode, - "http://uptimerobot.com/api.asp#errorMessages" - ); - - // add custom pocket fields - exception.Error = error.ToString(); - exception.ErrorCode = Convert.ToInt32(errorCode); - - // add to generic exception data - exception.Data.Add("Error", error); - exception.Data.Add("ErrorCode", errorCode); - - throw exception; - } - // HTTP Request Error - else if (response.StatusCode != HttpStatusCode.OK) - { - throw new UptimeSharpException( - "Request Exception: {0} (Code: {1})", - response.ErrorException, - response.ErrorMessage, - response.StatusCode - ); - } - // Exception by RestSharp - else if (response.ErrorException != null) - { - throw new UptimeSharpException("Error retrieving response", response.ErrorException); - } - } } } diff --git a/UptimeSharp/UptimeSharp.csproj b/UptimeSharp/UptimeSharp.csproj index c3562c9..41f97dc 100644 --- a/UptimeSharp/UptimeSharp.csproj +++ b/UptimeSharp/UptimeSharp.csproj @@ -2,18 +2,21 @@ + 10.0 Debug AnyCPU - {AF900ACF-DC2A-40AC-9614-B7C8C12E114C} + {EC1E656D-4966-499A-9EDD-9DB56137BF62} Library Properties UptimeSharp UptimeSharp v4.0 + Profile104 512 + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + ..\packages\Fody.1.17.4.0 - AnyCPU true full false @@ -23,7 +26,6 @@ 4 - AnyCPU pdbonly true bin\Release\ @@ -31,47 +33,50 @@ prompt 4 - - - - - - ..\packages\RestSharp.104.1\lib\net4\RestSharp.dll - - - ..\packages\ServiceStack.Text.3.9.56\lib\net35\ServiceStack.Text.dll - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - + + + + + + ..\packages\Microsoft.Bcl.Async.1.0.16\lib\portable-net40+sl4+win8+wp71\Microsoft.Threading.Tasks.dll + + + ..\packages\Microsoft.Bcl.Async.1.0.16\lib\portable-net40+sl4+win8+wp71\Microsoft.Threading.Tasks.Extensions.dll + + + ..\packages\Microsoft.Bcl.1.1.3\lib\portable-net40+sl4+win8+wp71\System.IO.dll + + + ..\packages\Microsoft.Net.Http.2.2.15\lib\portable-net40+sl4+win8+wp71\System.Net.Http.dll + + + ..\packages\Microsoft.Net.Http.2.2.15\lib\portable-net40+sl4+win8+wp71\System.Net.Http.Extensions.dll + + + ..\packages\Microsoft.Net.Http.2.2.15\lib\portable-net40+sl4+win8+wp71\System.Net.Http.Primitives.dll + + + ..\packages\Microsoft.Bcl.1.1.3\lib\portable-net40+sl4+win8+wp71\System.Runtime.dll + + + ..\packages\Microsoft.Bcl.1.1.3\lib\portable-net40+sl4+win8+wp71\System.Threading.Tasks.dll + + + + + + + + + + + +