GreenField: Add Server Info API basics

This commit is contained in:
Dennis Reimann 2020-05-16 23:57:49 +02:00
parent cf012a7946
commit e3b348b55c
No known key found for this signature in database
GPG key ID: 5009E1797F03F8D0
6 changed files with 261 additions and 0 deletions

View file

@ -0,0 +1,16 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using BTCPayServer.Client.Models;
namespace BTCPayServer.Client
{
public partial class BTCPayServerClient
{
public virtual async Task<ServerInfoData> GetServerInfo(CancellationToken token = default)
{
var response = await _httpClient.SendAsync(CreateHttpRequest("api/v1/server/info"), token);
return await HandleResponse<ServerInfoData>(response);
}
}
}

View file

@ -0,0 +1,47 @@
using System.Collections.Generic;
namespace BTCPayServer.Client.Models
{
public class ServerInfoData
{
/// <summary>
/// detailed status information
/// </summary>
public ServerInfoStatusData Status { get; set; }
/// <summary>
/// the BTCPay Server version
/// </summary>
public string Version { get; set; }
/// <summary>
/// the Tor hostname
/// </summary>
public string Onion { get; set; }
/// <summary>
/// the payment methods this server supports
/// </summary>
public IEnumerable<string> SupportedPaymentMethods { get; set; }
}
public class ServerInfoStatusData
{
/// <summary>
/// are all chains fully synched
/// </summary>
public bool FullySynched { get; set; }
/// <summary>
/// detailed sync information per chain
/// </summary>
public IEnumerable<ServerInfoSyncStatusData> SyncStatus { get; set; }
}
public class ServerInfoSyncStatusData
{
public string CryptoCode { get; set; }
public int BlockHeaders { get; set; }
public double Progress { get; set; }
}
}

View file

@ -291,5 +291,27 @@ namespace BTCPayServer.Tests
Assert.True(apiHealthData.Synchronized);
}
}
[Fact(Timeout = TestTimeout)]
[Trait("Integration", "Integration")]
public async Task ServerInfoControllerTests()
{
using (var tester = ServerTester.Create())
{
await tester.StartAsync();
var unauthClient = new BTCPayServerClient(tester.PayTester.ServerUri);
await AssertHttpError(401, async () => await unauthClient.GetServerInfo());
var user = tester.NewAccount();
user.GrantAccess();
var clientBasic = await user.CreateClient();
var serverInfoData = await clientBasic.GetServerInfo();
Assert.NotNull(serverInfoData);
Assert.NotNull(serverInfoData.Status);
Assert.True(serverInfoData.Status.FullySynched);
Assert.Contains("BTC", serverInfoData.SupportedPaymentMethods);
Assert.Contains("BTC_LightningLike", serverInfoData.SupportedPaymentMethods);
}
}
}
}

View file

@ -21,6 +21,10 @@
<None Remove="Build\**" />
<None Remove="wwwroot\bundles\jqueryvalidate\**" />
<None Remove="wwwroot\vendor\jquery-nice-select\**" />
<Content Update="wwwroot\swagger\v1\swagger.template.serverinfo.json">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<None Remove="Currencies.txt" />

View file

@ -0,0 +1,73 @@
using System.Linq;
using System.Threading.Tasks;
using BTCPayServer.Client.Models;
using BTCPayServer.Data;
using BTCPayServer.HostedServices;
using BTCPayServer.Security;
using BTCPayServer.Services;
using BTCPayServer.Services.Invoices;
using BTCPayServer.Services.Stores;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
namespace BTCPayServer.Controllers.GreenField
{
[ApiController]
public class GreenFieldServerInfoController : Controller
{
private readonly BTCPayServerEnvironment _env;
private readonly NBXplorerDashboard _dashBoard;
private readonly StoreRepository _storeRepository;
private readonly UserManager<ApplicationUser> _userManager;
private readonly BTCPayNetworkProvider _networkProvider;
private readonly PaymentMethodHandlerDictionary _paymentMethodHandlerDictionary;
public GreenFieldServerInfoController(
BTCPayServerEnvironment env,
NBXplorerDashboard dashBoard,
StoreRepository storeRepository,
UserManager<ApplicationUser> userManager,
BTCPayNetworkProvider networkProvider,
PaymentMethodHandlerDictionary paymentMethodHandlerDictionary)
{
_env = env;
_dashBoard = dashBoard;
_storeRepository = storeRepository;
_userManager = userManager;
_networkProvider = networkProvider;
_paymentMethodHandlerDictionary = paymentMethodHandlerDictionary;
}
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpGet("~/api/v1/server/info")]
public async Task<ActionResult> ServerInfo()
{
var stores = await _storeRepository.GetStoresByUserId(_userManager.GetUserId(User));
var supportedPaymentMethods = _paymentMethodHandlerDictionary
.SelectMany(handler => handler.GetSupportedPaymentMethods().Select(id => id.ToString()))
.Distinct();
var syncStatus = _dashBoard.GetAll()
.Select(summary => new ServerInfoSyncStatusData
{
CryptoCode = summary.Network.CryptoCode,
// TODO: Implement these fields
BlockHeaders = 0,
Progress = 0
});
ServerInfoStatusData status = new ServerInfoStatusData
{
FullySynched = _dashBoard.IsFullySynched(),
SyncStatus = syncStatus
};
ServerInfoData model = new ServerInfoData
{
Status = status,
Onion = _env.OnionUrl,
Version = _env.Version,
SupportedPaymentMethods = supportedPaymentMethods
};
return Ok(model);
}
}
}

View file

@ -0,0 +1,99 @@
{
"paths": {
"/api/v1/server/info": {
"get": {
"tags": [
"ServerInfo"
],
"summary": "Get server info",
"description": "Information about the server, chains and sync states",
"operationId": "ServerInfo_GetServerInfo",
"responses": {
"200": {
"description": "Server information",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ApplicationServerInfoData"
}
}
}
}
},
"security": [
{
"API Key": [],
"Basic": []
}
]
}
}
},
"components": {
"schemas": {
"ApplicationServerInfoData": {
"type": "object",
"properties": {
"status": {
"$ref": "#/components/schemas/ApplicationServerInfoStatusData"
},
"version": {
"type": "string",
"description": "BTCPay Server version"
},
"onion": {
"type": "string",
"description": "The Tor hostname"
},
"supportedPaymentMethods": {
"type": "array",
"description": "The payment methods this server supports",
"items": {
"type": "string"
}
}
}
},
"ApplicationServerInfoStatusData": {
"type": "object",
"description": "Detailed sync status",
"properties": {
"fullySynched": {
"type": "boolean",
"description": "True if the instance is fully synchronized, according to NBXplorer"
},
"syncStatus": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ApplicationServerInfoSyncStatusData"
}
}
}
},
"ApplicationServerInfoSyncStatusData": {
"type": "object",
"description": "Detailed sync status",
"properties": {
"cryptoCode": {
"type": "string",
"description": "True if the instance is fully synchronized, according to NBXplorer"
},
"blockHeaders": {
"type": "integer",
"description": "True if the instance is fully synchronized, according to NBXplorer"
},
"progress": {
"type": "number",
"format": "double",
"description": "True if the instance is fully synchronized, according to NBXplorer"
}
}
}
}
},
"tags": [
{
"name": "ServerInfo"
}
]
}