Skip to content

Commit

Permalink
Merge pull request #60 from JamiePrentice/feature/cryptobook_streaming
Browse files Browse the repository at this point in the history
Adding CryptoBook Streaming
  • Loading branch information
JamiePrentice authored Jun 2, 2020
2 parents fb6a904 + 245c6b9 commit 14617ff
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
9 changes: 6 additions & 3 deletions IEXSharp/Service/Cloud/CoreData/Crypto/CryptoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ public CryptoService(ExecutorREST executor, ExecutorSSE executorSSE)
public async Task<IEXResponse<CryptoBookResponse>> BookAsync(string symbol) =>
await executor.SymbolExecuteAsync<CryptoBookResponse>("crypto/[symbol]/book", symbol);

public SSEClient<CryptoBookResponse> SubscribeCryptoBook(IEnumerable<string> symbols) =>
executorSSE.SymbolsSubscribeSSE<CryptoBookResponse>("cryptoBook", symbols);

public SSEClient<EventCrypto> SubscribeCryptoEvents(IEnumerable<string> symbols) =>
executorSSE.SymbolsSubscribeSSE<EventCrypto>("cryptoEvents", symbols);

public async Task<IEXResponse<CryptoPriceResponse>> PriceAsync(string symbol) =>
await executor.SymbolExecuteAsync<CryptoPriceResponse>("crypto/[symbol]/price", symbol);

public async Task<IEXResponse<QuoteCryptoResponse>> QuoteAsync(string symbol) =>
await executor.SymbolExecuteAsync<QuoteCryptoResponse>("crypto/[symbol]/quote", symbol);

public SSEClient<EventCrypto> SubscribeCryptoEvents(IEnumerable<string> symbols) =>
executorSSE.SymbolsSubscribeSSE<EventCrypto>("cryptoEvents", symbols);

public SSEClient<QuoteCryptoResponse> SubscribeCryptoQuotes(IEnumerable<string> symbols) =>
executorSSE.SymbolsSubscribeSSE<QuoteCryptoResponse>("cryptoQuotes", symbols);
}
Expand Down
11 changes: 9 additions & 2 deletions IEXSharp/Service/Cloud/CoreData/Crypto/ICryptoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ public interface ICryptoService
/// <returns></returns>
Task<IEXResponse<CryptoBookResponse>> BookAsync(string symbol);

/// <summary>
/// <see cref="https://iexcloud.io/docs/api/#cryptocurrency-book" and cref="https://iexcloud.io/docs/api/#sse-streaming" />
/// </summary>
/// <param name="symbols"></param>
/// <returns></returns>
SSEClient<CryptoBookResponse> SubscribeCryptoBook(IEnumerable<string> symbols);

/// <summary>
/// <see cref="https://iexcloud.io/docs/api/#cryptocurrency-events"/>
/// Only accessible with SSE Streaming.
/// </summary>
/// <param name="symbol"></param>
/// <param name="symbols"></param>
/// <returns></returns>
SSEClient<EventCrypto> SubscribeCryptoEvents(IEnumerable<string> symbols);

Expand All @@ -39,7 +46,7 @@ public interface ICryptoService
/// <summary>
/// <see cref="https://iexcloud.io/docs/api/#cryptocurrency-quote"/>
/// </summary>
/// <param name="symbol"></param>
/// <param name="symbols"></param>
/// <returns></returns>
SSEClient<QuoteCryptoResponse> SubscribeCryptoQuotes(IEnumerable<string> symbols);
}
Expand Down
24 changes: 22 additions & 2 deletions IEXSharpTest/Cloud/CoreData/CryptoTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,33 @@ public async Task BookAsyncTest(string symbol)
Assert.IsNotNull(response.Data.bids.First().price);
}

[Test]
[TestCase(new object[] { "BTCUSD" })]
[TestCase(new object[] { "BTCUSD", "ETHUSD" })]
public async Task SubscribeCryptoBookTest(object[] symbols)
{
using var sseClient = sandBoxClient.Crypto.SubscribeCryptoBook(symbols.Cast<string>());
sseClient.Error += (s, e) =>
{
sseClient.Close();
Assert.Fail("EventSource Error Occurred. Details: {0}", e.Exception.Message);
};
sseClient.MessageReceived += (s, m) =>
{
sseClient.Close();
Assert.IsNotNull(m.First().asks);
Assert.IsNotNull(m.First().bids);
Assert.Pass(m.ToString());
};
await sseClient.StartAsync();
}

[Test]
[TestCase(new object[] { "btcusdt" })]
[TestCase(new object[] { "btcusdt", "ethusdt" })]
public async Task CryptoEventSSETest(object[] symbols)
{
using var sseClient = sandBoxClient.Crypto.SubscribeCryptoEvents(
symbols.Cast<string>());
using var sseClient = sandBoxClient.Crypto.SubscribeCryptoEvents(symbols.Cast<string>());
sseClient.Error += (s, e) =>
{
sseClient.Close();
Expand Down

0 comments on commit 14617ff

Please sign in to comment.