Skip to content

Commit

Permalink
Some small improvements and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf committed Mar 22, 2024
1 parent de72fe4 commit 8ddd9ec
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 22 deletions.
7 changes: 4 additions & 3 deletions CryptoExchange.Net/Converters/SystemTextJson/EnumConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ public EnumConverterInner(bool writeAsInt, bool warnOnMissingEntry)
{
JsonTokenType.String => reader.GetString(),
JsonTokenType.Number => reader.GetInt16().ToString(),
JsonTokenType.True => "true",
JsonTokenType.False => "false",
_ => null
JsonTokenType.True => reader.GetBoolean().ToString(),
JsonTokenType.False => reader.GetBoolean().ToString(),
JsonTokenType.Null => null,
_ => throw new Exception("Invalid token type for enum deserialization: " + reader.TokenType)
};

if (string.IsNullOrEmpty(stringValue))
Expand Down
69 changes: 55 additions & 14 deletions CryptoExchange.Net/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
using System.Web;
using CryptoExchange.Net.Objects;
using Microsoft.Extensions.Logging;
using System.Globalization;
using System.Collections;
using System.Net.Http;
using System.Data.Common;
using Newtonsoft.Json.Linq;

namespace CryptoExchange.Net
{
Expand Down Expand Up @@ -59,25 +64,30 @@ public static void AddOptionalParameter(this Dictionary<string, object> paramete
/// <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)
public static string CreateParamString(this IDictionary<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}"))}&";
uriString += $"{string.Join("&", ((object[])(urlEncodeValues ? Uri.EscapeDataString(arrayEntry.Value.ToString()) : arrayEntry.Value)).Select(v => $"{arrayEntry.Key}[]={string.Format(CultureInfo.InvariantCulture, "{0}", v)}"))}&";
}
else
else if (serializationType == ArrayParametersSerialization.MultipleValues)
{
var array = (Array)arrayEntry.Value;
uriString += string.Join("&", array.OfType<object>().Select(a => $"{arrayEntry.Key}={Uri.EscapeDataString(a.ToString())}"));
uriString += string.Join("&", array.OfType<object>().Select(a => $"{arrayEntry.Key}={Uri.EscapeDataString(string.Format(CultureInfo.InvariantCulture, "{0}", a))}"));
uriString += "&";
}
else
{
var array = (Array)arrayEntry.Value;
uriString += $"{arrayEntry.Key}=[{string.Join(",", array.OfType<object>().Select(a => string.Format(CultureInfo.InvariantCulture, "{0}", a)))}]&";
}
}

uriString += $"{string.Join("&", parameters.Where(p => !p.Value.GetType().IsArray).Select(s => $"{s.Key}={(urlEncodeValues ? Uri.EscapeDataString(s.Value.ToString()) : s.Value)}"))}";
uriString += $"{string.Join("&", parameters.Where(p => !p.Value.GetType().IsArray).Select(s => $"{s.Key}={(urlEncodeValues ? Uri.EscapeDataString(string.Format(CultureInfo.InvariantCulture, "{0}", s.Value)) : string.Format(CultureInfo.InvariantCulture, "{0}", s.Value))}"))}";
uriString = uriString.TrimEnd('&');
return uriString;
}
Expand All @@ -87,7 +97,7 @@ public static string CreateParamString(this Dictionary<string, object> parameter
/// </summary>
/// <param name="parameters"></param>
/// <returns></returns>
public static string ToFormData(this SortedDictionary<string, object> parameters)
public static string ToFormData(this IDictionary<string, object> parameters)
{
var formData = HttpUtility.ParseQueryString(string.Empty);
foreach (var kvp in parameters)
Expand All @@ -96,16 +106,15 @@ public static string ToFormData(this SortedDictionary<string, object> parameters
{
var array = (Array)kvp.Value;
foreach (var value in array)
formData.Add(kvp.Key, value.ToString());
formData.Add(kvp.Key, string.Format(CultureInfo.InvariantCulture, "{0}", value));
}
else
{
formData.Add(kvp.Key, kvp.Value.ToString());
formData.Add(kvp.Key, string.Format(CultureInfo.InvariantCulture, "{0}", kvp.Value));
}
}
return formData.ToString();
}


/// <summary>
/// Get the string the secure string is representing
Expand Down Expand Up @@ -349,10 +358,26 @@ public static Uri SetParameters(this Uri baseUri, SortedDictionary<string, objec
var httpValueCollection = HttpUtility.ParseQueryString(string.Empty);
foreach (var parameter in parameters)
{
if(parameter.Value.GetType().IsArray)
if (parameter.Value.GetType().IsArray)
{
foreach (var item in (object[])parameter.Value)
httpValueCollection.Add(arraySerialization == ArrayParametersSerialization.Array ? parameter.Key + "[]" : parameter.Key, item.ToString());
if (arraySerialization == ArrayParametersSerialization.JsonArray)
{
httpValueCollection.Add(parameter.Key, $"[{string.Join(",", (object[])parameter.Value)}]");
}
else
{
foreach (var item in (object[])parameter.Value)
{
if (arraySerialization == ArrayParametersSerialization.Array)
{
httpValueCollection.Add(parameter.Key + "[]", item.ToString());
}
else
{
httpValueCollection.Add(parameter.Key, item.ToString());
}
}
}
}
else
{
Expand Down Expand Up @@ -382,8 +407,24 @@ public static Uri SetParameters(this Uri baseUri, IOrderedEnumerable<KeyValuePai
{
if (parameter.Value.GetType().IsArray)
{
foreach (var item in (object[])parameter.Value)
httpValueCollection.Add(arraySerialization == ArrayParametersSerialization.Array ? parameter.Key + "[]" : parameter.Key, item.ToString());
if (arraySerialization == ArrayParametersSerialization.JsonArray)
{
httpValueCollection.Add(parameter.Key, $"[{string.Join(",", (object[])parameter.Value)}]");
}
else
{
foreach (var item in (object[])parameter.Value)
{
if (arraySerialization == ArrayParametersSerialization.Array)
{
httpValueCollection.Add(parameter.Key + "[]", item.ToString());
}
else
{
httpValueCollection.Add(parameter.Key, item.ToString());
}
}
}
}
else
{
Expand Down
9 changes: 7 additions & 2 deletions CryptoExchange.Net/Interfaces/ISymbolOrderBook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@ namespace CryptoExchange.Net.Interfaces
public interface ISymbolOrderBook
{
/// <summary>
/// Identifier
/// The exchange the book is for
/// </summary>
string Id { get; }
string Exchange { get; }

/// <summary>
/// The Api the book is for
/// </summary>
string Api { get; }

/// <summary>
/// The status of the order book. Order book is up to date when the status is `Synced`
Expand Down
13 changes: 10 additions & 3 deletions CryptoExchange.Net/Objects/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,22 @@ public enum OrderBookEntryType
/// Define how array parameters should be send
/// </summary>
public enum ArrayParametersSerialization
#pragma warning disable CS1570 // XML comment has badly formed XML
{
/// <summary>
/// Send multiple key=value for each entry
/// Send as key=value1&key=value2
/// </summary>
MultipleValues,

/// <summary>
/// Send as key[]=value1&key[]=value2
/// </summary>
Array,
/// <summary>
/// Create an []=value array
/// Send as key=[value1, value2]
/// </summary>
Array
JsonArray
#pragma warning restore CS1570 // XML comment has badly formed XML
}

/// <summary>
Expand Down

0 comments on commit 8ddd9ec

Please sign in to comment.