From e024df77b85efbb380c5569ab3fc8dc825f9c34e Mon Sep 17 00:00:00 2001 From: JKorf Date: Wed, 27 Mar 2024 16:50:44 +0100 Subject: [PATCH] Examples --- Examples/Examples.sln | 31 +++++++++++++ .../Huobi.Examples.Api.csproj | 16 +++++++ Examples/Huobi.Examples.Api/Program.cs | 46 +++++++++++++++++++ .../appsettings.Development.json | 8 ++++ Examples/Huobi.Examples.Api/appsettings.json | 9 ++++ .../Huobi.Examples.Console.csproj | 14 ++++++ Examples/Huobi.Examples.Console/Program.cs | 25 ++++++++++ Examples/README.md | 7 +++ 8 files changed, 156 insertions(+) create mode 100644 Examples/Examples.sln create mode 100644 Examples/Huobi.Examples.Api/Huobi.Examples.Api.csproj create mode 100644 Examples/Huobi.Examples.Api/Program.cs create mode 100644 Examples/Huobi.Examples.Api/appsettings.Development.json create mode 100644 Examples/Huobi.Examples.Api/appsettings.json create mode 100644 Examples/Huobi.Examples.Console/Huobi.Examples.Console.csproj create mode 100644 Examples/Huobi.Examples.Console/Program.cs create mode 100644 Examples/README.md diff --git a/Examples/Examples.sln b/Examples/Examples.sln new file mode 100644 index 00000000..bbd734ff --- /dev/null +++ b/Examples/Examples.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34330.188 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Huobi.Examples.Api", "Huobi.Examples.Api\Huobi.Examples.Api.csproj", "{ECA139DF-CA98-4E98-89DA-C594FEBC0865}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Huobi.Examples.Console", "Huobi.Examples.Console\Huobi.Examples.Console.csproj", "{FD4F95C8-D9B7-4F81-9245-4CE667DFD421}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {ECA139DF-CA98-4E98-89DA-C594FEBC0865}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {ECA139DF-CA98-4E98-89DA-C594FEBC0865}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ECA139DF-CA98-4E98-89DA-C594FEBC0865}.Release|Any CPU.ActiveCfg = Release|Any CPU + {ECA139DF-CA98-4E98-89DA-C594FEBC0865}.Release|Any CPU.Build.0 = Release|Any CPU + {FD4F95C8-D9B7-4F81-9245-4CE667DFD421}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FD4F95C8-D9B7-4F81-9245-4CE667DFD421}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FD4F95C8-D9B7-4F81-9245-4CE667DFD421}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FD4F95C8-D9B7-4F81-9245-4CE667DFD421}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {84A4E6CE-9D3A-43FF-97B1-D91CE93B7E8F} + EndGlobalSection +EndGlobal diff --git a/Examples/Huobi.Examples.Api/Huobi.Examples.Api.csproj b/Examples/Huobi.Examples.Api/Huobi.Examples.Api.csproj new file mode 100644 index 00000000..262bba0f --- /dev/null +++ b/Examples/Huobi.Examples.Api/Huobi.Examples.Api.csproj @@ -0,0 +1,16 @@ + + + + net7.0 + enable + enable + true + + + + + + + + + diff --git a/Examples/Huobi.Examples.Api/Program.cs b/Examples/Huobi.Examples.Api/Program.cs new file mode 100644 index 00000000..02995e59 --- /dev/null +++ b/Examples/Huobi.Examples.Api/Program.cs @@ -0,0 +1,46 @@ +using Huobi.Net.Interfaces.Clients; +using CryptoExchange.Net.Authentication; +using Microsoft.AspNetCore.Mvc; + +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +// Add the Bitget services +builder.Services.AddHuobi(); + +// OR to provide API credentials for accessing private endpoints, or setting other options: +/* +builder.Services.AddHuobi(restOptions => +{ + restOptions.ApiCredentials = new ApiCredentials("", ""); + restOptions.RequestTimeout = TimeSpan.FromSeconds(5); +}, socketOptions => +{ + socketOptions.ApiCredentials = new ApiCredentials("", ""); +}); +*/ + +var app = builder.Build(); +app.UseSwagger(); +app.UseSwaggerUI(); +app.UseHttpsRedirection(); + +// Map the endpoints and inject the Huobi rest client +app.MapGet("/{Symbol}", async ([FromServices] IHuobiRestClient client, string symbol) => +{ + var result = await client.SpotApi.ExchangeData.GetTickerAsync(symbol); + return (object)(result.Success ? result.Data : result.Error!); +}) +.WithOpenApi(); + +app.MapGet("/Balances", async ([FromServices] IHuobiRestClient client) => +{ + var account = await client.SpotApi.Account.GetAccountsAsync(); + var result = await client.SpotApi.Account.GetBalancesAsync(account.Data.Single(d => d.Type == Huobi.Net.Enums.AccountType.Spot).Id); + return (object)(result.Success ? result.Data : result.Error!); +}) +.WithOpenApi(); + +app.Run(); \ No newline at end of file diff --git a/Examples/Huobi.Examples.Api/appsettings.Development.json b/Examples/Huobi.Examples.Api/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/Examples/Huobi.Examples.Api/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Examples/Huobi.Examples.Api/appsettings.json b/Examples/Huobi.Examples.Api/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/Examples/Huobi.Examples.Api/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/Examples/Huobi.Examples.Console/Huobi.Examples.Console.csproj b/Examples/Huobi.Examples.Console/Huobi.Examples.Console.csproj new file mode 100644 index 00000000..ed7cc56f --- /dev/null +++ b/Examples/Huobi.Examples.Console/Huobi.Examples.Console.csproj @@ -0,0 +1,14 @@ + + + + Exe + net7.0 + enable + enable + + + + + + + diff --git a/Examples/Huobi.Examples.Console/Program.cs b/Examples/Huobi.Examples.Console/Program.cs new file mode 100644 index 00000000..f3b950b9 --- /dev/null +++ b/Examples/Huobi.Examples.Console/Program.cs @@ -0,0 +1,25 @@ +using Huobi.Net.Clients; +using CryptoExchange.Net.Objects; +using Microsoft.Extensions.Logging; + +// REST +var restClient = new HuobiRestClient(); +var ticker = await restClient.SpotApi.ExchangeData.GetTickerAsync("ETHUSDT"); +Console.WriteLine($"Rest client ticker price for ETH-USDT: {ticker.Data.ClosePrice}"); + +Console.WriteLine(); +Console.WriteLine("Press enter to start websocket subscription"); +Console.ReadLine(); + +// Websocket +// Optional, manually add logging +var logFactory = new LoggerFactory(); +logFactory.AddProvider(new TraceLoggerProvider()); + +var socketClient = new HuobiSocketClient(logFactory); +var subscription = await socketClient.SpotApi.SubscribeToTickerUpdatesAsync("ethusdt", update => +{ + Console.WriteLine($"Websocket client ticker price for ETHUSDT: {update.Data.ClosePrice}"); +}); + +Console.ReadLine(); diff --git a/Examples/README.md b/Examples/README.md new file mode 100644 index 00000000..b156afcb --- /dev/null +++ b/Examples/README.md @@ -0,0 +1,7 @@ +# Examples + +### Huobi.Examples.Api +A minimal API showing how to integrate Huobi.Net in a web API project + +### Huobi.Examples.Console +A simple console client demonstrating basic usage \ No newline at end of file