Skip to content
This repository has been archived by the owner on Aug 10, 2024. It is now read-only.

Commit

Permalink
Optional MAX_COUNT.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin A committed Jul 15, 2022
2 parents ff2c02e + e4fd5a5 commit af26719
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 23 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ ENV ASPNETCORE_URLS=http://+:2022
ENV NODE=http://node:7076
ENV DISABLE_CORS=true
ENV EXCLUDED_CALLS=delegators;delegators_count;representatives
ENV MAX_COUNT=500

CMD [ "dotnet", "NanoPublicApi.dll" ]
1 change: 1 addition & 0 deletions NanoPublicApi/Config/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ namespace NanoPublicApi.Config;
public class Options
{
public IEnumerable<string> ExcludedCalls { get; set; }
public int MaxCount { get; set; }
}
37 changes: 37 additions & 0 deletions NanoPublicApi/Controllers/ApiController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Net;
using System.Reflection;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Mvc;
using NanoPublicApi.Config;
using Newtonsoft.Json;

namespace NanoPublicApi.Controllers;

Expand Down Expand Up @@ -37,6 +39,18 @@ public ApiController(Options options)

#endregion

[HttpGet("api_info")]
[ProducesResponseType(typeof(AggregatedApiInfo), (int)HttpStatusCode.OK)]
public IActionResult ApiInfo()
{
return Json(new AggregatedApiInfo
{
ExcludedCalls = AllCalls.Intersect(Options.ExcludedCalls),
SupportedCalls = AllCalls.Except(Options.ExcludedCalls),
MaxCount = Options.MaxCount
});
}

[HttpGet("excluded_calls")]
[ProducesResponseType(typeof(IEnumerable<string>), (int)HttpStatusCode.OK)]
public IActionResult ExcludedCalls()
Expand All @@ -50,5 +64,28 @@ public IActionResult SupportedCalls()
{
return Json(AllCalls.Except(Options.ExcludedCalls));
}

[HttpGet("max_count")]
[ProducesResponseType(typeof(int), (int)HttpStatusCode.OK)]
public IActionResult MaxCount()
{
return Json(Options.MaxCount);
}

#region " AggregatedApiInfo "

public class AggregatedApiInfo
{
[JsonPropertyName("excluded_calls")]
public IEnumerable<string> ExcludedCalls { get; set; }

[JsonPropertyName("supported_calls")]
public IEnumerable<string> SupportedCalls { get; set; }

[JsonPropertyName("max_count")]
public int MaxCount { get; set; }
}

#endregion

}
8 changes: 8 additions & 0 deletions NanoPublicApi/Controllers/NodeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,14 @@ private async Task<IActionResult> Call<T>(string name, T request)
where T : Request
{
if (Options.ExcludedCalls.Contains(name)) { return Error($"{name} not supported"); }
if (Options.MaxCount > 0 && request is ICountRequest countable)
{
if (!int.TryParse(countable.Count, out int count) || count < 0 || count > Options.MaxCount)
{
return Error($"{count} greater than max count {Options.MaxCount}");
}
}

request.Action = name;
return await Node.Proxy(request);
}
Expand Down
6 changes: 6 additions & 0 deletions NanoPublicApi/Entities/ICountRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace NanoPublicApi.Entities;

public interface ICountRequest
{
public string Count { get; set; }
}
2 changes: 1 addition & 1 deletion NanoPublicApi/Entities/Input/AccountHistoryRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace NanoPublicApi.Entities.Input;

