Skip to content

Commit

Permalink
Feature/system.text.json (#201)
Browse files Browse the repository at this point in the history
CryptoExchange.Net V7.1.0
  • Loading branch information
JKorf authored Mar 16, 2024
1 parent 65ca17f commit 8a64e25
Show file tree
Hide file tree
Showing 33 changed files with 193 additions and 111 deletions.
1 change: 1 addition & 0 deletions ByBit.Net/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Bybit.UnitTests")]
4 changes: 2 additions & 2 deletions ByBit.Net/Bybit.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@
<None Include="..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="ConfigureAwaitChecker.Analyzer" Version="5.0.0">
<PackageReference Include="ConfigureAwaitChecker.Analyzer" Version="5.0.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="CryptoExchange.Net" Version="7.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="CryptoExchange.Net" Version="7.1.0" />
</ItemGroup>
</Project>
6 changes: 3 additions & 3 deletions ByBit.Net/BybitAuthenticationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
using System.Collections.Generic;
using System.Net.Http;
using Bybit.Net.Clients.CopyTradingApi;
using CryptoExchange.Net.Converters;
using System.Globalization;
using Bybit.Net.Clients.V5;
using System.Text;
using Newtonsoft.Json;
using Bybit.Net.Objects.Options;
using CryptoExchange.Net.Clients;

namespace Bybit.Net
{
Expand All @@ -22,7 +22,7 @@ public BybitAuthenticationProvider(ApiCredentials credentials) : base(credential
{
}

public override void AuthenticateRequest(RestApiClient apiClient, Uri uri, HttpMethod method, Dictionary<string, object> providedParameters, bool auth, ArrayParametersSerialization arraySerialization, HttpMethodParameterPosition parameterPosition, out SortedDictionary<string, object> uriParameters, out SortedDictionary<string, object> bodyParameters, out Dictionary<string, string> headers)
public override void AuthenticateRequest(RestApiClient apiClient, Uri uri, HttpMethod method, Dictionary<string, object> providedParameters, bool auth, ArrayParametersSerialization arraySerialization, HttpMethodParameterPosition parameterPosition, RequestBodyFormat bodyFormat, out SortedDictionary<string, object> uriParameters, out SortedDictionary<string, object> bodyParameters, out Dictionary<string, string> headers)
{
uriParameters = parameterPosition == HttpMethodParameterPosition.InUri ? new SortedDictionary<string, object>(providedParameters, new BybitComparer()) : new SortedDictionary<string, object>();
bodyParameters = parameterPosition == HttpMethodParameterPosition.InBody ? new SortedDictionary<string, object>(providedParameters, new BybitComparer()) : new SortedDictionary<string, object>();
Expand All @@ -35,7 +35,7 @@ public override void AuthenticateRequest(RestApiClient apiClient, Uri uri, HttpM
var timestamp = DateTimeConverter.ConvertToMilliseconds(GetTimestamp(apiClient).AddMilliseconds(-1000)).Value.ToString(CultureInfo.InvariantCulture);
if (apiClient is BybitRestClientCopyTradingApi || apiClient is BybitRestClientApi)
{
var signPayload = parameterPosition == HttpMethodParameterPosition.InUri ? uri.SetParameters(parameters, arraySerialization).Query.Replace("?", "") : apiClient.requestBodyFormat == RequestBodyFormat.FormData ? parameters.ToFormData() : JsonConvert.SerializeObject(parameters);
var signPayload = parameterPosition == HttpMethodParameterPosition.InUri ? uri.SetParameters(parameters, arraySerialization).Query.Replace("?", "") : bodyFormat == RequestBodyFormat.FormData ? parameters.ToFormData() : JsonConvert.SerializeObject(parameters);
var key = _credentials.Key!.GetString();
var recvWindow = ((BybitRestOptions)apiClient.ClientOptions).ReceiveWindow.TotalMilliseconds;
var payload = timestamp + key + recvWindow + signPayload;
Expand Down
2 changes: 1 addition & 1 deletion ByBit.Net/Clients/BybitRestClient.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Bybit.Net.Clients.CopyTradingApi;
using Bybit.Net.Interfaces.Clients;
using Bybit.Net.Interfaces.Clients.CopyTradingApi;
using CryptoExchange.Net;
using Bybit.Net.Clients.SpotApi.v3;
using Bybit.Net.Interfaces.Clients.SpotApi.v3;
using Bybit.Net.Interfaces.Clients.DerivativesApi;
Expand All @@ -11,6 +10,7 @@
using Bybit.Net.Objects.Options;
using System;
using Microsoft.Extensions.Logging;
using CryptoExchange.Net.Clients;

namespace Bybit.Net.Clients
{
Expand Down
4 changes: 2 additions & 2 deletions ByBit.Net/Clients/BybitSocketClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CryptoExchange.Net;
using System;
using System;
using Bybit.Net.Interfaces.Clients;
using Bybit.Net.Interfaces.Clients.SpotApi.v3;
using Bybit.Net.Interfaces.Clients.DerivativesApi.UnifiedMarginApi;
Expand All @@ -14,6 +13,7 @@
using Bybit.Net.Clients.DerivativesApi.ContractApi;
using Bybit.Net.Clients.DerivativesApi.UnifiedMarginApi;
using Bybit.Net.Clients.SpotApi.v3;
using CryptoExchange.Net.Clients;

namespace Bybit.Net.Clients
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Bybit.Net.Objects.Options;
using CryptoExchange.Net;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Clients;
using CryptoExchange.Net.Objects;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
Expand Down Expand Up @@ -51,7 +52,7 @@ internal BybitRestClientCopyTradingApi(ILogger logger, HttpClient? httpClient, B
ExchangeData = new BybitRestClientCopyTradingApiExchangeData(this);
Trading = new BybitRestClientCopyTradingApiTrading(this);

requestBodyFormat = RequestBodyFormat.FormData;
RequestBodyFormat = RequestBodyFormat.FormData;
ParameterPositions[HttpMethod.Delete] = HttpMethodParameterPosition.InUri;
}
#endregion
Expand All @@ -75,10 +76,9 @@ internal async Task<WebCallResult<BybitResult<T>>> SendRequestWrapperAsync<T>(
HttpMethod method,
CancellationToken cancellationToken,
Dictionary<string, object>? parameters = null,
bool signed = false,
JsonSerializer? deserializer = null) where T : class
bool signed = false) where T : class
{
var result = await base.SendRequestAsync<BybitResult<T>>(uri, method, cancellationToken, parameters, signed, deserializer: deserializer).ConfigureAwait(false);
var result = await base.SendRequestAsync<BybitResult<T>>(uri, method, cancellationToken, parameters, signed).ConfigureAwait(false);
if (!result)
return result.As<BybitResult<T>>(default);

Expand All @@ -94,10 +94,9 @@ internal async Task<WebCallResult<IEnumerable<T>>> SendRequestListAsync<T>(
CancellationToken cancellationToken,
Dictionary<string, object>? parameters = null,
bool signed = false,
JsonSerializer? deserializer = null,
bool ignoreRatelimit = false)
{
var result = await base.SendRequestAsync<BybitCopyTradingResult<BybitList<T>>>(uri, method, cancellationToken, parameters, signed, deserializer: deserializer, ignoreRatelimit: ignoreRatelimit).ConfigureAwait(false);
var result = await base.SendRequestAsync<BybitCopyTradingResult<BybitList<T>>>(uri, method, cancellationToken, parameters, signed, ignoreRatelimit: ignoreRatelimit).ConfigureAwait(false);
if (!result)
return result.As<IEnumerable<T>>(default);

Expand All @@ -113,10 +112,9 @@ internal async Task<WebCallResult<T>> SendRequestAsync<T>(
CancellationToken cancellationToken,
Dictionary<string, object>? parameters = null,
bool signed = false,
JsonSerializer? deserializer = null,
bool ignoreRatelimit = false)
{
var result = await base.SendRequestAsync<BybitCopyTradingResult<T>>(uri, method, cancellationToken, parameters, signed, deserializer: deserializer, ignoreRatelimit: ignoreRatelimit).ConfigureAwait(false);
var result = await base.SendRequestAsync<BybitCopyTradingResult<T>>(uri, method, cancellationToken, parameters, signed, ignoreRatelimit: ignoreRatelimit).ConfigureAwait(false);
if (!result)
return result.As<T>(default);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Bybit.Net.Objects.Options;
using CryptoExchange.Net;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Clients;
using CryptoExchange.Net.Objects;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
Expand Down Expand Up @@ -58,7 +59,7 @@ internal BybitRestClientDerivativesApi(ILogger logger, HttpClient? httpClient, B
ContractApi = new BybitRestClientContractApi(this);
UnifiedMarginApi = new BybitRestClientUnifiedMarginApi(this);

requestBodyFormat = RequestBodyFormat.FormData;
RequestBodyFormat = RequestBodyFormat.FormData;
ParameterPositions[HttpMethod.Delete] = HttpMethodParameterPosition.InUri;
}
#endregion
Expand All @@ -83,10 +84,9 @@ internal async Task<WebCallResult<BybitResult<T>>> SendRequestWrapperAsync<T>(
CancellationToken cancellationToken,
Dictionary<string, object>? parameters = null,
bool signed = false,
JsonSerializer? deserializer = null,
bool ignoreRatelimit = false) where T : class
{
var result = await base.SendRequestAsync<BybitResult<T>>(uri, method, cancellationToken, parameters, signed, deserializer: deserializer, ignoreRatelimit: ignoreRatelimit).ConfigureAwait(false);
var result = await base.SendRequestAsync<BybitResult<T>>(uri, method, cancellationToken, parameters, signed, ignoreRatelimit: ignoreRatelimit).ConfigureAwait(false);
if (!result)
return result.As<BybitResult<T>>(default);

Expand All @@ -102,10 +102,9 @@ internal async Task<WebCallResult<IEnumerable<T>>> SendRequestListAsync<T>(
CancellationToken cancellationToken,
Dictionary<string, object>? parameters = null,
bool signed = false,
JsonSerializer? deserializer = null,
bool ignoreRatelimit = false)
{
var result = await base.SendRequestAsync<BybitCopyTradingResult<BybitList<T>>>(uri, method, cancellationToken, parameters, signed, deserializer: deserializer, ignoreRatelimit: ignoreRatelimit).ConfigureAwait(false);
var result = await base.SendRequestAsync<BybitCopyTradingResult<BybitList<T>>>(uri, method, cancellationToken, parameters, signed, ignoreRatelimit: ignoreRatelimit).ConfigureAwait(false);
if (!result)
return result.As<IEnumerable<T>>(default);

Expand All @@ -121,10 +120,9 @@ internal async Task<WebCallResult<T>> SendRequestAsync<T>(
CancellationToken cancellationToken,
Dictionary<string, object>? parameters = null,
bool signed = false,
JsonSerializer? deserializer = null,
bool ignoreRatelimit = false)
{
var result = await base.SendRequestAsync<BybitCopyTradingResult<T>>(uri, method, cancellationToken, parameters, signed, deserializer: deserializer, ignoreRatelimit: ignoreRatelimit).ConfigureAwait(false);
var result = await base.SendRequestAsync<BybitCopyTradingResult<T>>(uri, method, cancellationToken, parameters, signed, ignoreRatelimit: ignoreRatelimit).ConfigureAwait(false);
if (!result)
return result.As<T>(default);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
using Bybit.Net.Objects.Sockets.Subscriptions;
using CryptoExchange.Net;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Clients;
using CryptoExchange.Net.Converters;
using CryptoExchange.Net.Converters.MessageParsing;
using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.Objects;
using CryptoExchange.Net.Objects.Sockets;
using CryptoExchange.Net.Sockets;
using CryptoExchange.Net.Sockets.MessageParsing;
using CryptoExchange.Net.Sockets.MessageParsing.Interfaces;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
using Bybit.Net.Objects.Sockets.Subscriptions;
using CryptoExchange.Net;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Clients;
using CryptoExchange.Net.Converters;
using CryptoExchange.Net.Converters.MessageParsing;
using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.Objects;
using CryptoExchange.Net.Objects.Sockets;
using CryptoExchange.Net.Sockets;
using CryptoExchange.Net.Sockets.MessageParsing;
using CryptoExchange.Net.Sockets.MessageParsing.Interfaces;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using System;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
using Bybit.Net.Objects.Sockets.Subscriptions;
using CryptoExchange.Net;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Clients;
using CryptoExchange.Net.Converters;
using CryptoExchange.Net.Converters.MessageParsing;
using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.Objects;
using CryptoExchange.Net.Objects.Sockets;
using CryptoExchange.Net.Sockets;
using CryptoExchange.Net.Sockets.MessageParsing;
using CryptoExchange.Net.Sockets.MessageParsing.Interfaces;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using System;
Expand Down
21 changes: 3 additions & 18 deletions ByBit.Net/Clients/SpotApi/BybitRestClientBaseSpotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Bybit.Net.Objects.Options;
using CryptoExchange.Net;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Clients;
using CryptoExchange.Net.CommonObjects;
using CryptoExchange.Net.Interfaces.CommonClients;
using CryptoExchange.Net.Objects;
Expand Down Expand Up @@ -49,9 +50,7 @@ public abstract class BybitRestClientBaseSpotApi : RestApiClient, ISpotClient
internal BybitRestClientBaseSpotApi(ILogger logger, HttpClient? httpClient, string baseAddress, BybitRestOptions options, RestApiOptions apiOptions)
: base(logger, httpClient, baseAddress, options, apiOptions)
{
manualParseError = true;

requestBodyFormat = RequestBodyFormat.FormData;
RequestBodyFormat = RequestBodyFormat.FormData;
ParameterPositions[HttpMethod.Delete] = HttpMethodParameterPosition.InUri;
}
#endregion
Expand All @@ -70,29 +69,15 @@ internal Uri GetUrl(string endpoint)
return new Uri(BaseAddress.AppendPath(endpoint));
}

/// <inheritdoc />
protected override Task<ServerError?> TryParseErrorAsync(JToken data)
{
var responseCode = data["ret_code"];
if (responseCode != null && responseCode.ToString() != "0")
{
var errorMessage = data["ret_msg"];
return Task.FromResult(new ServerError(responseCode.Value<int>(), errorMessage!.ToString()))!;
}

return Task.FromResult<ServerError?>(null);
}

internal async Task<WebCallResult<T>> SendRequestAsync<T>(
Uri uri,
HttpMethod method,
CancellationToken cancellationToken,
Dictionary<string, object>? parameters = null,
bool signed = false,
JsonSerializer? deserializer = null,
bool ignoreRatelimit = false)
{
var result = await base.SendRequestAsync<BybitResult<T>>(uri, method, cancellationToken, parameters, signed, deserializer: deserializer, ignoreRatelimit: ignoreRatelimit).ConfigureAwait(false);
var result = await base.SendRequestAsync<BybitResult<T>>(uri, method, cancellationToken, parameters, signed, ignoreRatelimit: ignoreRatelimit).ConfigureAwait(false);
if (!result)
return result.As<T>(default);

Expand Down
5 changes: 3 additions & 2 deletions ByBit.Net/Clients/SpotApi/v3/BybitSocketClientSpotApiV3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
using CryptoExchange.Net.Objects.Sockets;
using CryptoExchange.Net.Authentication;
using Bybit.Net.Objects.Sockets.Queries;
using CryptoExchange.Net.Sockets.MessageParsing.Interfaces;
using CryptoExchange.Net.Sockets.MessageParsing;
using Bybit.Net.Objects.Sockets.Subscriptions;
using CryptoExchange.Net.Sockets;
using CryptoExchange.Net.Converters;
using CryptoExchange.Net.Interfaces;
using CryptoExchange.Net.Converters.MessageParsing;
using CryptoExchange.Net.Clients;

namespace Bybit.Net.Clients.SpotApi.v3
{
Expand Down
39 changes: 27 additions & 12 deletions ByBit.Net/Clients/V5/BybitRestClientApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
using CryptoExchange.Net.Interfaces.CommonClients;
using CryptoExchange.Net.CommonObjects;
using System.Linq;
using CryptoExchange.Net.Clients;
using CryptoExchange.Net.Converters.MessageParsing;
using CryptoExchange.Net.Interfaces;

namespace Bybit.Net.Clients.V5
{
Expand Down Expand Up @@ -58,14 +61,12 @@ internal BybitRestClientApi(ILogger logger, HttpClient? httpClient, BybitRestOpt
{ "Referer", !string.IsNullOrEmpty(options.Referer) ? options.Referer! : _referer }
};

manualParseError = true;

Account = new BybitRestClientApiAccount(this);
ExchangeData = new BybitRestClientApiExchangeData(this);
Trading = new BybitRestClientApiTrading(this);
SubAccount = new BybitRestClientApiSubAccounts(this);

requestBodyFormat = RequestBodyFormat.Json;
RequestBodyFormat = RequestBodyFormat.Json;
ParameterPositions[HttpMethod.Delete] = HttpMethodParameterPosition.InUri;
}
#endregion
Expand Down Expand Up @@ -107,21 +108,19 @@ internal async Task<WebCallResult<BybitExtResult<T, U>>> SendRequestFullResponse
HttpMethod method,
CancellationToken cancellationToken,
Dictionary<string, object>? parameters = null,
bool signed = false,
JsonSerializer? deserializer = null)
bool signed = false)
{
return await base.SendRequestAsync<BybitExtResult<T, U>>(uri, method, cancellationToken, parameters, signed, deserializer: deserializer).ConfigureAwait(false);
return await base.SendRequestAsync<BybitExtResult<T, U>>(uri, method, cancellationToken, parameters, signed).ConfigureAwait(false);
}

internal async Task<WebCallResult<T>> SendRequestAsync<T>(
Uri uri,
HttpMethod method,
CancellationToken cancellationToken,
Dictionary<string, object>? parameters = null,
bool signed = false,
JsonSerializer? deserializer = null)
bool signed = false)
{
var result = await base.SendRequestAsync<BybitResult<T>>(uri, method, cancellationToken, parameters, signed, deserializer: deserializer).ConfigureAwait(false);
var result = await base.SendRequestAsync<BybitResult<T>>(uri, method, cancellationToken, parameters, signed).ConfigureAwait(false);
if (!result)
return result.As<T>(default);

Expand All @@ -136,10 +135,9 @@ internal async Task<WebCallResult> SendRequestAsync(
HttpMethod method,
CancellationToken cancellationToken,
Dictionary<string, object>? parameters = null,
bool signed = false,
JsonSerializer? deserializer = null)
bool signed = false)
{
var result = await base.SendRequestAsync<BybitResult<object>>(uri, method, cancellationToken, parameters, signed, deserializer: deserializer).ConfigureAwait(false);
var result = await base.SendRequestAsync<BybitResult<object>>(uri, method, cancellationToken, parameters, signed).ConfigureAwait(false);
if (!result)
return result.AsDataless();

Expand All @@ -149,6 +147,23 @@ internal async Task<WebCallResult> SendRequestAsync(
return result.AsDataless();
}

/// <inheritdoc />
protected override Error ParseErrorResponse(int httpStatusCode, IEnumerable<KeyValuePair<string, IEnumerable<string>>> responseHeaders, IMessageAccessor accessor)
{
if (!accessor.IsJson)
return new ServerError(accessor.GetOriginalString());

var code = accessor.GetValue<int?>(MessagePath.Get().Property("retCode"));
var msg = accessor.GetValue<string>(MessagePath.Get().Property("retMsg"));
if (msg == null)
return new ServerError(accessor.GetOriginalString());

if (code == null)
return new ServerError(msg);

return new ServerError(code.Value, msg);
}

internal void InvokeOrderPlaced(OrderId id)
{
OnOrderPlaced?.Invoke(id);
Expand Down
Loading

0 comments on commit 8a64e25

Please sign in to comment.