20 using Newtonsoft.Json;
21 using Newtonsoft.Json.Linq;
22 using System.Collections.Generic;
23 using System.Collections.Specialized;
36 public static string Hash(
int timestamp)
45 public static string Hash(
int timestamp,
string token)
49 var data = $
"{token}:{timestamp.ToStringInvariant()}";
50 return data.ToSHA256();
59 public static string Link(
string endpoint, IEnumerable<KeyValuePair<string, object>> payload =
null)
61 var queryString = HttpUtility.ParseQueryString(
string.Empty);
64 queryString.Add(
"authorization", Convert.ToBase64String(Encoding.UTF8.GetBytes($
"{Globals.UserId}:{Hash(timestamp)}")));
65 queryString.Add(
"timestamp", timestamp.ToStringInvariant());
69 return $
"{Globals.Api}{endpoint.RemoveFromStart("/
").RemoveFromEnd("/
")}?{queryString}";
76 public static void PopulateQueryString(NameValueCollection queryString, IEnumerable<KeyValuePair<string, object>> payload =
null)
80 foreach (var kv
in payload)
82 AddToQuery(queryString, kv);
90 private static void AddToQuery(NameValueCollection queryString, KeyValuePair<string, object> keyValuePairs)
92 var objectType = keyValuePairs.Value.GetType();
93 if (objectType.IsValueType || objectType == typeof(
string))
96 queryString.Add(keyValuePairs.Key, keyValuePairs.Value.ToString());
101 var serialized = JsonConvert.SerializeObject(keyValuePairs.Value);
102 foreach (var jObject
in JObject.Parse(serialized))
104 var subKey = $
"{keyValuePairs.Key}[{jObject.Key}]";
105 if (jObject.Value is JObject)
108 AddToQuery(queryString,
new KeyValuePair<string, object>(subKey, jObject.Value.ToObject<
object>()));
110 else if(jObject.Value is JArray jArray)
113 foreach (var value
in jArray.ToObject<List<object>>())
115 queryString.Add($
"{subKey}[{counter++}]", value.ToString());
120 queryString.Add(subKey, jObject.Value.ToString());