Skip to content

fix: GetExchangeApi generic resolves T #871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ The following cryptocurrency services are supported:

Exchange constructors are private, to get access to an exchange in code use:

`ExchangeAPI.GetExchangeAPIAsync<>()`.
`ExchangeAPI.GetExchangeAPIAsync<T>()`.

### Installing the CLI

Expand Down
7 changes: 4 additions & 3 deletions src/ExchangeSharp/API/Exchanges/_Base/ExchangeAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -605,19 +605,20 @@ public static Task<IExchangeAPI> GetExchangeAPIAsync(string exchangeName)
/// <typeparam name="T">Type of exchange to get</typeparam>
/// <returns>Exchange API or null if not found</returns>
[Obsolete("Use the async version")]
public static IExchangeAPI GetExchangeAPI<T>()
public static T GetExchangeAPI<T>()
where T : ExchangeAPI
{
return GetExchangeAPIAsync<T>().Result;
}

public static Task<IExchangeAPI> GetExchangeAPIAsync<T>()
public static async Task<T> GetExchangeAPIAsync<T>()
where T : ExchangeAPI
{
// note: this method will be slightly slow (milliseconds) the first time it is called due to cache miss and initialization
// subsequent calls with cache hits will be nanoseconds
Type type = typeof(T)!;
return GetExchangeAPIAsync(type);

return (T)await GetExchangeAPIAsync(type);
}

/// <summary>
Expand Down
24 changes: 12 additions & 12 deletions tests/ExchangeSharpTests/ExchangeBinanceAPITests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ public void DeserializeDiff()
string toParse =
@"{
""e"": ""depthUpdate"",
""E"": 123456789,
""s"": ""BNBBTC"",
""U"": 157,
""u"": 160,
""b"": [
""E"": 123456789,
""s"": ""BNBBTC"",
""U"": 157,
""u"": 160,
""b"": [
[
""0.0024"",
""0.0024"",
""10"",
[]
[]
]
],
""a"": [
""a"": [
[
""0.0026"",
""100"",
[]
""0.0026"",
""100"",
[]
]
]
}";
Expand Down Expand Up @@ -116,7 +116,7 @@ public async Task CurrenciesParsedCorrectly()
requestMaker
.MakeRequestAsync(
"/capital/config/getall",
((ExchangeBinanceAPI)binance).BaseUrlSApi
binance.BaseUrlSApi
)
.Returns(
new IAPIRequestMaker.RequestResult<string>()
Expand Down
2 changes: 1 addition & 1 deletion tests/ExchangeSharpTests/ExchangeMEXCAPITests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace ExchangeSharpTests;
public class MEXCAPITests
{
private const string MarketSymbol = "ETHBTC";
private static IExchangeAPI _api;
private static ExchangeMEXCAPI _api;

[AssemblyInitialize]
public static async Task AssemblyInitialize(TestContext testContext)
Expand Down
Loading