api fixes

This commit is contained in:
Andrew Camilleri
2018-05-28 14:55:49 +02:00
parent 6cf80b7533
commit c8a26ce952

View File

@@ -31,34 +31,6 @@ namespace BTCPayServer.Controllers
_CurrencyNameTable = currencyNameTable ?? throw new ArgumentNullException(nameof(currencyNameTable)); _CurrencyNameTable = currencyNameTable ?? throw new ArgumentNullException(nameof(currencyNameTable));
} }
[Route("rates")]
[HttpGet]
[BitpayAPIConstraint]
public async Task<IActionResult> GetRates()
{
var store = this.HttpContext.GetStoreData();
var currencypairs = "";
var supportedMethods = store.GetSupportedPaymentMethods(_NetworkProvider);
foreach (var supportedPaymentMethod in supportedMethods)
{
if (!string.IsNullOrEmpty(currencypairs))
{
currencypairs += ",";
}
currencypairs += supportedPaymentMethod.CryptoCode + "_USD,";
currencypairs += supportedPaymentMethod.CryptoCode + "_EUR";
}
var result = await GetRates2(currencypairs, store.Id);
var rates = (result as JsonResult)?.Value as Rate[];
if (rates == null)
return result;
return Json(new DataWrapper<Rate>(rates.First()));
}
[Route("rates/{baseCurrency}")] [Route("rates/{baseCurrency}")]
[HttpGet] [HttpGet]
[BitpayAPIConstraint] [BitpayAPIConstraint]
@@ -68,17 +40,17 @@ namespace BTCPayServer.Controllers
var currencypairs = ""; var currencypairs = "";
var supportedMethods = store.GetSupportedPaymentMethods(_NetworkProvider); var supportedMethods = store.GetSupportedPaymentMethods(_NetworkProvider);
currencypairs += baseCurrency + "_USD,"; var currencyCodes = supportedMethods.Where(method => !string.IsNullOrEmpty(method.CryptoCode))
currencypairs += baseCurrency + "_EUR"; .Select(method => method.CryptoCode).Distinct();
foreach (var supportedPaymentMethod in supportedMethods)
foreach (var currencyCode in currencyCodes)
{ {
if (!string.IsNullOrEmpty(currencypairs)) if (!string.IsNullOrEmpty(currencypairs))
{ {
currencypairs += ","; currencypairs += ",";
} }
currencypairs += baseCurrency + "_ " + currencyCode;
currencypairs += baseCurrency + "_ " +supportedPaymentMethod.CryptoCode +",";
currencypairs += baseCurrency + "_ " + supportedPaymentMethod.CryptoCode;
} }
var result = await GetRates2(currencypairs, store.Id); var result = await GetRates2(currencypairs, store.Id);
var rates = (result as JsonResult)?.Value as Rate[]; var rates = (result as JsonResult)?.Value as Rate[];
@@ -104,7 +76,7 @@ namespace BTCPayServer.Controllers
[Route("rates")] [Route("rates")]
[HttpGet] [HttpGet]
[BitpayAPIConstraint] [BitpayAPIConstraint]
public async Task<IActionResult> GetBitPayRates(string currencyPairs, string storeId) public async Task<IActionResult> GetRates(string currencyPairs, string storeId)
{ {
storeId = storeId ?? this.HttpContext.GetStoreData()?.Id; storeId = storeId ?? this.HttpContext.GetStoreData()?.Id;
var result = await GetRates2(currencyPairs, storeId); var result = await GetRates2(currencyPairs, storeId);
@@ -119,15 +91,14 @@ namespace BTCPayServer.Controllers
[HttpGet] [HttpGet]
public async Task<IActionResult> GetRates2(string currencyPairs, string storeId) public async Task<IActionResult> GetRates2(string currencyPairs, string storeId)
{ {
if(storeId == null || currencyPairs == null) if (storeId == null)
{ {
var result = Json(new BitpayErrorsModel() { Error = "You need to specify storeId (in your store settings) and currencyPairs (eg. BTC_USD,LTC_CAD)" }); var result = Json(new BitpayErrorsModel() { Error = "You need to specify storeId (in your store settings) and currencyPairs (eg. BTC_USD,LTC_CAD)" });
result.StatusCode = 400; result.StatusCode = 400;
return result; return result;
} }
var store = this.HttpContext.GetStoreData(); var store = this.HttpContext.GetStoreData();
if(store == null || store.Id != storeId) if (store == null || store.Id != storeId)
store = await _StoreRepo.FindStore(storeId); store = await _StoreRepo.FindStore(storeId);
if (store == null) if (store == null)
{ {
@@ -135,12 +106,45 @@ namespace BTCPayServer.Controllers
result.StatusCode = 404; result.StatusCode = 404;
return result; return result;
} }
if (currencyPairs == null)
{
currencyPairs = "";
var supportedMethods = store.GetSupportedPaymentMethods(_NetworkProvider);
var currencyCodes = supportedMethods.Where(method => !string.IsNullOrEmpty(method.CryptoCode))
.Select(method => method.CryptoCode).Distinct();
foreach (var currencyCode in currencyCodes)
{
foreach (var currencyCode2 in currencyCodes)
{
if (currencyCode == currencyCode2)
{
continue;
}
if (!string.IsNullOrEmpty(currencyPairs))
{
currencyPairs += ",";
}
currencyPairs += $"{currencyCode}_{currencyCode2}";
}
}
if (string.IsNullOrEmpty(currencyPairs))
{
var result = Json(new BitpayErrorsModel() { Error = "You need to specify storeId (in your store settings) and currencyPairs (eg. BTC_USD,LTC_CAD)" });
result.StatusCode = 400;
return result;
}
}
var rules = store.GetStoreBlob().GetRateRules(_NetworkProvider); var rules = store.GetStoreBlob().GetRateRules(_NetworkProvider);
HashSet<CurrencyPair> pairs = new HashSet<CurrencyPair>(); HashSet<CurrencyPair> pairs = new HashSet<CurrencyPair>();
foreach(var currency in currencyPairs.Split(',')) foreach (var currency in currencyPairs.Split(','))
{ {
if(!CurrencyPair.TryParse(currency, out var pair)) if (!CurrencyPair.TryParse(currency, out var pair))
{ {
var result = Json(new BitpayErrorsModel() { Error = $"Currency pair {currency} uncorrectly formatted" }); var result = Json(new BitpayErrorsModel() { Error = $"Currency pair {currency} uncorrectly formatted" });
result.StatusCode = 400; result.StatusCode = 400;