Normalize greenfield responses for 404s (#3220)

This commit is contained in:
Andrew Camilleri
2021-12-23 05:32:08 +01:00
committed by GitHub
parent ae33fc3031
commit 5f5f71bf37
10 changed files with 49 additions and 70 deletions

View File

@@ -30,11 +30,12 @@ namespace BTCPayServer.Controllers.GreenField
}
[HttpGet("~/api/v1/api-keys/current")]
public async Task<ActionResult<ApiKeyData>> GetKey()
public async Task<IActionResult> GetKey()
{
if (!ControllerContext.HttpContext.GetAPIKey(out var apiKey))
{
return NotFound();
return
this.CreateAPIError(404, "api-key-not-found", "The api key was not present.");
}
var data = await _apiKeyRepository.GetKey(apiKey);
return Ok(FromModel(data));
@@ -44,8 +45,7 @@ namespace BTCPayServer.Controllers.GreenField
[Authorize(Policy = Policies.Unrestricted, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
public async Task<IActionResult> CreateKey(CreateApiKeyRequest request)
{
if (request is null)
return NotFound();
request ??= new CreateApiKeyRequest();
request.Permissions ??= System.Array.Empty<Permission>();
var key = new APIKeyData()
{

View File

@@ -29,7 +29,6 @@ namespace BTCPayServer.Controllers.GreenField
private readonly BTCPayNetworkProvider _btcPayNetworkProvider;
private readonly ISettingsRepository _settingsRepository;
private readonly IAuthorizationService _authorizationService;
protected LightningNodeApiController(BTCPayNetworkProvider btcPayNetworkProvider,
ISettingsRepository settingsRepository,
IAuthorizationService authorizationService)
@@ -95,11 +94,6 @@ namespace BTCPayServer.Controllers.GreenField
public virtual async Task<IActionResult> OpenChannel(string cryptoCode, OpenLightningChannelRequest request)
{
var lightningClient = await GetLightningClient(cryptoCode, true);
if (lightningClient == null)
{
return NotFound();
}
if (request?.NodeURI is null)
{
ModelState.AddModelError(nameof(request.NodeURI),
@@ -166,11 +160,6 @@ namespace BTCPayServer.Controllers.GreenField
public virtual async Task<IActionResult> GetDepositAddress(string cryptoCode)
{
var lightningClient = await GetLightningClient(cryptoCode, true);
if (lightningClient == null)
{
return NotFound();
}
return Ok(new JValue((await lightningClient.GetDepositAddress()).ToString()));
}
@@ -207,18 +196,8 @@ namespace BTCPayServer.Controllers.GreenField
public virtual async Task<IActionResult> GetInvoice(string cryptoCode, string id)
{
var lightningClient = await GetLightningClient(cryptoCode, false);
if (lightningClient == null)
{
return NotFound();
}
var inv = await lightningClient.GetInvoice(id);
if (inv == null)
{
return this.CreateAPIError(404, "invoice-not-found", "Impossible to find a lightning invoice with this id");
}
return Ok(ToModel(inv));
return inv == null ? this.CreateAPIError(404, "invoice-not-found", "Impossible to find a lightning invoice with this id") : Ok(ToModel(inv));
}
public virtual async Task<IActionResult> CreateInvoice(string cryptoCode, CreateLightningInvoiceRequest request)

View File

@@ -568,7 +568,7 @@ namespace BTCPayServer.Controllers.GreenField
public override async Task<PaymentRequestData> GetPaymentRequest(string storeId, string paymentRequestId,
CancellationToken token = default)
{
return GetFromActionResult(await _paymentRequestController.GetPaymentRequest(storeId, paymentRequestId));
return GetFromActionResult<PaymentRequestData>(await _paymentRequestController.GetPaymentRequest(storeId, paymentRequestId));
}
public override async Task ArchivePaymentRequest(string storeId, string paymentRequestId,
@@ -594,7 +594,7 @@ namespace BTCPayServer.Controllers.GreenField
public override async Task<ApiKeyData> GetCurrentAPIKeyInfo(CancellationToken token = default)
{
return GetFromActionResult(await _apiKeysController.GetKey());
return GetFromActionResult<ApiKeyData>(await _apiKeysController.GetKey());
}
public override async Task<ApiKeyData> CreateAPIKey(CreateApiKeyRequest request,
@@ -734,7 +734,7 @@ namespace BTCPayServer.Controllers.GreenField
public override async Task<StoreData> GetStore(string storeId, CancellationToken token = default)
{
return GetFromActionResult(await _storesController.GetStore(storeId));
return GetFromActionResult<StoreData>(_storesController.GetStore(storeId));
}
public override async Task RemoveStore(string storeId, CancellationToken token = default)

View File

@@ -55,7 +55,7 @@ namespace BTCPayServer.Controllers.GreenField
if (items.Count == 0)
{
return NotFound();
return NotificationNotFound();
}
return Ok(ToModel(items.Items.First()));
@@ -71,7 +71,7 @@ namespace BTCPayServer.Controllers.GreenField
if (items.Count == 0)
{
return NotFound();
return NotificationNotFound();
}
return Ok(ToModel(items.First()));
@@ -101,5 +101,9 @@ namespace BTCPayServer.Controllers.GreenField
Link = string.IsNullOrEmpty(entity.ActionLink) ? null : new Uri(entity.ActionLink)
};
}
private IActionResult NotificationNotFound()
{
return this.CreateAPIError(404, "notification-not-found", "The notification was not found");
}
}
}

View File

@@ -42,14 +42,14 @@ namespace BTCPayServer.Controllers.GreenField
[Authorize(Policy = Policies.CanViewPaymentRequests, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpGet("~/api/v1/stores/{storeId}/payment-requests/{paymentRequestId}")]
public async Task<ActionResult<Client.Models.PaymentRequestData>> GetPaymentRequest(string storeId, string paymentRequestId)
public async Task<IActionResult> GetPaymentRequest(string storeId, string paymentRequestId)
{
var pr = await _paymentRequestRepository.FindPaymentRequests(
new PaymentRequestQuery() { StoreId = storeId, Ids = new[] { paymentRequestId } });
if (pr.Total == 0)
{
return NotFound();
return PaymentRequestNotFound();
}
return Ok(FromModel(pr.Items.First()));
@@ -58,13 +58,13 @@ namespace BTCPayServer.Controllers.GreenField
[Authorize(Policy = Policies.CanModifyPaymentRequests,
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpDelete("~/api/v1/stores/{storeId}/payment-requests/{paymentRequestId}")]
public async Task<ActionResult> ArchivePaymentRequest(string storeId, string paymentRequestId)
public async Task<IActionResult> ArchivePaymentRequest(string storeId, string paymentRequestId)
{
var pr = await _paymentRequestRepository.FindPaymentRequests(
new PaymentRequestQuery() { StoreId = storeId, Ids = new[] { paymentRequestId }, IncludeArchived = false });
if (pr.Total == 0)
{
return NotFound();
return PaymentRequestNotFound();
}
var updatedPr = pr.Items.First();
@@ -112,7 +112,7 @@ namespace BTCPayServer.Controllers.GreenField
new PaymentRequestQuery() { StoreId = storeId, Ids = new[] { paymentRequestId } });
if (pr.Total == 0)
{
return NotFound();
return PaymentRequestNotFound();
}
var updatedPr = pr.Items.First();
@@ -164,5 +164,10 @@ namespace BTCPayServer.Controllers.GreenField
CustomCSSLink = blob.CustomCSSLink
};
}
private IActionResult PaymentRequestNotFound()
{
return this.CreateAPIError(404, "payment-request-not-found", "The payment request was not found");
}
}
}

View File

@@ -227,8 +227,6 @@ namespace BTCPayServer.Controllers.GreenField
[AllowAnonymous]
public async Task<IActionResult> CreatePayout(string pullPaymentId, CreatePayoutRequest request)
{
if (request is null)
return NotFound();
if (!PaymentMethodId.TryParse(request?.PaymentMethod, out var paymentMethodId))
{
ModelState.AddModelError(nameof(request.PaymentMethod), "Invalid payment method");

View File

@@ -77,11 +77,7 @@ namespace BTCPayServer.Controllers.GreenField
[HttpGet("~/api/v1/stores/{storeId}/payment-methods/LNURLPay/{cryptoCode}")]
public IActionResult GetLNURLPayPaymentMethod(string storeId, string cryptoCode)
{
if (!GetNetwork(cryptoCode, out BTCPayNetwork _))
{
return NotFound();
}
AssertCryptoCodeWallet(cryptoCode, out _);
var method = GetExistingLNURLPayPaymentMethod(cryptoCode);
if (method is null)
{
@@ -97,10 +93,8 @@ namespace BTCPayServer.Controllers.GreenField
string storeId,
string cryptoCode)
{
if (!GetNetwork(cryptoCode, out BTCPayNetwork _))
{
return NotFound();
}
AssertCryptoCodeWallet(cryptoCode, out _);
var id = new PaymentMethodId(cryptoCode, PaymentTypes.LNURLPay);
var store = Store;
@@ -116,10 +110,7 @@ namespace BTCPayServer.Controllers.GreenField
{
var paymentMethodId = new PaymentMethodId(cryptoCode, PaymentTypes.LNURLPay);
if (!GetNetwork(cryptoCode, out var network))
{
return NotFound();
}
AssertCryptoCodeWallet(cryptoCode, out _);
var lnMethod = StoreLightningNetworkPaymentMethodsController.GetExistingLightningLikePaymentMethod(_btcPayNetworkProvider,
cryptoCode, Store);
@@ -169,12 +160,12 @@ namespace BTCPayServer.Controllers.GreenField
paymentMethod.UseBech32Scheme, paymentMethod.EnableForStandardInvoices
);
}
private bool GetNetwork(string cryptoCode, [MaybeNullWhen(false)] out BTCPayNetwork network)
private void AssertCryptoCodeWallet(string cryptoCode, out BTCPayNetwork network)
{
network = _btcPayNetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode);
network = network?.SupportLightning is true ? network : null;
return network != null;
if (network is null)
throw new JsonHttpException(this.CreateAPIError(404, "unknown-cryptocode", "This crypto code isn't set up in this BTCPay Server instance"));
}
}
}

View File

@@ -18,11 +18,8 @@ namespace BTCPayServer.Controllers.GreenField
public async Task<IActionResult> GenerateOnChainWallet(string storeId, string cryptoCode,
GenerateWalletRequest request)
{
var network = _btcPayNetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode);
if (network is null)
{
return NotFound();
}
AssertCryptoCodeWallet(cryptoCode, out var network, out var wallet);
if (!_walletProvider.IsAvailable(network))
{

View File

@@ -151,7 +151,8 @@ namespace BTCPayServer.Controllers.GreenField
var addr = await _walletReceiveService.UnReserveAddress(new WalletId(storeId, cryptoCode));
if (addr is null)
{
return NotFound();
return this.CreateAPIError("no-reserved-address",
$"There was no reserved address for {cryptoCode} on this store.");
}
return Ok();
}
@@ -210,7 +211,7 @@ namespace BTCPayServer.Controllers.GreenField
var tx = await wallet.FetchTransaction(derivationScheme.AccountDerivation, uint256.Parse(transactionId));
if (tx is null)
{
return NotFound();
return this.CreateAPIError(404, "transaction-not-found", "The transaction was not found.");
}
var walletId = new WalletId(storeId, cryptoCode);
@@ -523,8 +524,7 @@ namespace BTCPayServer.Controllers.GreenField
network = _btcPayNetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode);
if (network is null)
{
actionResult = NotFound();
return true;
throw new JsonHttpException(this.CreateAPIError(404, "unknown-cryptocode", "This crypto code isn't set up in this BTCPay Server instance"));
}

View File

@@ -42,14 +42,14 @@ namespace BTCPayServer.Controllers.GreenField
[Authorize(Policy = Policies.CanViewStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpGet("~/api/v1/stores/{storeId}")]
public Task<ActionResult<Client.Models.StoreData>> GetStore(string storeId)
public IActionResult GetStore(string storeId)
{
var store = HttpContext.GetStoreData();
if (store == null)
{
return Task.FromResult<ActionResult<Client.Models.StoreData>>(NotFound());
return StoreNotFound();
}
return Task.FromResult<ActionResult<Client.Models.StoreData>>(Ok(FromModel(store)));
return Ok(FromModel(store));
}
[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
@@ -59,7 +59,7 @@ namespace BTCPayServer.Controllers.GreenField
var store = HttpContext.GetStoreData();
if (store == null)
{
return NotFound();
return StoreNotFound();
}
if (!_storeRepository.CanDeleteStores())
@@ -96,7 +96,7 @@ namespace BTCPayServer.Controllers.GreenField
var store = HttpContext.GetStoreData();
if (store == null)
{
return NotFound();
return StoreNotFound();
}
var validationResult = Validate(request);
if (validationResult != null)
@@ -215,5 +215,10 @@ namespace BTCPayServer.Controllers.GreenField
return !ModelState.IsValid ? this.CreateValidationError(ModelState) : null;
}
private IActionResult StoreNotFound()
{
return this.CreateAPIError(404, "store-not-found", "The store was not found");
}
}
}