using System;
using System.Net.Http;
using System.Threading.Tasks;
using Flurl;
using Flurl.Http;
namespace myApp
{
class Program
{
static void Main()
{
MainAsync().Wait();
}
static async Task MainAsync()
{
GillieAPI gAPI;
gAPI = new GillieAPI("https://gillie.io", "..insert public key here...", "...insert private key here...");
string jsonResult = await gAPI.callAPI("get", "/api/customers");
Console.WriteLine(jsonResult.ToString());
}
}
}
public class GillieAPI
{
static readonly HttpClient client = new HttpClient();
private string host;
private string publicKey;
private string privateKey;
public GillieAPI(string hostName, string publicK, string privateK)
{
host = hostName;
publicKey = publicK;
privateKey = privateK;
}
private static string ToHex(byte[] bytes)
{
System.Text.StringBuilder result = new System.Text.StringBuilder(bytes.Length * 2);
for (int i = 0; i < bytes.Length; i++)
result.Append(bytes[i].ToString("x2"));
return result.ToString();
}
private static string SHA256HexHashString(string StringIn)
{
string hashString;
using (var sha256 = System.Security.Cryptography.SHA256Managed.Create())
{
var hash = sha256.ComputeHash(System.Text.Encoding.Default.GetBytes(StringIn));
hashString = ToHex(hash);
}
return hashString;
}
public async Task<string> callAPI(string method, string url, object urlparams = null, object body = null)
{
string fullUrl = host + url;
Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
string salt = unixTimestamp.ToString();
string hash = SHA256HexHashString(privateKey + publicKey + salt);
string response = "";
Url callUrl = fullUrl.SetQueryParams(urlparams)
.SetQueryParam("apikey", publicKey)
.SetQueryParam("apisalt", salt)
.SetQueryParam("apihash", hash);
Console.WriteLine("Call Gillie API with URL: " + callUrl.ToString());
if (method == "get")
{
response = await callUrl.GetStringAsync();
}
else if (method == "post")
{
response = await callUrl.PostJsonAsync(body).ReceiveString();
}
else if (method == "delete")
{
response = await callUrl.DeleteAsync().ReceiveString();
}
return response;
}
}