Skip to content

Commit

Permalink
[Feature] Add Restapi acs (#2322)
Browse files Browse the repository at this point in the history
  • Loading branch information
Atralupus authored Nov 29, 2023
1 parent 9caa30b commit 93a277c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public AccessControlServiceController(IMutableAccessControlService accessControl
[HttpGet("entries/{address}")]
public ActionResult<int?> GetTxQuota(string address)
{
return _accessControlService.GetTxQuota(new Address(address));
var result = _accessControlService.GetTxQuota(new Address(address));

return result != null ? result : NotFound();
}

[HttpPost("entries/add-tx-quota/{address}")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ public enum StorageType
/// <summary>
/// Use SQLite
/// </summary>
SQLite
SQLite,

/// <summary>
/// Use RestAPI
/// </summary>
RestAPI
}

public static IAccessControlService Create(
Expand All @@ -27,6 +32,7 @@ string connectionString
{
StorageType.Redis => new RedisAccessControlService(connectionString),
StorageType.SQLite => new SQLiteAccessControlService(connectionString),
StorageType.RestAPI => new RestAPIAccessControlService(connectionString),
_ => throw new ArgumentOutOfRangeException(nameof(storageType), storageType, null)
};
}
Expand Down
2 changes: 0 additions & 2 deletions NineChronicles.Headless/Services/RedisAccessControlService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public RedisAccessControlService(string storageUri)
RedisValue result = _db.StringGet(address.ToString());
if (!result.IsNull)
{
Log.ForContext("Source", nameof(IAccessControlService))
.Verbose("\"{Address}\" Tx Quota: {Quota}", address, result);
return Convert.ToInt32(result);
}

Expand Down
44 changes: 44 additions & 0 deletions NineChronicles.Headless/Services/RestAPIAccessControlService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Net.Http;
using Libplanet.Crypto;
using Nekoyume.Blockchain;
using Serilog;

namespace NineChronicles.Headless.Services
{
public class RestAPIAccessControlService : IAccessControlService
{
private readonly HttpClient _httpClient;
private readonly string _baseUrl;

public RestAPIAccessControlService(string baseUrl)
{
_baseUrl = baseUrl;
_httpClient = new HttpClient
{
Timeout = TimeSpan.FromMilliseconds(500)
};
}

public int? GetTxQuota(Address address)
{
try
{
string requestUri = $"{_baseUrl}/entries/{address}";
HttpResponseMessage response = _httpClient.GetAsync(requestUri).GetAwaiter().GetResult();

if (response.IsSuccessStatusCode)
{
string resultString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
return Convert.ToInt32(resultString);
}
}
catch (Exception ex)
{
Log.Error(ex, "Error occurred while calling Rest API.");
}

return null;
}
}
}
48 changes: 35 additions & 13 deletions NineChronicles.Headless/Services/SQLiteAccessControlService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class SQLiteAccessControlService : IAccessControlService
{
private const string CreateTableSql =
"CREATE TABLE IF NOT EXISTS txquotalist (address VARCHAR(42), quota INT)";
private const string CreateIndexSql = "CREATE INDEX IF NOT EXISTS idx_address ON txquotalist (address);";
private const string GetTxQuotaSql =
"SELECT quota FROM txquotalist WHERE address=@Address";

Expand All @@ -18,31 +19,52 @@ public class SQLiteAccessControlService : IAccessControlService
public SQLiteAccessControlService(string connectionString)
{
_connectionString = connectionString;
using var connection = new SqliteConnection(_connectionString);
connection.Open();
try
{
using var connection = new SqliteConnection(_connectionString);
connection.Open();

using var command = connection.CreateCommand();
command.CommandText = CreateTableSql;
command.ExecuteNonQuery();
ExecuteNonQuery(connection, CreateTableSql);
ExecuteNonQuery(connection, CreateIndexSql);
}
catch (Exception ex)
{
Log.Error(ex, "An error occurred while initializing the database.");
throw;
}
}

public int? GetTxQuota(Address address)
{
using var connection = new SqliteConnection(_connectionString);
connection.Open();
try
{
using var connection = new SqliteConnection(_connectionString);
connection.Open();

using var command = connection.CreateCommand();
command.CommandText = GetTxQuotaSql;
command.Parameters.AddWithValue("@Address", address.ToString());
using var command = connection.CreateCommand();
command.CommandText = GetTxQuotaSql;
command.Parameters.AddWithValue("@Address", address.ToString());

var queryResult = command.ExecuteScalar();
var queryResult = command.ExecuteScalar();

if (queryResult != null)
if (queryResult != null && queryResult != DBNull.Value)
{
return Convert.ToInt32(queryResult);
}
}
catch (Exception ex)
{
return Convert.ToInt32(queryResult);
Log.Error(ex, "An error occurred while getting transaction quota.");
}

return null;
}

private void ExecuteNonQuery(SqliteConnection connection, string commandText)
{
using var command = connection.CreateCommand();
command.CommandText = commandText;
command.ExecuteNonQuery();
}
}
}

0 comments on commit 93a277c

Please sign in to comment.