Skip to content

Commit

Permalink
Small fixed/improvements. Added deflate stream method
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf committed Jul 25, 2024
1 parent e1dafdf commit b309deb
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CryptoExchange.Net/Clients/RestApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ protected virtual async Task<WebCallResult<T>> SendAsync<T>(
uriParameters,
bodyParameters,
additionalHeaders);
_logger.RestApiSendRequest(request.RequestId, definition, request.Content, request.Uri.Query, string.Join(", ", request.GetHeaders().Select(h => h.Key + $"=[{string.Join(",", h.Value)}]")));
_logger.RestApiSendRequest(request.RequestId, definition, request.Content, string.IsNullOrEmpty(request.Uri.Query) ? "-" : request.Uri.Query, string.Join(", ", request.GetHeaders().Select(h => h.Key + $"=[{string.Join(",", h.Value)}]")));
TotalRequestsMade++;
var result = await GetResponseAsync<T>(request, definition.RateLimitGate, cancellationToken).ConfigureAwait(false);
if (!result)
Expand Down
6 changes: 5 additions & 1 deletion CryptoExchange.Net/Clients/SocketApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ protected virtual async Task<CallResult<UpdateSubscription>> SubscribeAsync(stri
return new CallResult<UpdateSubscription>(new InvalidOperationError("Client disposed, can't subscribe"));

if (subscription.Authenticated && AuthenticationProvider == null)
{
_logger.LogWarning("Failed to subscribe, private subscription but no API credentials set");
return new CallResult<UpdateSubscription>(new NoApiCredentialsError());
}

SocketConnection socketConnection;
var released = false;
Expand Down Expand Up @@ -786,9 +789,10 @@ public override void Dispose()
/// <summary>
/// Preprocess a stream message
/// </summary>
/// <param name="connection"></param>
/// <param name="type"></param>
/// <param name="data"></param>
/// <returns></returns>
public virtual ReadOnlyMemory<byte> PreprocessStreamMessage(WebSocketMessageType type, ReadOnlyMemory<byte> data) => data;
public virtual ReadOnlyMemory<byte> PreprocessStreamMessage(SocketConnection connection, WebSocketMessageType type, ReadOnlyMemory<byte> data) => data;
}
}
19 changes: 18 additions & 1 deletion CryptoExchange.Net/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public static Uri AddQueryParmeter(this Uri uri, string name, string value)
}

/// <summary>
/// Decompress using Gzip
/// Decompress using GzipStream
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
Expand All @@ -467,6 +467,23 @@ public static ReadOnlyMemory<byte> DecompressGzip(this ReadOnlyMemory<byte> data
deflateStream.CopyTo(decompressedStream);
return new ReadOnlyMemory<byte>(decompressedStream.GetBuffer(), 0, (int)decompressedStream.Length);
}

/// <summary>
/// Decompress using DeflateStream
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static ReadOnlyMemory<byte> Decompress(this ReadOnlyMemory<byte> input)
{
var output = new MemoryStream();

using (var compressStream = new MemoryStream(input.ToArray()))
using (var decompressor = new DeflateStream(compressStream, CompressionMode.Decompress))
decompressor.CopyTo(output);

output.Position = 0;
return new ReadOnlyMemory<byte>(output.GetBuffer(), 0, (int)output.Length);
}
}
}

2 changes: 1 addition & 1 deletion CryptoExchange.Net/Objects/ParameterCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public void AddEnum<T>(string key, T value)
public void AddEnumAsInt<T>(string key, T value)
{
var stringVal = EnumConverter.GetString(value);
Add(key, EnumConverter.GetString(int.Parse(stringVal))!);
Add(key, int.Parse(stringVal)!);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion CryptoExchange.Net/Sockets/SocketConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ protected virtual async Task HandleStreamMessage(WebSocketMessageType type, Read
string? originalData = null;

// 1. Decrypt/Preprocess if necessary
data = ApiClient.PreprocessStreamMessage(type, data);
data = ApiClient.PreprocessStreamMessage(this, type, data);

// 2. Read data into accessor
_accessor.Read(data);
Expand Down

0 comments on commit b309deb

Please sign in to comment.