Skip to content

Commit

Permalink
WIP - Totally refactored (incompatible) API
Browse files Browse the repository at this point in the history
  • Loading branch information
monoman committed Apr 17, 2024
1 parent 2d4a2ac commit 0c69a09
Show file tree
Hide file tree
Showing 71 changed files with 799 additions and 1,035 deletions.
25 changes: 12 additions & 13 deletions InterlockLedger.Rest.Client/Abstractions/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,17 @@
//
// ******************************************************************************************************************************

namespace InterlockLedger.Rest.Client
namespace InterlockLedger.Rest.Client;

internal static class Globals
{
internal static class Globals
{
public static readonly JsonSerializerOptions JsonSettings = new() {
AllowTrailingCommas = true,
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
ReadCommentHandling = JsonCommentHandling.Skip,
WriteIndented = true,
PreferredObjectCreationHandling = JsonObjectCreationHandling.Replace,
};
}
public static readonly JsonSerializerOptions JsonSettings = new() {
AllowTrailingCommas = true,
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
ReadCommentHandling = JsonCommentHandling.Skip,
WriteIndented = true,
PreferredObjectCreationHandling = JsonObjectCreationHandling.Replace,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@

namespace InterlockLedger.Rest.Client.Abstractions;

internal sealed class InterlockingsImplementation : IRestInterlockings
internal sealed class InterlockingsImplementation<T> : IInterlockings where T: IRestChain
{
public InterlockingsImplementation(RestAbstractChain parent) {
public InterlockingsImplementation(RestAbstractChain<T> parent) {
_parent = parent.Required();
_rest = _parent._rest;
_node = _parent._node;
_id = _parent.Id;
}

public Task<InterlockingRecordModel> ForceInterlockAsync(ForceInterlockModel model)
=> _rest.PostAsync<InterlockingRecordModel>($"/chain/{_id}/interlockings", model);
public Task<InterlockingRecordModel?> ForceInterlockAsync(ForceInterlockModel model)
=> _node.PostAsync<InterlockingRecordModel>($"/chain/{_id}/interlockings", model);

public Task<PageOf<InterlockingRecordModel>> GetInterlocksAsync(ushort page = 0, byte pageSize = 10)
=> _rest.GetAsync<PageOf<InterlockingRecordModel>>($"/chain/{_id}/interlockings?page={page}&pageSize={pageSize}");
public Task<PageOf<InterlockingRecordModel>?> GetInterlocksAsync(ushort page = 0, byte pageSize = 10)
=> _node.GetAsync<PageOf<InterlockingRecordModel>>($"/chain/{_id}/interlockings?page={page}&pageSize={pageSize}");

private readonly string _id;
private readonly RestAbstractChain _parent;
private readonly IRestNodeInternals _rest;
private readonly RestAbstractChain<T> _parent;
private readonly RestAbstractNode<T> _node;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,30 @@

namespace InterlockLedger.Rest.Client.Abstractions;

internal sealed class RecordsAsJsonImplementation : IRestRecordsAsJson
internal sealed class RecordsAsJsonStoreImplementation<T> : IRecordsAsJsonStore where T : IRestChain
{
public RecordsAsJsonImplementation(RestAbstractChain parent) {
public RecordsAsJsonStoreImplementation(RestAbstractChain<T> parent) {
_parent = parent.Required();
_rest = _parent._rest;
_node = _parent._node;
_id = _parent.Id;
}

public Task<RecordModelAsJson> AddAsync(NewRecordModelAsJson model)
public Task<RecordModelAsJson?> AddAsync(NewRecordModelAsJson model)
=> AddAsync(model.ApplicationId, model.PayloadTagId, model.Type, model.Json);

public Task<RecordModelAsJson> AddAsync(ulong applicationId, ulong payloadTagId, object payload)
public Task<RecordModelAsJson?> AddAsync(ulong applicationId, ulong payloadTagId, object? payload)
=> AddAsync(applicationId, payloadTagId, RecordType.Data, payload);

public Task<RecordModelAsJson> AddAsync(ulong applicationId, ulong payloadTagId, RecordType type, object payload)
=> _rest.PostAsync<RecordModelAsJson>($"records@{_id}/asJson?applicationId={applicationId}&payloadTagId={payloadTagId}&type={type}", payload);
public Task<RecordModelAsJson?> AddAsync(ulong applicationId, ulong payloadTagId, RecordType type, object? payload)
=> _node.PostAsync<RecordModelAsJson>($"records@{_id}/asJson?applicationId={applicationId}&payloadTagId={payloadTagId}&type={type}", payload);

public Task<PageOf<RecordModelAsJson>> FromAsync(ulong firstSerial, ushort page = 0, byte pageSize = 10, bool lastToFirst = false, bool ommitPayload = false)
=> _rest.GetAsync<PageOf<RecordModelAsJson>>($"records@{_id}/asJson?firstSerial={firstSerial}&page={page}&pageSize={pageSize}&lastToFirst={lastToFirst}&ommitPayload={ommitPayload}");
public Task<PageOf<RecordModelAsJson>?> FromAsync(ulong firstSerial, ushort page = 0, byte pageSize = 10, bool lastToFirst = false, bool ommitPayload = false)
=> _node.GetAsync<PageOf<RecordModelAsJson>>($"records@{_id}/asJson?firstSerial={firstSerial}&page={page}&pageSize={pageSize}&lastToFirst={lastToFirst}&ommitPayload={ommitPayload}");

public Task<PageOf<RecordModelAsJson>> FromToAsync(ulong firstSerial, ulong lastSerial, ushort page = 0, byte pageSize = 10, bool lastToFirst = false, bool ommitPayload = false)
=> _rest.GetAsync<PageOf<RecordModelAsJson>>($"records@{_id}/asJson?firstSerial={firstSerial}&lastSerial={lastSerial}&page={page}&pageSize={pageSize}&lastToFirst={lastToFirst}&ommitPayload={ommitPayload}");
public Task<PageOf<RecordModelAsJson>?> FromToAsync(ulong firstSerial, ulong lastSerial, ushort page = 0, byte pageSize = 10, bool lastToFirst = false, bool ommitPayload = false)
=> _node.GetAsync<PageOf<RecordModelAsJson>>($"records@{_id}/asJson?firstSerial={firstSerial}&lastSerial={lastSerial}&page={page}&pageSize={pageSize}&lastToFirst={lastToFirst}&ommitPayload={ommitPayload}");

private readonly string _id;
private readonly RestAbstractChain _parent;
private readonly IRestNodeInternals _rest;
private readonly RestAbstractChain<T> _parent;
private readonly RestAbstractNode<T> _node;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,24 @@

namespace InterlockLedger.Rest.Client.Abstractions;

internal sealed class RecordsImplementation : IRestRecords
internal sealed class RecordsStoreImplementation<T> : IRecordsStore where T: IRestChain
{
public RecordsImplementation(RestAbstractChain parent) {
public RecordsStoreImplementation(RestAbstractChain<T> parent) {
_parent = parent.Required();
_rest = _parent._rest;
_node = _parent._node;
_id = _parent.Id;
}

public Task<RecordModel> AddRecordAsync(NewRecordModel model)
=> _rest.PostAsync<RecordModel>($"records@{_id}", model);
public Task<RecordModel?> AddRecordAsync(NewRecordModel model)
=> _node.PostAsync<RecordModel>($"records@{_id}", model);

public Task<RecordModel> AddRecordAsync(ulong applicationId, ulong payloadTagId, byte[] bytes)
=> AddRecordAsync(applicationId, payloadTagId, RecordType.Data, bytes);
public Task<PageOf<RecordModel>?> RecordsFromAsync(ulong firstSerial, ushort page = 0, byte pageSize = 10, bool lastToFirst = false, bool ommitPayload = false)
=> _node.GetAsync<PageOf<RecordModel>>($"records@{_id}?firstSerial={firstSerial}&page={page}&pageSize={pageSize}&lastToFirst={lastToFirst}&ommitPayload={ommitPayload}");

public Task<RecordModel> AddRecordAsync(ulong applicationId, ulong payloadTagId, RecordType type, byte[] bytes)
=> _rest.PostRawAsync<RecordModel>($"records@{_id}/with?applicationId={applicationId}&payloadTagId={payloadTagId}&type={type}", bytes, "application/interlockledger");

public Task<PageOf<RecordModel>> RecordsFromAsync(ulong firstSerial, ushort page = 0, byte pageSize = 10, bool lastToFirst = false, bool ommitPayload = false)
=> _rest.GetAsync<PageOf<RecordModel>>($"records@{_id}?firstSerial={firstSerial}&page={page}&pageSize={pageSize}&lastToFirst={lastToFirst}&ommitPayload={ommitPayload}");

public Task<PageOf<RecordModel>> RecordsFromToAsync(ulong firstSerial, ulong lastSerial, ushort page = 0, byte pageSize = 10, bool lastToFirst = false, bool ommitPayload = false)
=> _rest.GetAsync<PageOf<RecordModel>>($"records@{_id}?firstSerial={firstSerial}&lastSerial={lastSerial}&page={page}&pageSize={pageSize}&lastToFirst={lastToFirst}&ommitPayload={ommitPayload}");
public Task<PageOf<RecordModel>?> RecordsFromToAsync(ulong firstSerial, ulong lastSerial, ushort page = 0, byte pageSize = 10, bool lastToFirst = false, bool ommitPayload = false)
=> _node.GetAsync<PageOf<RecordModel>>($"records@{_id}?firstSerial={firstSerial}&lastSerial={lastSerial}&page={page}&pageSize={pageSize}&lastToFirst={lastToFirst}&ommitPayload={ommitPayload}");

private readonly string _id;
private readonly RestAbstractChain _parent;
private readonly IRestNodeInternals _rest;
private readonly RestAbstractChain<T> _parent;
private readonly RestAbstractNode<T> _node;
}
48 changes: 22 additions & 26 deletions InterlockLedger.Rest.Client/Abstractions/RestAbstractChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,47 +35,43 @@
namespace InterlockLedger.Rest.Client.Abstractions;

[DebuggerDisplay("{" + nameof(GetDebuggerDisplay) + "(),nq}")]
public abstract class RestAbstractChain : IRestChain
public abstract class RestAbstractChain<T> : IRestChain where T : IRestChain
{
public string Id { get; }
IRestInterlockings IRestChain.Interlockings => _interlockings;
public string Name { get; }
public IInterlockings Interlockings { get; }
public string? Name { get; }

IRestRecords IRestChain.Records => _records;
public IRecordsStore Records { get; }

IRestRecordsAsJson IRestChain.RecordsAsJson => _recordsAsJson;
public IRecordsAsJsonStore RecordsAsJson { get; }

Task<IEnumerable<ulong>> IRestChain.GetActiveAppsAsync() => _rest.GetAsync<IEnumerable<ulong>>($"/chain/{Id}/activeApps");
public Task<IEnumerable<ulong>?> GetActiveAppsAsync() => _node.GetAsync<IEnumerable<ulong>>($"/chain/{Id}/activeApps");

Task<IEnumerable<KeyModel>> IRestChain.GetPermittedKeysAsync() => _rest.GetAsync<IEnumerable<KeyModel>>($"/chain/{Id}/key");
public Task<IEnumerable<KeyModel>?> GetPermittedKeysAsync() => _node.GetAsync<IEnumerable<KeyModel>>($"/chain/{Id}/key");

Task<ChainSummaryModel> IRestChain.GetSummaryAsync() => _rest.GetAsync<ChainSummaryModel>($"/chain/{Id}");
public Task<ChainSummaryModel?> GetSummaryAsync() => _node.GetAsync<ChainSummaryModel>($"/chain/{Id}");

Task<IEnumerable<ulong>> IRestChain.PermitAppsAsync(params ulong[] appsToPermit)
=> _rest.PostAsync<IEnumerable<ulong>>($"/chain/{Id}/activeApps", appsToPermit);
public Task<IEnumerable<ulong>?> PermitAppsAsync(params ulong[] appsToPermit)
=> _node.PostAsync<IEnumerable<ulong>>($"/chain/{Id}/activeApps", appsToPermit);

Task<IEnumerable<KeyModel>> IRestChain.PermitKeysAsync(params KeyPermitModel[] keysToPermit)
=> _rest.PostAsync<IEnumerable<KeyModel>>($"/chain/{Id}/key", keysToPermit);
public Task<IEnumerable<KeyModel>?> PermitKeysAsync(params KeyPermitModel[] keysToPermit)
=> _node.PostAsync<IEnumerable<KeyModel>>($"/chain/{Id}/key", keysToPermit);

public Task<PageOf<RecordModel>> RecordsFromAsync(ulong firstSerial)
=> _rest.GetAsync<PageOf<RecordModel>>($"records@{Id}?firstSerial={firstSerial}");
public Task<PageOf<RecordModel>?> RecordsFromAsync(ulong firstSerial)
=> _node.GetAsync<PageOf<RecordModel>>($"records@{Id}?firstSerial={firstSerial}");

public override string ToString() => $"Chain '{Name}' #{Id}";

internal readonly IRestNodeInternals _rest;
internal readonly RestAbstractNode<T> _node;

internal RestAbstractChain(IRestNodeInternals rest, ChainIdModel chainId) {
_rest = rest.Required();
Id = chainId.Required().Id;
internal RestAbstractChain(RestAbstractNode<T> node, ChainIdModel chainId) {
_node = node.Required();
Id = chainId.Required().Id.Required();
Name = chainId.Name;
_records = new RecordsImplementation(this);
_recordsAsJson = new RecordsAsJsonImplementation(this);
_interlockings = new InterlockingsImplementation(this);
Records = new RecordsStoreImplementation<T>(this);
RecordsAsJson = new RecordsAsJsonStoreImplementation<T>(this);
Interlockings = new InterlockingsImplementation<T>(this);
}

private readonly IRestInterlockings _interlockings;
private readonly IRestRecords _records;
private readonly IRestRecordsAsJson _recordsAsJson;

private string GetDebuggerDisplay() => $"{this} at {_rest}";
private string GetDebuggerDisplay() => $"{this} at {_node}";
}
Loading

0 comments on commit 0c69a09

Please sign in to comment.