Merge pull request #1576 from dennisreimann/api-server-info

GreenField: Add Server Info API basics
This commit is contained in:
Nicolas Dorier
2020-05-20 02:31:16 +09:00
committed by GitHub
5 changed files with 264 additions and 3 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 float Progress { get; set; }
}
}

View File

@@ -4,11 +4,8 @@ using System.Net.Http;
using System.Threading.Tasks;
using BTCPayServer.Client;
using BTCPayServer.Client.Models;
using BTCPayServer.Controllers;
using BTCPayServer.Services;
using BTCPayServer.Tests.Logging;
using Microsoft.AspNet.SignalR.Client;
using Microsoft.AspNetCore.Mvc;
using Xunit;
using Xunit.Abstractions;
using CreateApplicationUserRequest = BTCPayServer.Client.Models.CreateApplicationUserRequest;
@@ -291,5 +288,34 @@ 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.Version);
Assert.NotNull(serverInfoData.Onion);
Assert.NotNull(serverInfoData.Status);
Assert.True(serverInfoData.Status.FullySynched);
Assert.Contains("BTC", serverInfoData.SupportedPaymentMethods);
Assert.Contains("BTC_LightningLike", serverInfoData.SupportedPaymentMethods);
Assert.NotNull(serverInfoData.Status.SyncStatus);
Assert.Single(serverInfoData.Status.SyncStatus.Select(s => s.CryptoCode == "BTC"));
}
}
}
}

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()
.Where(summary => summary.Network.ShowSyncSummary)
.Select(summary => new ServerInfoSyncStatusData
{
CryptoCode = summary.Network.CryptoCode,
BlockHeaders = summary.Status.ChainHeight,
Progress = summary.Status.SyncHeight.GetValueOrDefault(0) / (float)summary.Status.ChainHeight
});
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"
}
]
}