mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2026-01-29 10:54:36 +01:00
Greenfield: Add store rates api (#4550)
* Add store rates api * Improve doc --------- Co-authored-by: nicolas.dorier <nicolas.dorier@gmail.com>
This commit is contained in:
@@ -85,24 +85,31 @@ namespace BTCPayServer.Controllers.GreenField
|
||||
[HttpPost("preview")]
|
||||
[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
||||
public async Task<IActionResult> PreviewUpdateStoreRateConfiguration(
|
||||
StoreRateConfiguration configuration, [FromQuery] string[] currencyPair)
|
||||
StoreRateConfiguration configuration, [FromQuery] string[]? currencyPair)
|
||||
{
|
||||
var data = HttpContext.GetStoreData();
|
||||
var blob = data.GetStoreBlob();
|
||||
var parsedCurrencyPairs = new HashSet<CurrencyPair>();
|
||||
|
||||
|
||||
foreach (var pair in currencyPair ?? Array.Empty<string>())
|
||||
if (currencyPair?.Any() is true)
|
||||
{
|
||||
if (!CurrencyPair.TryParse(pair, out var currencyPairParsed))
|
||||
foreach (var pair in currencyPair)
|
||||
{
|
||||
ModelState.AddModelError(nameof(currencyPair),
|
||||
$"Invalid currency pair '{pair}' (it should be formatted like BTC_USD,BTC_CAD)");
|
||||
break;
|
||||
}
|
||||
if (!CurrencyPair.TryParse(pair, out var currencyPairParsed))
|
||||
{
|
||||
ModelState.AddModelError(nameof(currencyPair),
|
||||
$"Invalid currency pair '{pair}' (it should be formatted like BTC_USD,BTC_CAD)");
|
||||
break;
|
||||
}
|
||||
|
||||
parsedCurrencyPairs.Add(currencyPairParsed);
|
||||
parsedCurrencyPairs.Add(currencyPairParsed);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
parsedCurrencyPairs = blob.DefaultCurrencyPairs.ToHashSet();
|
||||
}
|
||||
|
||||
ValidateAndSanitizeConfiguration(configuration, blob);
|
||||
if (!ModelState.IsValid)
|
||||
return this.CreateValidationError(ModelState);
|
||||
@@ -113,12 +120,12 @@ namespace BTCPayServer.Controllers.GreenField
|
||||
|
||||
var rateTasks = _rateProviderFactory.FetchRates(parsedCurrencyPairs, rules, CancellationToken.None);
|
||||
await Task.WhenAll(rateTasks.Values);
|
||||
var result = new List<StoreRatePreviewResult>();
|
||||
var result = new List<StoreRateResult>();
|
||||
foreach (var rateTask in rateTasks)
|
||||
{
|
||||
var rateTaskResult = rateTask.Value.Result;
|
||||
|
||||
result.Add(new StoreRatePreviewResult()
|
||||
result.Add(new StoreRateResult()
|
||||
{
|
||||
CurrencyPair = rateTask.Key.ToString(),
|
||||
Errors = rateTaskResult.Errors.Select(errors => errors.ToString()).ToList(),
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using BTCPayServer.Abstractions.Constants;
|
||||
using BTCPayServer.Client;
|
||||
using BTCPayServer.Client.Models;
|
||||
using BTCPayServer.Data;
|
||||
using BTCPayServer.Rating;
|
||||
using BTCPayServer.Services.Rates;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace BTCPayServer.Controllers.GreenField
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/v1/stores/{storeId}/rates")]
|
||||
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
||||
public class GreenfieldStoreRatesController : ControllerBase
|
||||
{
|
||||
private readonly RateFetcher _rateProviderFactory;
|
||||
private readonly BTCPayNetworkProvider _btcPayNetworkProvider;
|
||||
|
||||
public GreenfieldStoreRatesController(
|
||||
RateFetcher rateProviderFactory,
|
||||
BTCPayNetworkProvider btcPayNetworkProvider)
|
||||
{
|
||||
_rateProviderFactory = rateProviderFactory;
|
||||
_btcPayNetworkProvider = btcPayNetworkProvider;
|
||||
}
|
||||
|
||||
[HttpGet("")]
|
||||
[Authorize(Policy = Policies.CanViewStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
||||
public async Task<IActionResult> GetStoreRates([FromQuery] string[]? currencyPair)
|
||||
{
|
||||
var data = HttpContext.GetStoreData();
|
||||
var blob = data.GetStoreBlob();
|
||||
var parsedCurrencyPairs = new HashSet<CurrencyPair>();
|
||||
|
||||
if (currencyPair?.Any() is true)
|
||||
{
|
||||
foreach (var pair in currencyPair)
|
||||
{
|
||||
if (!CurrencyPair.TryParse(pair, out var currencyPairParsed))
|
||||
{
|
||||
ModelState.AddModelError(nameof(currencyPair),
|
||||
$"Invalid currency pair '{pair}' (it should be formatted like BTC_USD,BTC_CAD)");
|
||||
break;
|
||||
}
|
||||
|
||||
parsedCurrencyPairs.Add(currencyPairParsed);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
parsedCurrencyPairs = blob.DefaultCurrencyPairs.ToHashSet();
|
||||
}
|
||||
|
||||
|
||||
var rules = blob.GetRateRules(_btcPayNetworkProvider);
|
||||
|
||||
|
||||
var rateTasks = _rateProviderFactory.FetchRates(parsedCurrencyPairs, rules, CancellationToken.None);
|
||||
await Task.WhenAll(rateTasks.Values);
|
||||
var result = new List<StoreRateResult>();
|
||||
foreach (var rateTask in rateTasks)
|
||||
{
|
||||
var rateTaskResult = rateTask.Value.Result;
|
||||
|
||||
result.Add(new StoreRateResult()
|
||||
{
|
||||
CurrencyPair = rateTask.Key.ToString(),
|
||||
Errors = rateTaskResult.Errors.Select(errors => errors.ToString()).ToList(),
|
||||
Rate = rateTaskResult.Errors.Any() ? null : rateTaskResult.BidAsk.Bid
|
||||
});
|
||||
}
|
||||
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1223,12 +1223,18 @@ namespace BTCPayServer.Controllers.Greenfield
|
||||
return Task.FromResult(GetFromActionResult<StoreRateConfiguration>(GetController<GreenfieldStoreRateConfigurationController>().GetStoreRateConfiguration()));
|
||||
}
|
||||
|
||||
public override async Task<List<StoreRatePreviewResult>> PreviewUpdateStoreRateConfiguration(string storeId,
|
||||
public override async Task<List<StoreRateResult>> GetStoreRates (string storeId,
|
||||
string[] currencyPair, CancellationToken token = default)
|
||||
{
|
||||
return GetFromActionResult<List<StoreRateResult>>(await GetController<GreenfieldStoreRatesController>().GetStoreRates(currencyPair));
|
||||
}
|
||||
|
||||
public override async Task<List<StoreRateResult>> PreviewUpdateStoreRateConfiguration(string storeId,
|
||||
StoreRateConfiguration request,
|
||||
string[] currencyPair,
|
||||
CancellationToken token = default)
|
||||
{
|
||||
return GetFromActionResult<List<StoreRatePreviewResult>>(
|
||||
return GetFromActionResult<List<StoreRateResult>>(
|
||||
await GetController<GreenfieldStoreRateConfigurationController>().PreviewUpdateStoreRateConfiguration(request,
|
||||
currencyPair));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user