mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-06-09 00:46:19 +00:00
325 lines
12 KiB
C#
325 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Security;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using CryptoExchange.Net.Logging;
|
|
using CryptoExchange.Net.Objects;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace CryptoExchange.Net
|
|
{
|
|
/// <summary>
|
|
/// Helper methods
|
|
/// </summary>
|
|
public static class ExtensionMethods
|
|
{
|
|
/// <summary>
|
|
/// Add a parameter
|
|
/// </summary>
|
|
/// <param name="parameters"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="value"></param>
|
|
public static void AddParameter(this Dictionary<string, object> parameters, string key, string value)
|
|
{
|
|
parameters.Add(key, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add a parameter
|
|
/// </summary>
|
|
/// <param name="parameters"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="value"></param>
|
|
/// <param name="converter"></param>
|
|
public static void AddParameter(this Dictionary<string, object> parameters, string key, string value, JsonConverter converter)
|
|
{
|
|
parameters.Add(key, JsonConvert.SerializeObject(value, converter));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add a parameter
|
|
/// </summary>
|
|
/// <param name="parameters"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="value"></param>
|
|
public static void AddParameter(this Dictionary<string, object> parameters, string key, object value)
|
|
{
|
|
parameters.Add(key, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add a parameter
|
|
/// </summary>
|
|
/// <param name="parameters"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="value"></param>
|
|
/// <param name="converter"></param>
|
|
public static void AddParameter(this Dictionary<string, object> parameters, string key, object value, JsonConverter converter)
|
|
{
|
|
parameters.Add(key, JsonConvert.SerializeObject(value, converter));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add an optional parameter. Not added if value is null
|
|
/// </summary>
|
|
/// <param name="parameters"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="value"></param>
|
|
public static void AddOptionalParameter(this Dictionary<string, object> parameters, string key, object? value)
|
|
{
|
|
if(value != null)
|
|
parameters.Add(key, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add an optional parameter. Not added if value is null
|
|
/// </summary>
|
|
/// <param name="parameters"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="value"></param>
|
|
/// <param name="converter"></param>
|
|
public static void AddOptionalParameter(this Dictionary<string, object> parameters, string key, object? value, JsonConverter converter)
|
|
{
|
|
if (value != null)
|
|
parameters.Add(key, JsonConvert.SerializeObject(value, converter));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add an optional parameter. Not added if value is null
|
|
/// </summary>
|
|
/// <param name="parameters"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="value"></param>
|
|
public static void AddOptionalParameter(this Dictionary<string, string> parameters, string key, string? value)
|
|
{
|
|
if (value != null)
|
|
parameters.Add(key, value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add an optional parameter. Not added if value is null
|
|
/// </summary>
|
|
/// <param name="parameters"></param>
|
|
/// <param name="key"></param>
|
|
/// <param name="value"></param>
|
|
/// <param name="converter"></param>
|
|
public static void AddOptionalParameter(this Dictionary<string, string> parameters, string key, string? value, JsonConverter converter)
|
|
{
|
|
if (value != null)
|
|
parameters.Add(key, JsonConvert.SerializeObject(value, converter));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a query string of the specified parameters
|
|
/// </summary>
|
|
/// <param name="parameters">The parameters to use</param>
|
|
/// <param name="urlEncodeValues">Whether or not the values should be url encoded</param>
|
|
/// <param name="serializationType">How to serialize array parameters</param>
|
|
/// <returns></returns>
|
|
public static string CreateParamString(this Dictionary<string, object> parameters, bool urlEncodeValues, ArrayParametersSerialization serializationType)
|
|
{
|
|
var uriString = string.Empty;
|
|
var arraysParameters = parameters.Where(p => p.Value.GetType().IsArray).ToList();
|
|
foreach (var arrayEntry in arraysParameters)
|
|
{
|
|
if(serializationType == ArrayParametersSerialization.Array)
|
|
uriString += $"{string.Join("&", ((object[])(urlEncodeValues ? Uri.EscapeDataString(arrayEntry.Value.ToString()) : arrayEntry.Value)).Select(v => $"{arrayEntry.Key}[]={v}"))}&";
|
|
else
|
|
{
|
|
var array = (Array)arrayEntry.Value;
|
|
uriString += string.Join("&", array.OfType<object>().Select(a => $"{arrayEntry.Key}={Uri.EscapeDataString(a.ToString())}"));
|
|
uriString += "&";
|
|
}
|
|
}
|
|
|
|
uriString += $"{string.Join("&", parameters.Where(p => !p.Value.GetType().IsArray).Select(s => $"{s.Key}={(urlEncodeValues ? Uri.EscapeDataString(s.Value.ToString()) : s.Value)}"))}";
|
|
uriString = uriString.TrimEnd('&');
|
|
return uriString;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the string the secure string is representing
|
|
/// </summary>
|
|
/// <param name="source">The source secure string</param>
|
|
/// <returns></returns>
|
|
public static string GetString(this SecureString source)
|
|
{
|
|
lock (source)
|
|
{
|
|
string result;
|
|
var length = source.Length;
|
|
var pointer = IntPtr.Zero;
|
|
var chars = new char[length];
|
|
|
|
try
|
|
{
|
|
pointer = Marshal.SecureStringToBSTR(source);
|
|
Marshal.Copy(pointer, chars, 0, length);
|
|
|
|
result = string.Join("", chars);
|
|
}
|
|
finally
|
|
{
|
|
if (pointer != IntPtr.Zero)
|
|
{
|
|
Marshal.ZeroFreeBSTR(pointer);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a secure string from a string
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static SecureString ToSecureString(this string source)
|
|
{
|
|
var secureString = new SecureString();
|
|
foreach (var c in source)
|
|
secureString.AppendChar(c);
|
|
secureString.MakeReadOnly();
|
|
return secureString;
|
|
}
|
|
|
|
/// <summary>
|
|
/// String to JToken
|
|
/// </summary>
|
|
/// <param name="stringData"></param>
|
|
/// <param name="log"></param>
|
|
/// <returns></returns>
|
|
public static JToken? ToJToken(this string stringData, Log? log = null)
|
|
{
|
|
if (string.IsNullOrEmpty(stringData))
|
|
return null;
|
|
|
|
try
|
|
{
|
|
return JToken.Parse(stringData);
|
|
}
|
|
catch (JsonReaderException jre)
|
|
{
|
|
var info = $"Deserialize JsonReaderException: {jre.Message}, Path: {jre.Path}, LineNumber: {jre.LineNumber}, LinePosition: {jre.LinePosition}. Data: {stringData}";
|
|
log?.Write(LogLevel.Error, info);
|
|
if (log == null) Debug.WriteLine(info);
|
|
return null;
|
|
}
|
|
catch (JsonSerializationException jse)
|
|
{
|
|
var info = $"Deserialize JsonSerializationException: {jse.Message}. Data: {stringData}";
|
|
log?.Write(LogLevel.Error, info);
|
|
if (log == null) Debug.WriteLine(info);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validates an int is one of the allowed values
|
|
/// </summary>
|
|
/// <param name="value">Value of the int</param>
|
|
/// <param name="argumentName">Name of the parameter</param>
|
|
/// <param name="allowedValues">Allowed values</param>
|
|
public static void ValidateIntValues(this int value, string argumentName, params int[] allowedValues)
|
|
{
|
|
if (!allowedValues.Contains(value))
|
|
throw new ArgumentException(
|
|
$"{value} not allowed for parameter {argumentName}, allowed values: {string.Join(", ", allowedValues)}", argumentName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validates an int is between two values
|
|
/// </summary>
|
|
/// <param name="value">The value of the int</param>
|
|
/// <param name="argumentName">Name of the parameter</param>
|
|
/// <param name="minValue">Min value</param>
|
|
/// <param name="maxValue">Max value</param>
|
|
public static void ValidateIntBetween(this int value, string argumentName, int minValue, int maxValue)
|
|
{
|
|
if (value < minValue || value > maxValue)
|
|
throw new ArgumentException(
|
|
$"{value} not allowed for parameter {argumentName}, min: {minValue}, max: {maxValue}", argumentName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validates a string is not null or empty
|
|
/// </summary>
|
|
/// <param name="value">The value of the string</param>
|
|
/// <param name="argumentName">Name of the parameter</param>
|
|
public static void ValidateNotNull(this string value, string argumentName)
|
|
{
|
|
if (string.IsNullOrEmpty(value))
|
|
throw new ArgumentException($"No value provided for parameter {argumentName}", argumentName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validates a string is null or not empty
|
|
/// </summary>
|
|
/// <param name="value"></param>
|
|
/// <param name="argumentName"></param>
|
|
public static void ValidateNullOrNotEmpty(this string value, string argumentName)
|
|
{
|
|
if (value != null && string.IsNullOrEmpty(value))
|
|
throw new ArgumentException($"No value provided for parameter {argumentName}", argumentName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validates an object is not null
|
|
/// </summary>
|
|
/// <param name="value">The value of the object</param>
|
|
/// <param name="argumentName">Name of the parameter</param>
|
|
public static void ValidateNotNull(this object value, string argumentName)
|
|
{
|
|
if (value == null)
|
|
throw new ArgumentException($"No value provided for parameter {argumentName}", argumentName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validates a list is not null or empty
|
|
/// </summary>
|
|
/// <param name="value">The value of the object</param>
|
|
/// <param name="argumentName">Name of the parameter</param>
|
|
public static void ValidateNotNull<T>(this IEnumerable<T> value, string argumentName)
|
|
{
|
|
if (value == null || !value.Any())
|
|
throw new ArgumentException($"No values provided for parameter {argumentName}", argumentName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Format an exception and inner exception to a readable string
|
|
/// </summary>
|
|
/// <param name="exception"></param>
|
|
/// <returns></returns>
|
|
public static string ToLogString(this Exception exception)
|
|
{
|
|
var message = new StringBuilder();
|
|
var indent = 0;
|
|
while (exception != null)
|
|
{
|
|
for (var i = 0; i < indent; i++)
|
|
message.Append(' ');
|
|
message.Append(exception.GetType().Name);
|
|
message.Append(" - ");
|
|
message.AppendLine(exception.Message);
|
|
for (var i = 0; i < indent; i++)
|
|
message.Append(' ');
|
|
message.AppendLine(exception.StackTrace);
|
|
|
|
indent += 2;
|
|
exception = exception.InnerException;
|
|
}
|
|
|
|
return message.ToString();
|
|
}
|
|
}
|
|
}
|
|
|