public class AccountHistoryRequest : AccountRequest
public class AccountHistoryRequest : AccountRequest, ICountRequest
{

[DefaultValue("account_history")]
Expand Down
2 changes: 1 addition & 1 deletion NanoPublicApi/Entities/Input/AccountsPendingRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace NanoPublicApi.Entities.Input;

public class AccountsPendingRequest : AccountsRequest
public class AccountsPendingRequest : AccountsRequest, ICountRequest
{

[DefaultValue("accounts_pending_request")]
Expand Down
2 changes: 1 addition & 1 deletion NanoPublicApi/Entities/Input/ChainRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace NanoPublicApi.Entities.Input;

public class ChainRequest : Request
public class ChainRequest : Request, ICountRequest
{

[DefaultValue("chain")]
Expand Down
2 changes: 1 addition & 1 deletion NanoPublicApi/Entities/Input/DelegatorsRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace NanoPublicApi.Entities.Input;

public class DelegatorsRequest : AccountRequest
public class DelegatorsRequest : AccountRequest, ICountRequest
{

[DefaultValue("delegators")]
Expand Down
2 changes: 1 addition & 1 deletion NanoPublicApi/Entities/Input/FrontiersRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace NanoPublicApi.Entities.Input;

public class FrontiersRequest : AccountRequest
public class FrontiersRequest : AccountRequest, ICountRequest
{

[DefaultValue("frontiers")]
Expand Down
2 changes: 1 addition & 1 deletion NanoPublicApi/Entities/Input/ReceivableRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace NanoPublicApi.Entities.Input;

public class ReceivableRequest : AccountRequest
public class ReceivableRequest : AccountRequest, ICountRequest
{

[DefaultValue("receivable")]
Expand Down
2 changes: 1 addition & 1 deletion NanoPublicApi/Entities/Input/RepresentativesRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace NanoPublicApi.Entities.Input;

public class RepresentativesRequest : Request
public class RepresentativesRequest : Request, ICountRequest
{

[DefaultValue("representatives")]
Expand Down
2 changes: 1 addition & 1 deletion NanoPublicApi/Entities/Input/SuccessorsRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace NanoPublicApi.Entities.Input;

public class SuccessorsRequest : Request
public class SuccessorsRequest : Request, ICountRequest
{

[DefaultValue("successors")]
Expand Down
50 changes: 35 additions & 15 deletions NanoPublicApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
using NanoPublicApi.Config;
using NanoPublicApi.Connectors;

var env = Environment.GetEnvironmentVariables();
var node = env["NODE"] as string;
var disableCors = bool.Parse(env["DISABLE_CORS"] as string);
var excludedCalls = (env["EXCLUDED_CALLS"] as string)?.Split(
';',
StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries
) ?? Enumerable.Empty<string>();
var env = GetEnvironmentVariables();
var node = env["NODE"];
var disableCors = bool.Parse(env["DISABLE_CORS"]);
var excludedCalls = env["EXCLUDED_CALLS"]?.Split(
';',
StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries
) ?? Enumerable.Empty<string>();
var maxCount = int.Parse(env["MAX_COUNT"]);
if (maxCount == 0) { maxCount = -1; }

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -17,23 +19,28 @@
builder.Services.AddCors(options =>
options.AddDefaultPolicy(policy =>
policy.AllowAnyOrigin()
.AllowAnyHeader()
.WithMethods("get", "post"))
.AllowAnyHeader()
.WithMethods("get", "post"))
);
}

builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
});
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
});

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddSingleton(_ => new NodeConnector(new Uri(node)));
builder.Services.AddSingleton(_ => new Options { ExcludedCalls = excludedCalls });
builder.Services.AddSingleton(_ =>
new Options
{
ExcludedCalls = excludedCalls,
MaxCount = maxCount
});

var app = builder.Build();

Expand All @@ -51,3 +58,16 @@
app.MapControllers();

app.Run();


static IDictionary<string, string> GetEnvironmentVariables()
{
var env = Environment.GetEnvironmentVariables();
var dic = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (string key in env.Keys)
{
dic.Add(key, (string)env[key]);
}

return dic;
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ services:
- 'DISABLE_CORS=true'
# optional; specifies which calls to remove from support; default value provided
- 'EXCLUDED_CALLS=delegators;delegators_count;representatives'
# optional; specifices a maximum value for count; -1 for no limit; default value provided
- 'MAX_COUNT=500'
networks:
- traefik
- nano
Expand Down

0 comments on commit af26719

Please sign in to comment.