Skip to content

Commit

Permalink
better align to other API + quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
cryi committed Jul 24, 2022
1 parent cc1c8f8 commit 52eae50
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
8 changes: 5 additions & 3 deletions Tzkt.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
using Tzkt.Api.Websocket.Hubs;
using Tzkt.Api.Websocket.Processors;
using Tzkt.Data;

using System.Text.Json.Serialization;

namespace Tzkt.Api
{
public class Startup
Expand Down Expand Up @@ -92,8 +93,8 @@ public void ConfigureServices(IServiceCollection services)
services.AddTransient<HeadProcessor<DefaultHub>>();
services.AddTransient<IHubProcessor, HeadProcessor<DefaultHub>>();

services.AddTransient<CycleProcessor<DefaultHub>>();
services.AddTransient<IHubProcessor, CycleProcessor<DefaultHub>>();
services.AddTransient<CyclesProcessor<DefaultHub>>();
services.AddTransient<IHubProcessor, CyclesProcessor<DefaultHub>>();

services.AddTransient<BlocksProcessor<DefaultHub>>();
services.AddTransient<IHubProcessor, BlocksProcessor<DefaultHub>>();
Expand Down Expand Up @@ -126,6 +127,7 @@ public void ConfigureServices(IServiceCollection services)
jsonOptions.PayloadSerializerOptions.Converters.Add(new DateTimeConverter());
jsonOptions.PayloadSerializerOptions.Converters.Add(new OperationConverter());
jsonOptions.PayloadSerializerOptions.Converters.Add(new OperationErrorConverter());
jsonOptions.PayloadSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
}
#endregion
Expand Down
14 changes: 8 additions & 6 deletions Tzkt.Api/Swagger/WsSubscriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,20 @@ Sends the blockchain head every time cycle is changed.

### Method

`SubscribeToCycle`
`SubscribeToCycles`

### Channel

`cycle`
`cycles`

### Parameters

````js
{
delayBlocks: 2, // number of blocks to delay cycle changed notification
// minimum 2, defaults to 2
delayBlocks: 2, // number of blocks to delay cycle changed notification
// minimum 2, defaults to 2
quote: "eur,usd" // comma-separated list of ticker symbols to inject historical prices into response
// defaults to "none"
}
````

Expand All @@ -63,8 +65,8 @@ State contains cycle (`int`) of the last processed cycle.
### Example

````js
connection.on("cycle", (msg) => { console.log(msg); });
await connection.invoke("SubscribeToCycle");
connection.on("cycles", (msg) => { console.log(msg); });
await connection.invoke("SubscribeToCycles");
````

---
Expand Down
6 changes: 3 additions & 3 deletions Tzkt.Api/Websocket/Hubs/DefaultHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Tzkt.Api.Websocket.Hubs
public class DefaultHub : BaseHub
{
readonly HeadProcessor<DefaultHub> Head;
readonly CycleProcessor<DefaultHub> Cycle;
readonly CyclesProcessor<DefaultHub> Cycle;
readonly BlocksProcessor<DefaultHub> Blocks;
readonly OperationsProcessor<DefaultHub> Operations;
readonly BigMapsProcessor<DefaultHub> BigMaps;
Expand All @@ -19,7 +19,7 @@ public class DefaultHub : BaseHub

public DefaultHub(
HeadProcessor<DefaultHub> head,
CycleProcessor<DefaultHub> cycle,
CyclesProcessor<DefaultHub> cycle,
BlocksProcessor<DefaultHub> blocks,
OperationsProcessor<DefaultHub> operations,
BigMapsProcessor<DefaultHub> bigMaps,
Expand All @@ -44,7 +44,7 @@ public Task<int> SubscribeToHead()
return Head.Subscribe(Clients.Caller, Context.ConnectionId);
}

public Task<int> SubscribeToCycle(CycleParameter parameters)
public Task<int> SubscribeToCycles(CyclesParameter parameters = null)
{
parameters ??= new();
parameters.EnsureValid();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json.Converters;
using System.Text.Json.Serialization;
using Tzkt.Api.Models;

namespace Tzkt.Api.Websocket
{
public class CycleParameter
public class CyclesParameter
{
public int DelayBlocks { get; set; } = 2; // 2 to cover possible reorganization
public Symbols Quote { get; set; } = Symbols.None;
Expand Down
10 changes: 5 additions & 5 deletions Tzkt.Api/Websocket/Processors/CyclesProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

namespace Tzkt.Api.Websocket.Processors
{
public class CycleProcessor<T> : IHubProcessor where T : Hub
public class CyclesProcessor<T> : IHubProcessor where T : Hub
{
#region static
const string CycleGroup = "cycle";
const string CycleChannel = "cycle";
const string CycleGroup = "cycles";
const string CycleChannel = "cycles";
static readonly SemaphoreSlim Sema = new(1, 1);
static readonly Dictionary<int, HashSet<string>> DelaySubs = new();
static readonly Dictionary<string, Symbols> QuoteSubs = new();
Expand All @@ -27,7 +27,7 @@ public class CycleProcessor<T> : IHubProcessor where T : Hub
readonly IHubContext<T> Context;
readonly ILogger Logger;

public CycleProcessor(StateCache cache, CyclesRepository repo, IHubContext<T> hubContext, ILogger<CycleProcessor<T>> logger)
public CyclesProcessor(StateCache cache, CyclesRepository repo, IHubContext<T> hubContext, ILogger<CyclesProcessor<T>> logger)
{
StateCache = cache;
CyclesRepo = repo;
Expand Down Expand Up @@ -79,7 +79,7 @@ public async Task OnStateChanged()
}
}

public async Task<int> Subscribe(IClientProxy client, string connectionId, CycleParameter parameter)
public async Task<int> Subscribe(IClientProxy client, string connectionId, CyclesParameter parameter)
{
Task sending = Task.CompletedTask;
try
Expand Down

0 comments on commit 52eae50

Please sign in to comment.