Last active
August 29, 2015 14:02
-
-
Save trcio/6019c4775f5addee8374 to your computer and use it in GitHub Desktop.
Instasign wrapper (my product)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.Serialization.Json; | |
using System.Runtime.Serialization; | |
using System.Collections.Specialized; | |
using System.Text; | |
using System.Net; | |
using System.IO; | |
using Instasign.Models; | |
namespace Instasign | |
{ | |
public static class Session | |
{ | |
private const string SignEndpoint = "http://instasign.us/api/sign.php"; | |
private const string ReportEndpoint = "http://instasign.us/api/report.php"; | |
private static string Apikey; | |
/// <summary> | |
/// Sets the API key for the session. | |
/// </summary> | |
public static string APIKey | |
{ | |
get { return Apikey; } | |
set | |
{ | |
if (value.Length != 32) | |
throw new InvalidDataException("Invalid key (Must be 32 characters)"); | |
else | |
Apikey = value; | |
} | |
} | |
public static SignedDataModel Sign(string RequestBody) | |
{ | |
if (string.IsNullOrEmpty(Apikey)) | |
throw new InvalidOperationException("APIKey must be set before signing"); | |
else if (string.IsNullOrEmpty(RequestBody)) | |
throw new ArgumentNullException("RequestBody cannot be empty"); | |
using (var client = GetClient()) | |
{ | |
var reqParams = GetParams(); | |
reqParams["data"] = RequestBody; | |
var response = Encoding.UTF8.GetString(client.UploadValues(SignEndpoint, reqParams)); | |
return JsonHelper.Deserialize<SignedDataModel>(response); | |
} | |
} | |
public static ReportedDataModel Report(string ReportHash) | |
{ | |
if (string.IsNullOrEmpty(Apikey)) | |
throw new InvalidOperationException("APIKey must be set before reporting"); | |
else if (string.IsNullOrEmpty(ReportHash) || ReportHash.Length != 32) | |
throw new ArgumentException("Invalid hash (Must be 32 characters)", "ReportHash"); | |
using (var client = GetClient()) | |
{ | |
var reqParams = GetParams(); | |
reqParams["hash"] = ReportHash; | |
var response = Encoding.UTF8.GetString(client.UploadValues(ReportEndpoint, reqParams)); | |
return JsonHelper.Deserialize<ReportedDataModel>(response); | |
} | |
} | |
private static WebClient GetClient() | |
{ | |
var client = new WebClient(); | |
client.Proxy = null; | |
return client; | |
} | |
private static NameValueCollection GetParams() | |
{ | |
var reqParams = new NameValueCollection(); | |
reqParams["key"] = Apikey; | |
return reqParams; | |
} | |
} | |
internal static class JsonHelper | |
{ | |
/// <summary> | |
/// Serializes an object to the respectable JSON string. | |
/// </summary> | |
public static string Serialize<T>(T t) | |
{ | |
var ser = new DataContractJsonSerializer(typeof(T)); | |
using (var ms = new MemoryStream()) | |
{ | |
ser.WriteObject(ms, t); | |
return Encoding.UTF8.GetString(ms.ToArray()); | |
} | |
} | |
/// <summary> | |
/// Deserializes a JSON string to the specified object. | |
/// </summary> | |
public static T Deserialize<T>(string json) | |
{ | |
var ser = new DataContractJsonSerializer(typeof(T)); | |
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(json))) | |
{ | |
return (T)ser.ReadObject(ms); | |
} | |
} | |
} | |
} | |
namespace Instasign.Models | |
{ | |
[DataContract] | |
public class SignedDataModel | |
{ | |
/// <summary> | |
/// Returns true if operation was a success. | |
/// </summary> | |
[DataMember(Name = "error")] | |
public bool Error { get; protected set; } | |
/// <summary> | |
/// Returns the status of the operation. | |
/// </summary> | |
[DataMember(Name = "status")] | |
public string Status { get; protected set; } | |
/// <summary> | |
/// Returns the unique identification hash for the operation. | |
/// </summary> | |
[DataMember(Name = "error_hash")] | |
public string ReportHash { get; protected set; } | |
/// <summary> | |
/// Returns the signed data, encoded in Base64. | |
/// </summary> | |
[DataMember(Name = "signed_body")] | |
public string SignedData { get; protected set; } | |
} | |
[DataContract] | |
public class ReportedDataModel | |
{ | |
/// <summary> | |
/// Returns true if the operation was a success. | |
/// </summary> | |
[DataMember(Name = "error")] | |
public bool Error { get; protected set; } | |
/// <summary> | |
/// Returns the status of the operation. | |
/// </summary> | |
[DataMember(Name = "status")] | |
public string Status { get; protected set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment