diff --git a/BTCPayServer.Client/BTCPayServerClient.APIKeys.cs b/BTCPayServer.Client/BTCPayServerClient.APIKeys.cs index 71afc92c4..adaa28ac3 100644 --- a/BTCPayServer.Client/BTCPayServerClient.APIKeys.cs +++ b/BTCPayServer.Client/BTCPayServerClient.APIKeys.cs @@ -1,58 +1,44 @@ using System; -using System.Collections.Generic; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using BTCPayServer.Client.Models; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + public virtual async Task GetCurrentAPIKeyInfo(CancellationToken token = default) { - public virtual async Task GetCurrentAPIKeyInfo(CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest("api/v1/api-keys/current"), token); - return await HandleResponse(response); - } + return await SendHttpRequest("api/v1/api-keys/current", null, HttpMethod.Get, token); + } - public virtual async Task CreateAPIKey(CreateApiKeyRequest request, CancellationToken token = default) - { - if (request == null) - throw new ArgumentNullException(nameof(request)); - var response = await _httpClient.SendAsync(CreateHttpRequest("api/v1/api-keys", bodyPayload: request, method: HttpMethod.Post), token); - return await HandleResponse(response); - } + public virtual async Task CreateAPIKey(CreateApiKeyRequest request, CancellationToken token = default) + { + if (request == null) throw new ArgumentNullException(nameof(request)); + return await SendHttpRequest("api/v1/api-keys", request, HttpMethod.Post, token); + } - public virtual async Task CreateAPIKey(string userId, CreateApiKeyRequest request, CancellationToken token = default) - { - if (request == null) - throw new ArgumentNullException(nameof(request)); - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/users/{userId}/api-keys", - bodyPayload: request, method: HttpMethod.Post), token); - return await HandleResponse(response); - } + public virtual async Task CreateAPIKey(string userId, CreateApiKeyRequest request, CancellationToken token = default) + { + if (request == null) throw new ArgumentNullException(nameof(request)); + return await SendHttpRequest($"api/v1/users/{userId}/api-keys", request, HttpMethod.Post, token); + } - public virtual async Task RevokeCurrentAPIKeyInfo(CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest("api/v1/api-keys/current", null, HttpMethod.Delete), token); - await HandleResponse(response); - } + public virtual async Task RevokeCurrentAPIKeyInfo(CancellationToken token = default) + { + await SendHttpRequest("api/v1/api-keys/current", null, HttpMethod.Delete, token); + } - public virtual async Task RevokeAPIKey(string apikey, CancellationToken token = default) - { - if (apikey == null) - throw new ArgumentNullException(nameof(apikey)); - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/api-keys/{apikey}", null, HttpMethod.Delete), token); - await HandleResponse(response); - } - public virtual async Task RevokeAPIKey(string userId, string apikey, CancellationToken token = default) - { - if (apikey == null) - throw new ArgumentNullException(nameof(apikey)); - if (userId is null) - throw new ArgumentNullException(nameof(userId)); - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/users/{userId}/api-keys/{apikey}", null, HttpMethod.Delete), token); - await HandleResponse(response); - } + public virtual async Task RevokeAPIKey(string apikey, CancellationToken token = default) + { + if (apikey == null) throw new ArgumentNullException(nameof(apikey)); + await SendHttpRequest($"api/v1/api-keys/{apikey}", null, HttpMethod.Delete, token); + } + public virtual async Task RevokeAPIKey(string userId, string apikey, CancellationToken token = default) + { + if (apikey == null) throw new ArgumentNullException(nameof(apikey)); + if (userId is null) throw new ArgumentNullException(nameof(userId)); + await SendHttpRequest($"api/v1/users/{userId}/api-keys/{apikey}", null, HttpMethod.Delete, token); } } diff --git a/BTCPayServer.Client/BTCPayServerClient.Apps.cs b/BTCPayServer.Client/BTCPayServerClient.Apps.cs index 5dc704d2e..ca7e4b7bc 100644 --- a/BTCPayServer.Client/BTCPayServerClient.Apps.cs +++ b/BTCPayServer.Client/BTCPayServerClient.Apps.cs @@ -4,100 +4,63 @@ using System.Threading; using System.Threading.Tasks; using BTCPayServer.Client.Models; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + public virtual async Task CreatePointOfSaleApp(string storeId, + CreatePointOfSaleAppRequest request, CancellationToken token = default) { + if (request == null) throw new ArgumentNullException(nameof(request)); + return await SendHttpRequest($"api/v1/stores/{storeId}/apps/pos", request, HttpMethod.Post, token); + } - public virtual async Task CreatePointOfSaleApp(string storeId, - CreatePointOfSaleAppRequest request, CancellationToken token = default) - { - if (request == null) - throw new ArgumentNullException(nameof(request)); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/apps/pos", bodyPayload: request, - method: HttpMethod.Post), token); - return await HandleResponse(response); - } + public virtual async Task CreateCrowdfundApp(string storeId, + CreateCrowdfundAppRequest request, CancellationToken token = default) + { + if (request == null) throw new ArgumentNullException(nameof(request)); + return await SendHttpRequest($"api/v1/stores/{storeId}/apps/crowdfund", request, HttpMethod.Post, token); + } - public virtual async Task CreateCrowdfundApp(string storeId, - CreateCrowdfundAppRequest request, CancellationToken token = default) - { - if (request == null) - throw new ArgumentNullException(nameof(request)); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/apps/crowdfund", bodyPayload: request, - method: HttpMethod.Post), token); - return await HandleResponse(response); - } + public virtual async Task UpdatePointOfSaleApp(string appId, + CreatePointOfSaleAppRequest request, CancellationToken token = default) + { + if (request == null) throw new ArgumentNullException(nameof(request)); + return await SendHttpRequest($"api/v1/apps/pos/{appId}", request, HttpMethod.Put, token); + } - public virtual async Task UpdatePointOfSaleApp(string appId, - CreatePointOfSaleAppRequest request, CancellationToken token = default) - { - if (request == null) - throw new ArgumentNullException(nameof(request)); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/apps/pos/{appId}", bodyPayload: request, - method: HttpMethod.Put), token); - return await HandleResponse(response); - } + public virtual async Task GetApp(string appId, CancellationToken token = default) + { + if (appId == null) throw new ArgumentNullException(nameof(appId)); + return await SendHttpRequest($"api/v1/apps/{appId}", null, HttpMethod.Get, token); + } - public virtual async Task GetApp(string appId, CancellationToken token = default) - { - if (appId == null) - throw new ArgumentNullException(nameof(appId)); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/apps/{appId}", - method: HttpMethod.Get), token); - return await HandleResponse(response); - } + public virtual async Task GetAllApps(string storeId, CancellationToken token = default) + { + if (storeId == null) throw new ArgumentNullException(nameof(storeId)); + return await SendHttpRequest($"api/v1/stores/{storeId}/apps", null, HttpMethod.Get, token); + } - public virtual async Task GetAllApps(string storeId, CancellationToken token = default) - { - if (storeId == null) - throw new ArgumentNullException(nameof(storeId)); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/apps", - method: HttpMethod.Get), token); - return await HandleResponse(response); - } + public virtual async Task GetAllApps(CancellationToken token = default) + { + return await SendHttpRequest("api/v1/apps", null, HttpMethod.Get, token); + } - public virtual async Task GetAllApps(CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/apps", - method: HttpMethod.Get), token); - return await HandleResponse(response); - } + public virtual async Task GetPosApp(string appId, CancellationToken token = default) + { + if (appId == null) throw new ArgumentNullException(nameof(appId)); + return await SendHttpRequest($"api/v1/apps/pos/{appId}", null, HttpMethod.Get, token); + } - public virtual async Task GetPosApp(string appId, CancellationToken token = default) - { - if (appId == null) - throw new ArgumentNullException(nameof(appId)); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/apps/pos/{appId}", - method: HttpMethod.Get), token); - return await HandleResponse(response); - } + public virtual async Task GetCrowdfundApp(string appId, CancellationToken token = default) + { + if (appId == null) throw new ArgumentNullException(nameof(appId)); + return await SendHttpRequest($"api/v1/apps/crowdfund/{appId}", null, HttpMethod.Get, token); + } - public virtual async Task GetCrowdfundApp(string appId, CancellationToken token = default) - { - if (appId == null) - throw new ArgumentNullException(nameof(appId)); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/apps/crowdfund/{appId}", - method: HttpMethod.Get), token); - return await HandleResponse(response); - } - - public virtual async Task DeleteApp(string appId, CancellationToken token = default) - { - if (appId == null) - throw new ArgumentNullException(nameof(appId)); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/apps/{appId}", - method: HttpMethod.Delete), token); - await HandleResponse(response); - } + public virtual async Task DeleteApp(string appId, CancellationToken token = default) + { + if (appId == null) throw new ArgumentNullException(nameof(appId)); + await SendHttpRequest($"api/v1/apps/{appId}", null, HttpMethod.Delete, token); } } diff --git a/BTCPayServer.Client/BTCPayServerClient.Authorization.cs b/BTCPayServer.Client/BTCPayServerClient.Authorization.cs index e5e5ab852..e34e023fe 100644 --- a/BTCPayServer.Client/BTCPayServerClient.Authorization.cs +++ b/BTCPayServer.Client/BTCPayServerClient.Authorization.cs @@ -1,34 +1,29 @@ using System; using System.Collections.Generic; -using System.Threading.Tasks; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + public static Uri GenerateAuthorizeUri(Uri btcpayHost, string[] permissions, bool strict = true, + bool selectiveStores = false, (string ApplicationIdentifier, Uri Redirect) applicationDetails = default) { - - public static Uri GenerateAuthorizeUri(Uri btcpayHost, string[] permissions, bool strict = true, - bool selectiveStores = false, (string ApplicationIdentifier, Uri Redirect) applicationDetails = default) - { - var result = new UriBuilder(btcpayHost); - result.Path = "api-keys/authorize"; - - AppendPayloadToQuery(result, - new Dictionary() - { - {"strict", strict}, {"selectiveStores", selectiveStores}, {"permissions", permissions} - }); - - if (applicationDetails.Redirect != null) + var result = new UriBuilder(btcpayHost) { Path = "api-keys/authorize" }; + AppendPayloadToQuery(result, + new Dictionary { - AppendPayloadToQuery(result, new KeyValuePair("redirect", applicationDetails.Redirect)); - if (!string.IsNullOrEmpty(applicationDetails.ApplicationIdentifier)) - { - AppendPayloadToQuery(result, new KeyValuePair("applicationIdentifier", applicationDetails.ApplicationIdentifier)); - } - } + {"strict", strict}, {"selectiveStores", selectiveStores}, {"permissions", permissions} + }); - return result.Uri; + if (applicationDetails.Redirect != null) + { + AppendPayloadToQuery(result, new KeyValuePair("redirect", applicationDetails.Redirect)); + if (!string.IsNullOrEmpty(applicationDetails.ApplicationIdentifier)) + { + AppendPayloadToQuery(result, new KeyValuePair("applicationIdentifier", applicationDetails.ApplicationIdentifier)); + } } + + return result.Uri; } } diff --git a/BTCPayServer.Client/BTCPayServerClient.Health.cs b/BTCPayServer.Client/BTCPayServerClient.Health.cs index 0643b364d..edf71fc67 100644 --- a/BTCPayServer.Client/BTCPayServerClient.Health.cs +++ b/BTCPayServer.Client/BTCPayServerClient.Health.cs @@ -1,15 +1,14 @@ +using System.Net.Http; using System.Threading; using System.Threading.Tasks; using BTCPayServer.Client.Models; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + public virtual async Task GetHealth(CancellationToken token = default) { - public virtual async Task GetHealth(CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest("api/v1/health"), token); - return await HandleResponse(response); - } + return await SendHttpRequest("api/v1/health", null, HttpMethod.Get, token); } } diff --git a/BTCPayServer.Client/BTCPayServerClient.Invoices.cs b/BTCPayServer.Client/BTCPayServerClient.Invoices.cs index aabccfe55..2e201beea 100644 --- a/BTCPayServer.Client/BTCPayServerClient.Invoices.cs +++ b/BTCPayServer.Client/BTCPayServerClient.Invoices.cs @@ -7,139 +7,95 @@ using System.Threading.Tasks; using BTCPayServer.Client.Models; using NBitcoin; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + public virtual async Task> GetInvoices(string storeId, string[] orderId = null, + InvoiceStatus[] status = null, + DateTimeOffset? startDate = null, + DateTimeOffset? endDate = null, + string textSearch = null, + bool includeArchived = false, + int? skip = null, + int? take = null, + CancellationToken token = default) { - public virtual async Task> GetInvoices(string storeId, string[] orderId = null, - InvoiceStatus[] status = null, - DateTimeOffset? startDate = null, - DateTimeOffset? endDate = null, - string textSearch = null, - bool includeArchived = false, - int? skip = null, - int? take = null, - CancellationToken token = default) - { - Dictionary queryPayload = new Dictionary(); - queryPayload.Add(nameof(includeArchived), includeArchived); + var queryPayload = new Dictionary { { nameof(includeArchived), includeArchived } }; + if (startDate is { } s) + queryPayload.Add(nameof(startDate), Utils.DateTimeToUnixTime(s)); + if (endDate is { } e) + queryPayload.Add(nameof(endDate), Utils.DateTimeToUnixTime(e)); + if (orderId != null) + queryPayload.Add(nameof(orderId), orderId); + if (textSearch != null) + queryPayload.Add(nameof(textSearch), textSearch); + if (status != null) + queryPayload.Add(nameof(status), status.Select(s => s.ToString().ToLower()).ToArray()); + if (skip != null) + queryPayload.Add(nameof(skip), skip); + if (take != null) + queryPayload.Add(nameof(take), take); - if (startDate is DateTimeOffset s) - queryPayload.Add(nameof(startDate), Utils.DateTimeToUnixTime(s)); + return await SendHttpRequest>($"api/v1/stores/{storeId}/invoices", queryPayload, HttpMethod.Get, token); + } - if (endDate is DateTimeOffset e) - queryPayload.Add(nameof(endDate), Utils.DateTimeToUnixTime(e)); + public virtual async Task GetInvoice(string storeId, string invoiceId, + CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/invoices/{invoiceId}", null, HttpMethod.Get, token); + } + public virtual async Task GetInvoicePaymentMethods(string storeId, string invoiceId, + CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/invoices/{invoiceId}/payment-methods", null, HttpMethod.Get, token); + } - if (orderId != null) - queryPayload.Add(nameof(orderId), orderId); - if (textSearch != null) - queryPayload.Add(nameof(textSearch), textSearch); - if (status != null) - queryPayload.Add(nameof(status), status.Select(s => s.ToString().ToLower()).ToArray()); + public virtual async Task ArchiveInvoice(string storeId, string invoiceId, + CancellationToken token = default) + { + await SendHttpRequest($"api/v1/stores/{storeId}/invoices/{invoiceId}", null, HttpMethod.Delete, token); + } - if (skip != null) - { - queryPayload.Add(nameof(skip), skip); - } + public virtual async Task CreateInvoice(string storeId, + CreateInvoiceRequest request, CancellationToken token = default) + { + if (request == null) throw new ArgumentNullException(nameof(request)); + return await SendHttpRequest($"api/v1/stores/{storeId}/invoices", request, HttpMethod.Post, token); + } - if (take != null) - { - queryPayload.Add(nameof(take), take); - } + public virtual async Task UpdateInvoice(string storeId, string invoiceId, + UpdateInvoiceRequest request, CancellationToken token = default) + { + if (request == null) throw new ArgumentNullException(nameof(request)); + return await SendHttpRequest($"api/v1/stores/{storeId}/invoices/{invoiceId}", request, HttpMethod.Put, token); + } - var response = - await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/invoices", - queryPayload), token); - return await HandleResponse>(response); - } + public virtual async Task MarkInvoiceStatus(string storeId, string invoiceId, + MarkInvoiceStatusRequest request, CancellationToken token = default) + { + if (request == null) throw new ArgumentNullException(nameof(request)); + if (request.Status != InvoiceStatus.Settled && request.Status != InvoiceStatus.Invalid) throw new ArgumentOutOfRangeException(nameof(request.Status), "Status can only be Invalid or Complete"); + return await SendHttpRequest($"api/v1/stores/{storeId}/invoices/{invoiceId}/status", request, HttpMethod.Post, token); + } - public virtual async Task GetInvoice(string storeId, string invoiceId, - CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/invoices/{invoiceId}"), token); - return await HandleResponse(response); - } - public virtual async Task GetInvoicePaymentMethods(string storeId, string invoiceId, - CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/invoices/{invoiceId}/payment-methods"), token); - return await HandleResponse(response); - } + public virtual async Task UnarchiveInvoice(string storeId, string invoiceId, CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/invoices/{invoiceId}/unarchive", null, HttpMethod.Post, token); + } - public virtual async Task ArchiveInvoice(string storeId, string invoiceId, - CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/invoices/{invoiceId}", - method: HttpMethod.Delete), token); - await HandleResponse(response); - } + public virtual async Task ActivateInvoicePaymentMethod(string storeId, string invoiceId, string paymentMethod, CancellationToken token = default) + { + await SendHttpRequest($"api/v1/stores/{storeId}/invoices/{invoiceId}/payment-methods/{paymentMethod}/activate", null, HttpMethod.Post, token); + } - public virtual async Task CreateInvoice(string storeId, - CreateInvoiceRequest request, CancellationToken token = default) - { - if (request == null) - throw new ArgumentNullException(nameof(request)); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/invoices", bodyPayload: request, - method: HttpMethod.Post), token); - return await HandleResponse(response); - } - - public virtual async Task UpdateInvoice(string storeId, string invoiceId, - UpdateInvoiceRequest request, CancellationToken token = default) - { - if (request == null) - throw new ArgumentNullException(nameof(request)); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/invoices/{invoiceId}", bodyPayload: request, - method: HttpMethod.Put), token); - return await HandleResponse(response); - } - - public virtual async Task MarkInvoiceStatus(string storeId, string invoiceId, - MarkInvoiceStatusRequest request, CancellationToken token = default) - { - if (request == null) - throw new ArgumentNullException(nameof(request)); - if (request.Status != InvoiceStatus.Settled && request.Status != InvoiceStatus.Invalid) - throw new ArgumentOutOfRangeException(nameof(request.Status), "Status can only be Invalid or Complete"); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/invoices/{invoiceId}/status", bodyPayload: request, - method: HttpMethod.Post), token); - return await HandleResponse(response); - } - - public virtual async Task UnarchiveInvoice(string storeId, string invoiceId, CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/invoices/{invoiceId}/unarchive", - method: HttpMethod.Post), token); - return await HandleResponse(response); - } - - public virtual async Task ActivateInvoicePaymentMethod(string storeId, string invoiceId, string paymentMethod, CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/invoices/{invoiceId}/payment-methods/{paymentMethod}/activate", - method: HttpMethod.Post), token); - await HandleResponse(response); - } - - public virtual async Task RefundInvoice( - string storeId, - string invoiceId, - RefundInvoiceRequest request, - CancellationToken token = default - ) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/invoices/{invoiceId}/refund", bodyPayload: request, - method: HttpMethod.Post), token); - return await HandleResponse(response); - } + public virtual async Task RefundInvoice( + string storeId, + string invoiceId, + RefundInvoiceRequest request, + CancellationToken token = default + ) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/invoices/{invoiceId}/refund", request, HttpMethod.Post, token); } } diff --git a/BTCPayServer.Client/BTCPayServerClient.Lightning.Internal.cs b/BTCPayServer.Client/BTCPayServerClient.Lightning.Internal.cs index 36abdf8d4..979dd014a 100644 --- a/BTCPayServer.Client/BTCPayServerClient.Lightning.Internal.cs +++ b/BTCPayServer.Client/BTCPayServerClient.Lightning.Internal.cs @@ -5,142 +5,101 @@ using System.Threading; using System.Threading.Tasks; using BTCPayServer.Client.Models; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + public virtual async Task GetLightningNodeInfo(string cryptoCode, + CancellationToken token = default) { - public virtual async Task GetLightningNodeInfo(string cryptoCode, - CancellationToken token = default) + return await SendHttpRequest($"api/v1/server/lightning/{cryptoCode}/info", null, HttpMethod.Get, token); + } + + public virtual async Task GetLightningNodeBalance(string cryptoCode, + CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/server/lightning/{cryptoCode}/balance", null, HttpMethod.Get, token); + } + + public virtual async Task ConnectToLightningNode(string cryptoCode, ConnectToNodeRequest request, + CancellationToken token = default) + { + if (request == null) throw new ArgumentNullException(nameof(request)); + await SendHttpRequest($"api/v1/server/lightning/{cryptoCode}/connect", request, HttpMethod.Post, token); + } + + public virtual async Task> GetLightningNodeChannels(string cryptoCode, + CancellationToken token = default) + { + return await SendHttpRequest>($"api/v1/server/lightning/{cryptoCode}/channels", null, HttpMethod.Get, token); + } + + public virtual async Task OpenLightningChannel(string cryptoCode, OpenLightningChannelRequest request, + CancellationToken token = default) + { + await SendHttpRequest($"api/v1/server/lightning/{cryptoCode}/channels", request, HttpMethod.Post, token); + } + + public virtual async Task GetLightningDepositAddress(string cryptoCode, CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/server/lightning/{cryptoCode}/address", null, HttpMethod.Post, token); + } + + public virtual async Task PayLightningInvoice(string cryptoCode, PayLightningInvoiceRequest request, + CancellationToken token = default) + { + if (request == null) throw new ArgumentNullException(nameof(request)); + return await SendHttpRequest($"api/v1/server/lightning/{cryptoCode}/invoices/pay", request, HttpMethod.Post, token); + } + + public virtual async Task GetLightningPayment(string cryptoCode, + string paymentHash, CancellationToken token = default) + { + if (paymentHash == null) throw new ArgumentNullException(nameof(paymentHash)); + return await SendHttpRequest($"api/v1/server/lightning/{cryptoCode}/payments/{paymentHash}", null, HttpMethod.Get, token); + } + + public virtual async Task GetLightningInvoice(string cryptoCode, + string invoiceId, CancellationToken token = default) + { + if (invoiceId == null) throw new ArgumentNullException(nameof(invoiceId)); + return await SendHttpRequest($"api/v1/server/lightning/{cryptoCode}/invoices/{invoiceId}", null, HttpMethod.Get, token); + } + + public virtual async Task GetLightningInvoices(string cryptoCode, + bool? pendingOnly = null, long? offsetIndex = null, CancellationToken token = default) + { + var queryPayload = new Dictionary(); + if (pendingOnly is bool v) { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/server/lightning/{cryptoCode}/info", - method: HttpMethod.Get), token); - return await HandleResponse(response); + queryPayload.Add("pendingOnly", v.ToString()); } - - public virtual async Task GetLightningNodeBalance(string cryptoCode, - CancellationToken token = default) + if (offsetIndex is > 0) { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/server/lightning/{cryptoCode}/balance", - method: HttpMethod.Get), token); - return await HandleResponse(response); + queryPayload.Add("offsetIndex", offsetIndex); } + return await SendHttpRequest($"api/v1/server/lightning/{cryptoCode}/invoices", queryPayload, HttpMethod.Get, token); + } - public virtual async Task ConnectToLightningNode(string cryptoCode, ConnectToNodeRequest request, - CancellationToken token = default) + public virtual async Task GetLightningPayments(string cryptoCode, + bool? includePending = null, long? offsetIndex = null, CancellationToken token = default) + { + var queryPayload = new Dictionary(); + if (includePending is bool v) { - if (request == null) - throw new ArgumentNullException(nameof(request)); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/server/lightning/{cryptoCode}/connect", bodyPayload: request, - method: HttpMethod.Post), token); - await HandleResponse(response); + queryPayload.Add("includePending", v.ToString()); } - - public virtual async Task> GetLightningNodeChannels(string cryptoCode, - CancellationToken token = default) + if (offsetIndex is > 0) { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/server/lightning/{cryptoCode}/channels", - method: HttpMethod.Get), token); - return await HandleResponse>(response); + queryPayload.Add("offsetIndex", offsetIndex); } + return await SendHttpRequest($"api/v1/server/lightning/{cryptoCode}/payments", queryPayload, HttpMethod.Get, token); + } - public virtual async Task OpenLightningChannel(string cryptoCode, OpenLightningChannelRequest request, - CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/server/lightning/{cryptoCode}/channels", bodyPayload: request, - method: HttpMethod.Post), token); - await HandleResponse(response); - } - - public virtual async Task GetLightningDepositAddress(string cryptoCode, CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/server/lightning/{cryptoCode}/address", method: HttpMethod.Post), token); - return await HandleResponse(response); - } - - public virtual async Task PayLightningInvoice(string cryptoCode, PayLightningInvoiceRequest request, - CancellationToken token = default) - { - if (request == null) - throw new ArgumentNullException(nameof(request)); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/server/lightning/{cryptoCode}/invoices/pay", bodyPayload: request, - method: HttpMethod.Post), token); - return await HandleResponse(response); - } - - public virtual async Task GetLightningPayment(string cryptoCode, - string paymentHash, CancellationToken token = default) - { - if (paymentHash == null) - throw new ArgumentNullException(nameof(paymentHash)); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/server/lightning/{cryptoCode}/payments/{paymentHash}", - method: HttpMethod.Get), token); - return await HandleResponse(response); - } - - public virtual async Task GetLightningInvoice(string cryptoCode, - string invoiceId, CancellationToken token = default) - { - if (invoiceId == null) - throw new ArgumentNullException(nameof(invoiceId)); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/server/lightning/{cryptoCode}/invoices/{invoiceId}", - method: HttpMethod.Get), token); - return await HandleResponse(response); - } - - public virtual async Task GetLightningInvoices(string cryptoCode, - bool? pendingOnly = null, long? offsetIndex = null, CancellationToken token = default) - { - var queryPayload = new Dictionary(); - if (pendingOnly is bool v) - { - queryPayload.Add("pendingOnly", v.ToString()); - } - if (offsetIndex is > 0) - { - queryPayload.Add("offsetIndex", offsetIndex); - } - - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/server/lightning/{cryptoCode}/invoices", queryPayload), token); - return await HandleResponse(response); - } - - public virtual async Task GetLightningPayments(string cryptoCode, - bool? includePending = null, long? offsetIndex = null, CancellationToken token = default) - { - var queryPayload = new Dictionary(); - if (includePending is bool v) - { - queryPayload.Add("includePending", v.ToString()); - } - if (offsetIndex is > 0) - { - queryPayload.Add("offsetIndex", offsetIndex); - } - - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/server/lightning/{cryptoCode}/payments", queryPayload), token); - return await HandleResponse(response); - } - - public virtual async Task CreateLightningInvoice(string cryptoCode, CreateLightningInvoiceRequest request, - CancellationToken token = default) - { - if (request == null) - throw new ArgumentNullException(nameof(request)); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/server/lightning/{cryptoCode}/invoices", bodyPayload: request, - method: HttpMethod.Post), token); - return await HandleResponse(response); - } + public virtual async Task CreateLightningInvoice(string cryptoCode, CreateLightningInvoiceRequest request, + CancellationToken token = default) + { + if (request == null) throw new ArgumentNullException(nameof(request)); + return await SendHttpRequest($"api/v1/server/lightning/{cryptoCode}/invoices", request, HttpMethod.Post, token); } } diff --git a/BTCPayServer.Client/BTCPayServerClient.Lightning.Store.cs b/BTCPayServer.Client/BTCPayServerClient.Lightning.Store.cs index abe4c6c06..eeefeb570 100644 --- a/BTCPayServer.Client/BTCPayServerClient.Lightning.Store.cs +++ b/BTCPayServer.Client/BTCPayServerClient.Lightning.Store.cs @@ -5,144 +5,102 @@ using System.Threading; using System.Threading.Tasks; using BTCPayServer.Client.Models; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + public virtual async Task GetLightningNodeInfo(string storeId, string cryptoCode, + CancellationToken token = default) { - public virtual async Task GetLightningNodeInfo(string storeId, string cryptoCode, - CancellationToken token = default) + return await SendHttpRequest($"api/v1/stores/{storeId}/lightning/{cryptoCode}/info", null, HttpMethod.Get, token); + } + + public virtual async Task GetLightningNodeBalance(string storeId, string cryptoCode, + CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/lightning/{cryptoCode}/balance", null, HttpMethod.Get, token); + } + + public virtual async Task ConnectToLightningNode(string storeId, string cryptoCode, ConnectToNodeRequest request, + CancellationToken token = default) + { + if (request == null) throw new ArgumentNullException(nameof(request)); + await SendHttpRequest($"api/v1/stores/{storeId}/lightning/{cryptoCode}/connect", request, HttpMethod.Post, token); + } + + public virtual async Task> GetLightningNodeChannels(string storeId, string cryptoCode, + CancellationToken token = default) + { + return await SendHttpRequest>($"api/v1/stores/{storeId}/lightning/{cryptoCode}/channels", null, HttpMethod.Get, token); + } + + public virtual async Task OpenLightningChannel(string storeId, string cryptoCode, OpenLightningChannelRequest request, + CancellationToken token = default) + { + await SendHttpRequest($"api/v1/stores/{storeId}/lightning/{cryptoCode}/channels", request, HttpMethod.Post, token); + } + + public virtual async Task GetLightningDepositAddress(string storeId, string cryptoCode, + CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/lightning/{cryptoCode}/address", null, HttpMethod.Post, token); + } + + public virtual async Task PayLightningInvoice(string storeId, string cryptoCode, PayLightningInvoiceRequest request, + CancellationToken token = default) + { + if (request == null) throw new ArgumentNullException(nameof(request)); + return await SendHttpRequest($"api/v1/stores/{storeId}/lightning/{cryptoCode}/invoices/pay", request, HttpMethod.Post, token); + } + + public virtual async Task GetLightningPayment(string storeId, string cryptoCode, + string paymentHash, CancellationToken token = default) + { + if (paymentHash == null) throw new ArgumentNullException(nameof(paymentHash)); + return await SendHttpRequest($"api/v1/stores/{storeId}/lightning/{cryptoCode}/payments/{paymentHash}", null, HttpMethod.Get, token); + } + + public virtual async Task GetLightningInvoice(string storeId, string cryptoCode, + string invoiceId, CancellationToken token = default) + { + if (invoiceId == null) throw new ArgumentNullException(nameof(invoiceId)); + return await SendHttpRequest($"api/v1/stores/{storeId}/lightning/{cryptoCode}/invoices/{invoiceId}", null, HttpMethod.Get, token); + } + + public virtual async Task GetLightningInvoices(string storeId, string cryptoCode, + bool? pendingOnly = null, long? offsetIndex = null, CancellationToken token = default) + { + var queryPayload = new Dictionary(); + if (pendingOnly is bool v) { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/lightning/{cryptoCode}/info", - method: HttpMethod.Get), token); - return await HandleResponse(response); + queryPayload.Add("pendingOnly", v.ToString()); } - - public virtual async Task GetLightningNodeBalance(string storeId, string cryptoCode, - CancellationToken token = default) + if (offsetIndex is > 0) { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/lightning/{cryptoCode}/balance", - method: HttpMethod.Get), token); - return await HandleResponse(response); + queryPayload.Add("offsetIndex", offsetIndex); } + return await SendHttpRequest($"api/v1/stores/{storeId}/lightning/{cryptoCode}/invoices", queryPayload, HttpMethod.Get, token); + } - public virtual async Task ConnectToLightningNode(string storeId, string cryptoCode, ConnectToNodeRequest request, - CancellationToken token = default) + public virtual async Task GetLightningPayments(string storeId, string cryptoCode, + bool? includePending = null, long? offsetIndex = null, CancellationToken token = default) + { + var queryPayload = new Dictionary(); + if (includePending is bool v) { - if (request == null) - throw new ArgumentNullException(nameof(request)); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/lightning/{cryptoCode}/connect", bodyPayload: request, - method: HttpMethod.Post), token); - await HandleResponse(response); + queryPayload.Add("includePending", v.ToString()); } - - public virtual async Task> GetLightningNodeChannels(string storeId, string cryptoCode, - CancellationToken token = default) + if (offsetIndex is > 0) { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/lightning/{cryptoCode}/channels", - method: HttpMethod.Get), token); - return await HandleResponse>(response); + queryPayload.Add("offsetIndex", offsetIndex); } + return await SendHttpRequest($"api/v1/stores/{storeId}/lightning/{cryptoCode}/payments", queryPayload, HttpMethod.Get, token); + } - public virtual async Task OpenLightningChannel(string storeId, string cryptoCode, OpenLightningChannelRequest request, - CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/lightning/{cryptoCode}/channels", bodyPayload: request, - method: HttpMethod.Post), token); - await HandleResponse(response); - } - - public virtual async Task GetLightningDepositAddress(string storeId, string cryptoCode, - CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/lightning/{cryptoCode}/address", method: HttpMethod.Post), - token); - return await HandleResponse(response); - } - - public virtual async Task PayLightningInvoice(string storeId, string cryptoCode, PayLightningInvoiceRequest request, - CancellationToken token = default) - { - if (request == null) - throw new ArgumentNullException(nameof(request)); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/lightning/{cryptoCode}/invoices/pay", bodyPayload: request, - method: HttpMethod.Post), token); - return await HandleResponse(response); - } - - public virtual async Task GetLightningPayment(string storeId, string cryptoCode, - string paymentHash, CancellationToken token = default) - { - if (paymentHash == null) - throw new ArgumentNullException(nameof(paymentHash)); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/lightning/{cryptoCode}/payments/{paymentHash}", - method: HttpMethod.Get), token); - return await HandleResponse(response); - } - - public virtual async Task GetLightningInvoice(string storeId, string cryptoCode, - string invoiceId, CancellationToken token = default) - { - if (invoiceId == null) - throw new ArgumentNullException(nameof(invoiceId)); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/lightning/{cryptoCode}/invoices/{invoiceId}", - method: HttpMethod.Get), token); - return await HandleResponse(response); - } - - public virtual async Task GetLightningInvoices(string storeId, string cryptoCode, - bool? pendingOnly = null, long? offsetIndex = null, CancellationToken token = default) - { - var queryPayload = new Dictionary(); - if (pendingOnly is bool v) - { - queryPayload.Add("pendingOnly", v.ToString()); - } - if (offsetIndex is > 0) - { - queryPayload.Add("offsetIndex", offsetIndex); - } - - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/lightning/{cryptoCode}/invoices", queryPayload), token); - return await HandleResponse(response); - } - - public virtual async Task GetLightningPayments(string storeId, string cryptoCode, - bool? includePending = null, long? offsetIndex = null, CancellationToken token = default) - { - var queryPayload = new Dictionary(); - if (includePending is bool v) - { - queryPayload.Add("includePending", v.ToString()); - } - if (offsetIndex is > 0) - { - queryPayload.Add("offsetIndex", offsetIndex); - } - - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/lightning/{cryptoCode}/payments", queryPayload), token); - return await HandleResponse(response); - } - - public virtual async Task CreateLightningInvoice(string storeId, string cryptoCode, - CreateLightningInvoiceRequest request, CancellationToken token = default) - { - if (request == null) - throw new ArgumentNullException(nameof(request)); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/lightning/{cryptoCode}/invoices", bodyPayload: request, - method: HttpMethod.Post), token); - return await HandleResponse(response); - } + public virtual async Task CreateLightningInvoice(string storeId, string cryptoCode, + CreateLightningInvoiceRequest request, CancellationToken token = default) + { + if (request == null) throw new ArgumentNullException(nameof(request)); + return await SendHttpRequest($"api/v1/stores/{storeId}/lightning/{cryptoCode}/invoices", request, HttpMethod.Post, token); } } diff --git a/BTCPayServer.Client/BTCPayServerClient.LightningAddresses.cs b/BTCPayServer.Client/BTCPayServerClient.LightningAddresses.cs index 524c671ca..20d485337 100644 --- a/BTCPayServer.Client/BTCPayServerClient.LightningAddresses.cs +++ b/BTCPayServer.Client/BTCPayServerClient.LightningAddresses.cs @@ -3,46 +3,32 @@ using System.Threading; using System.Threading.Tasks; using BTCPayServer.Client.Models; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + public virtual async Task GetStoreLightningAddresses(string storeId, + CancellationToken token = default) { - public virtual async Task GetStoreLightningAddresses(string storeId, - CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/lightning-addresses", - method: HttpMethod.Get), token); - return await HandleResponse(response); - } + return await SendHttpRequest($"api/v1/stores/{storeId}/lightning-addresses", null, HttpMethod.Get, token); + } - public virtual async Task GetStoreLightningAddress(string storeId, string username, - CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/lightning-addresses/{username}", - method: HttpMethod.Get), token); - return await HandleResponse(response); - } + public virtual async Task GetStoreLightningAddress(string storeId, string username, + CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/lightning-addresses/{username}", null, HttpMethod.Get, token); + } - public virtual async Task RemoveStoreLightningAddress(string storeId, string username, - CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/lightning-addresses/{username}", - method: HttpMethod.Delete), token); - await HandleResponse(response); - } + public virtual async Task RemoveStoreLightningAddress(string storeId, string username, + CancellationToken token = default) + { + await SendHttpRequest($"api/v1/stores/{storeId}/lightning-addresses/{username}", null, HttpMethod.Delete, token); + } - public virtual async Task AddOrUpdateStoreLightningAddress(string storeId, - string username, LightningAddressData data, - CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/lightning-addresses/{username}", - method: HttpMethod.Post, bodyPayload: data), token); - - return await HandleResponse(response); - } + public virtual async Task AddOrUpdateStoreLightningAddress(string storeId, + string username, LightningAddressData data, + CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/lightning-addresses/{username}", data, HttpMethod.Post, token); } } diff --git a/BTCPayServer.Client/BTCPayServerClient.Misc.cs b/BTCPayServer.Client/BTCPayServerClient.Misc.cs index 258628ca0..6515cb75c 100644 --- a/BTCPayServer.Client/BTCPayServerClient.Misc.cs +++ b/BTCPayServer.Client/BTCPayServerClient.Misc.cs @@ -1,23 +1,19 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Net.Http; using System.Threading; using System.Threading.Tasks; using BTCPayServer.Client.Models; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + public virtual async Task GetPermissionMetadata(CancellationToken token = default) { - public virtual async Task GetPermissionMetadata(CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest("misc/permissions"), token); - return await HandleResponse(response); - } - public virtual async Task GetAvailableLanguages(CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest("misc/lang"), token); - return await HandleResponse(response); - } + return await SendHttpRequest("misc/permissions", null, HttpMethod.Get, token); + } + + public virtual async Task GetAvailableLanguages(CancellationToken token = default) + { + return await SendHttpRequest("misc/lang", null, HttpMethod.Get, token); } } diff --git a/BTCPayServer.Client/BTCPayServerClient.Notifications.cs b/BTCPayServer.Client/BTCPayServerClient.Notifications.cs index 943e28560..2e156a41d 100644 --- a/BTCPayServer.Client/BTCPayServerClient.Notifications.cs +++ b/BTCPayServer.Client/BTCPayServerClient.Notifications.cs @@ -1,56 +1,40 @@ -using System; using System.Collections.Generic; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using BTCPayServer.Client.Models; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + public virtual async Task> GetNotifications(bool? seen = null, int? skip = null, + int? take = null, CancellationToken token = default) { - public virtual async Task> GetNotifications(bool? seen = null, int? skip = null, - int? take = null, CancellationToken token = default) - { - Dictionary queryPayload = new Dictionary(); + var queryPayload = new Dictionary(); + if (seen != null) + queryPayload.Add(nameof(seen), seen); + if (skip != null) + queryPayload.Add(nameof(skip), skip); + if (take != null) + queryPayload.Add(nameof(take), take); + return await SendHttpRequest>("api/v1/users/me/notifications", queryPayload, HttpMethod.Get, token); + } - if (seen != null) - queryPayload.Add(nameof(seen), seen); - if (skip != null) - queryPayload.Add(nameof(skip), skip); - if (take != null) - queryPayload.Add(nameof(take), take); + public virtual async Task GetNotification(string notificationId, + CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/users/me/notifications/{notificationId}", null, HttpMethod.Get, token); + } - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/users/me/notifications", - queryPayload), token); + public virtual async Task UpdateNotification(string notificationId, bool? seen, + CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/users/me/notifications/{notificationId}", new UpdateNotification { Seen = seen }, HttpMethod.Put, token); + } - return await HandleResponse>(response); - } - - public virtual async Task GetNotification(string notificationId, - CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/users/me/notifications/{notificationId}"), token); - return await HandleResponse(response); - } - - public virtual async Task UpdateNotification(string notificationId, bool? seen, - CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/users/me/notifications/{notificationId}", - method: HttpMethod.Put, bodyPayload: new UpdateNotification() { Seen = seen }), token); - return await HandleResponse(response); - } - - public virtual async Task RemoveNotification(string notificationId, CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/users/me/notifications/{notificationId}", - method: HttpMethod.Delete), token); - await HandleResponse(response); - } + public virtual async Task RemoveNotification(string notificationId, CancellationToken token = default) + { + await SendHttpRequest($"api/v1/users/me/notifications/{notificationId}", null, HttpMethod.Delete, token); } } diff --git a/BTCPayServer.Client/BTCPayServerClient.OnChainPaymentMethods.cs b/BTCPayServer.Client/BTCPayServerClient.OnChainPaymentMethods.cs index 99114fdab..341509cc4 100644 --- a/BTCPayServer.Client/BTCPayServerClient.OnChainPaymentMethods.cs +++ b/BTCPayServer.Client/BTCPayServerClient.OnChainPaymentMethods.cs @@ -5,45 +5,35 @@ using System.Threading.Tasks; using BTCPayServer.Client.Models; using Newtonsoft.Json.Linq; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient - { - public virtual async Task - PreviewProposedStoreOnChainPaymentMethodAddresses( - string storeId, string paymentMethodId, string derivationScheme, int offset = 0, - int amount = 10, - CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/{paymentMethodId}/preview", - bodyPayload: new UpdatePaymentMethodRequest() { Config = JValue.CreateString(derivationScheme) }, - queryPayload: new Dictionary() { { "offset", offset }, { "amount", amount } }, - method: HttpMethod.Post), token); - return await HandleResponse(response); - } + public virtual async Task + PreviewProposedStoreOnChainPaymentMethodAddresses( + string storeId, string paymentMethodId, string derivationScheme, int offset = 0, + int amount = 10, + CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/payment-methods/{paymentMethodId}/preview", + new Dictionary { { "offset", offset }, { "amount", amount } }, + new UpdatePaymentMethodRequest { Config = JValue.CreateString(derivationScheme) }, + HttpMethod.Post, token); + } - public virtual async Task PreviewStoreOnChainPaymentMethodAddresses( - string storeId, string paymentMethodId, int offset = 0, int amount = 10, - CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/{paymentMethodId}/preview", - queryPayload: new Dictionary() { { "offset", offset }, { "amount", amount } }, - method: HttpMethod.Get), token); - return await HandleResponse(response); - } + public virtual async Task PreviewStoreOnChainPaymentMethodAddresses( + string storeId, string paymentMethodId, int offset = 0, int amount = 10, + CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/payment-methods/{paymentMethodId}/preview", + new Dictionary { { "offset", offset }, { "amount", amount } }, HttpMethod.Get, token); + } - public virtual async Task GenerateOnChainWallet(string storeId, - string paymentMethodId, GenerateOnChainWalletRequest request, - CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/{paymentMethodId}/generate", - bodyPayload: request, - method: HttpMethod.Post), token); - return await HandleResponse(response); - } + public virtual async Task GenerateOnChainWallet(string storeId, + string paymentMethodId, GenerateOnChainWalletRequest request, + CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/payment-methods/{paymentMethodId}/generate", request, HttpMethod.Post, token); + } - } } diff --git a/BTCPayServer.Client/BTCPayServerClient.OnChainWallet.Objects.cs b/BTCPayServer.Client/BTCPayServerClient.OnChainWallet.Objects.cs index 700f13b00..859157f1f 100644 --- a/BTCPayServer.Client/BTCPayServerClient.OnChainWallet.Objects.cs +++ b/BTCPayServer.Client/BTCPayServerClient.OnChainWallet.Objects.cs @@ -1,82 +1,62 @@ -using System; using System.Collections.Generic; -using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using BTCPayServer.Client.Models; -using NBitcoin; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + public virtual async Task GetOnChainWalletObject(string storeId, string cryptoCode, OnChainWalletObjectId objectId, bool? includeNeighbourData = null, CancellationToken token = default) { - public virtual async Task GetOnChainWalletObject(string storeId, string cryptoCode, OnChainWalletObjectId objectId, bool? includeNeighbourData = null, CancellationToken token = default) + var parameters = new Dictionary(); + if (includeNeighbourData is bool v) + parameters.Add("includeNeighbourData", v); + try { - Dictionary parameters = new Dictionary(); - if (includeNeighbourData is bool v) - parameters.Add("includeNeighbourData", v); - var response = - await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/objects/{objectId.Type}/{objectId.Id}", parameters, method: HttpMethod.Get), token); - try - { - return await HandleResponse(response); - } - catch (GreenfieldAPIException err) when (err.APIError.Code == "wallet-object-not-found") - { - return null; - } + return await SendHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/objects/{objectId.Type}/{objectId.Id}", parameters, HttpMethod.Get, token); } - public virtual async Task GetOnChainWalletObjects(string storeId, string cryptoCode, GetWalletObjectsRequest query = null, CancellationToken token = default) + catch (GreenfieldAPIException err) when (err.APIError.Code == "wallet-object-not-found") { - Dictionary parameters = new Dictionary(); - if (query?.Type is string s) - parameters.Add("type", s); - if (query?.Ids is string[] ids) - parameters.Add("ids", ids); - if (query?.IncludeNeighbourData is bool v) - parameters.Add("includeNeighbourData", v); - var response = - await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/objects", parameters, method: HttpMethod.Get), token); - return await HandleResponse(response); - } - public virtual async Task RemoveOnChainWalletObject(string storeId, string cryptoCode, OnChainWalletObjectId objectId, - CancellationToken token = default) - { - var response = - await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/objects/{objectId.Type}/{objectId.Id}", method: HttpMethod.Delete), token); - await HandleResponse(response); - } - public virtual async Task AddOrUpdateOnChainWalletObject(string storeId, string cryptoCode, AddOnChainWalletObjectRequest request, - CancellationToken token = default) - { - var response = - await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/objects", method: HttpMethod.Post, bodyPayload: request), token); - return await HandleResponse(response); - } - public virtual async Task AddOrUpdateOnChainWalletLink(string storeId, string cryptoCode, - OnChainWalletObjectId objectId, - AddOnChainWalletObjectLinkRequest request = null, - CancellationToken token = default) - { - var response = - await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/objects/{objectId.Type}/{objectId.Id}/links", method: HttpMethod.Post, bodyPayload: request), token); - await HandleResponse(response); - } - public virtual async Task RemoveOnChainWalletLinks(string storeId, string cryptoCode, - OnChainWalletObjectId objectId, - OnChainWalletObjectId link, - CancellationToken token = default) - { - var response = - await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/objects/{objectId.Type}/{objectId.Id}/links/{link.Type}/{link.Id}", method: HttpMethod.Delete), token); - await HandleResponse(response); + return null; } } + public virtual async Task GetOnChainWalletObjects(string storeId, string cryptoCode, GetWalletObjectsRequest query = null, CancellationToken token = default) + { + Dictionary parameters = new Dictionary(); + if (query?.Type is string s) + parameters.Add("type", s); + if (query?.Ids is string[] ids) + parameters.Add("ids", ids); + if (query?.IncludeNeighbourData is bool v) + parameters.Add("includeNeighbourData", v); + return await SendHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/objects", parameters, HttpMethod.Get, token); + } + public virtual async Task RemoveOnChainWalletObject(string storeId, string cryptoCode, OnChainWalletObjectId objectId, + CancellationToken token = default) + { + await SendHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/objects/{objectId.Type}/{objectId.Id}", null, HttpMethod.Delete, token); + } + public virtual async Task AddOrUpdateOnChainWalletObject(string storeId, string cryptoCode, AddOnChainWalletObjectRequest request, + CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/objects", request, HttpMethod.Post, token); + } + + public virtual async Task AddOrUpdateOnChainWalletLink(string storeId, string cryptoCode, + OnChainWalletObjectId objectId, + AddOnChainWalletObjectLinkRequest request = null, + CancellationToken token = default) + { + await SendHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/objects/{objectId.Type}/{objectId.Id}/links", request, HttpMethod.Post, token); + } + + public virtual async Task RemoveOnChainWalletLinks(string storeId, string cryptoCode, + OnChainWalletObjectId objectId, + OnChainWalletObjectId link, + CancellationToken token = default) + { + await SendHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/objects/{objectId.Type}/{objectId.Id}/links/{link.Type}/{link.Id}", null, HttpMethod.Delete, token); + } } diff --git a/BTCPayServer.Client/BTCPayServerClient.OnChainWallet.cs b/BTCPayServer.Client/BTCPayServerClient.OnChainWallet.cs index e34cc6d95..f37e056f4 100644 --- a/BTCPayServer.Client/BTCPayServerClient.OnChainWallet.cs +++ b/BTCPayServer.Client/BTCPayServerClient.OnChainWallet.cs @@ -7,138 +7,105 @@ using System.Threading.Tasks; using BTCPayServer.Client.Models; using NBitcoin; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + public virtual async Task ShowOnChainWalletOverview(string storeId, string cryptoCode, + CancellationToken token = default) { - public virtual async Task ShowOnChainWalletOverview(string storeId, string cryptoCode, - CancellationToken token = default) + return await SendHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet", null, HttpMethod.Get, token); + } + public virtual async Task GetOnChainFeeRate(string storeId, string cryptoCode, int? blockTarget = null, + CancellationToken token = default) + { + var queryParams = new Dictionary(); + if (blockTarget != null) { - var response = - await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet"), token); - return await HandleResponse(response); - } - public virtual async Task GetOnChainFeeRate(string storeId, string cryptoCode, int? blockTarget = null, - CancellationToken token = default) - { - Dictionary queryParams = new Dictionary(); - if (blockTarget != null) - { - queryParams.Add("blockTarget", blockTarget); - } - var response = - await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/feeRate", queryParams), token); - return await HandleResponse(response); + queryParams.Add("blockTarget", blockTarget); } + return await SendHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/feeRate", queryParams, HttpMethod.Get, token); + } - public virtual async Task GetOnChainWalletReceiveAddress(string storeId, string cryptoCode, bool forceGenerate = false, - CancellationToken token = default) + public virtual async Task GetOnChainWalletReceiveAddress(string storeId, string cryptoCode, bool forceGenerate = false, + CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/address", new Dictionary { - var response = - await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/address", new Dictionary() - { - {"forceGenerate", forceGenerate} - }), token); - return await HandleResponse(response); - } + {"forceGenerate", forceGenerate} + }, HttpMethod.Get, token); + } - public virtual async Task UnReserveOnChainWalletReceiveAddress(string storeId, string cryptoCode, - CancellationToken token = default) - { - var response = - await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/address", method: HttpMethod.Delete), token); - await HandleResponse(response); - } + public virtual async Task UnReserveOnChainWalletReceiveAddress(string storeId, string cryptoCode, + CancellationToken token = default) + { + await SendHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/address", null, HttpMethod.Delete, token); + } - public virtual async Task> ShowOnChainWalletTransactions( - string storeId, string cryptoCode, TransactionStatus[] statusFilter = null, string labelFilter = null, int skip = 0, - CancellationToken token = default) + public virtual async Task> ShowOnChainWalletTransactions( + string storeId, string cryptoCode, TransactionStatus[] statusFilter = null, string labelFilter = null, int skip = 0, + CancellationToken token = default) + { + var query = new Dictionary(); + if (statusFilter?.Any() is true) { - var query = new Dictionary(); - if (statusFilter?.Any() is true) - { - query.Add(nameof(statusFilter), statusFilter); - } - if (labelFilter != null) - { - query.Add(nameof(labelFilter), labelFilter); - } - if (skip != 0) - { - query.Add(nameof(skip), skip); - } - var response = - await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/transactions", query), token); - return await HandleResponse>(response); + query.Add(nameof(statusFilter), statusFilter); } + if (labelFilter != null) + { + query.Add(nameof(labelFilter), labelFilter); + } + if (skip != 0) + { + query.Add(nameof(skip), skip); + } + return await SendHttpRequest>($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/transactions", query, HttpMethod.Get, token); + } - public virtual async Task GetOnChainWalletTransaction( - string storeId, string cryptoCode, string transactionId, - CancellationToken token = default) - { - var response = - await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/transactions/{transactionId}"), token); - return await HandleResponse(response); - } + public virtual async Task GetOnChainWalletTransaction( + string storeId, string cryptoCode, string transactionId, + CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/transactions/{transactionId}", null, HttpMethod.Get, token); + } - public virtual async Task PatchOnChainWalletTransaction( - string storeId, string cryptoCode, string transactionId, - PatchOnChainTransactionRequest request, - bool force = false, CancellationToken token = default) - { - var response = - await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/transactions/{transactionId}", queryPayload: new Dictionary() - { - {"force", force} - }, bodyPayload: request, HttpMethod.Patch), token); - return await HandleResponse(response); - } + public virtual async Task PatchOnChainWalletTransaction( + string storeId, string cryptoCode, string transactionId, + PatchOnChainTransactionRequest request, + bool force = false, CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/transactions/{transactionId}", + new Dictionary { {"force", force} }, request, HttpMethod.Patch, token); + } - public virtual async Task> GetOnChainWalletUTXOs(string storeId, - string cryptoCode, - CancellationToken token = default) - { - var response = - await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/utxos"), token); - return await HandleResponse>(response); - } + public virtual async Task> GetOnChainWalletUTXOs(string storeId, + string cryptoCode, + CancellationToken token = default) + { + return await SendHttpRequest>($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/utxos", null, HttpMethod.Get, token); + } - public virtual async Task CreateOnChainTransaction(string storeId, - string cryptoCode, CreateOnChainTransactionRequest request, - CancellationToken token = default) + public virtual async Task CreateOnChainTransaction(string storeId, + string cryptoCode, CreateOnChainTransactionRequest request, + CancellationToken token = default) + { + if (!request.ProceedWithBroadcast) { - if (!request.ProceedWithBroadcast) - { - throw new ArgumentOutOfRangeException(nameof(request.ProceedWithBroadcast), - "Please use CreateOnChainTransactionButDoNotBroadcast when wanting to only create the transaction"); - } - var response = - await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/transactions", null, request, HttpMethod.Post), token); - return await HandleResponse(response); + throw new ArgumentOutOfRangeException(nameof(request.ProceedWithBroadcast), + "Please use CreateOnChainTransactionButDoNotBroadcast when wanting to only create the transaction"); } + return await SendHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/transactions", request, HttpMethod.Post, token); + } - public virtual async Task CreateOnChainTransactionButDoNotBroadcast(string storeId, - string cryptoCode, CreateOnChainTransactionRequest request, Network network, - CancellationToken token = default) + public virtual async Task CreateOnChainTransactionButDoNotBroadcast(string storeId, + string cryptoCode, CreateOnChainTransactionRequest request, Network network, + CancellationToken token = default) + { + if (request.ProceedWithBroadcast) { - if (request.ProceedWithBroadcast) - { - throw new ArgumentOutOfRangeException(nameof(request.ProceedWithBroadcast), - "Please use CreateOnChainTransaction when wanting to also broadcast the transaction"); - } - var response = - await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/transactions", null, request, HttpMethod.Post), token); - return Transaction.Parse(await HandleResponse(response), network); + throw new ArgumentOutOfRangeException(nameof(request.ProceedWithBroadcast), + "Please use CreateOnChainTransaction when wanting to also broadcast the transaction"); } + return Transaction.Parse(await SendHttpRequest($"api/v1/stores/{storeId}/payment-methods/onchain/{cryptoCode}/wallet/transactions", request, HttpMethod.Post, token), network); } } diff --git a/BTCPayServer.Client/BTCPayServerClient.PaymentRequests.cs b/BTCPayServer.Client/BTCPayServerClient.PaymentRequests.cs index 96e2a6d51..547d8d349 100644 --- a/BTCPayServer.Client/BTCPayServerClient.PaymentRequests.cs +++ b/BTCPayServer.Client/BTCPayServerClient.PaymentRequests.cs @@ -5,72 +5,49 @@ using System.Threading; using System.Threading.Tasks; using BTCPayServer.Client.Models; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + public virtual async Task> GetPaymentRequests(string storeId, + bool includeArchived = false, + CancellationToken token = default) { - public virtual async Task> GetPaymentRequests(string storeId, - bool includeArchived = false, - CancellationToken token = default) - { - var response = - await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-requests", - new Dictionary() { { nameof(includeArchived), includeArchived } }), token); - return await HandleResponse>(response); - } + return await SendHttpRequest>($"api/v1/stores/{storeId}/payment-requests", + new Dictionary { { nameof(includeArchived), includeArchived } }, HttpMethod.Get, token); + } - public virtual async Task GetPaymentRequest(string storeId, string paymentRequestId, - CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-requests/{paymentRequestId}"), token); - return await HandleResponse(response); - } + public virtual async Task GetPaymentRequest(string storeId, string paymentRequestId, + CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/payment-requests/{paymentRequestId}", null, HttpMethod.Get, token); + } - public virtual async Task ArchivePaymentRequest(string storeId, string paymentRequestId, - CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-requests/{paymentRequestId}", - method: HttpMethod.Delete), token); - await HandleResponse(response); - } + public virtual async Task ArchivePaymentRequest(string storeId, string paymentRequestId, + CancellationToken token = default) + { + await SendHttpRequest($"api/v1/stores/{storeId}/payment-requests/{paymentRequestId}", null, HttpMethod.Delete, token); + } - public virtual async Task PayPaymentRequest(string storeId, string paymentRequestId, PayPaymentRequestRequest request, CancellationToken token = default) - { - if (request == null) - throw new ArgumentNullException(nameof(request)); - if (storeId is null) - throw new ArgumentNullException(nameof(storeId)); - if (paymentRequestId is null) - throw new ArgumentNullException(nameof(paymentRequestId)); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-requests/{paymentRequestId}/pay", bodyPayload: request, - method: HttpMethod.Post), token); - return await HandleResponse(response); - } + public virtual async Task PayPaymentRequest(string storeId, string paymentRequestId, PayPaymentRequestRequest request, CancellationToken token = default) + { + if (request == null) throw new ArgumentNullException(nameof(request)); + if (storeId is null) throw new ArgumentNullException(nameof(storeId)); + if (paymentRequestId is null) throw new ArgumentNullException(nameof(paymentRequestId)); + return await SendHttpRequest($"api/v1/stores/{storeId}/payment-requests/{paymentRequestId}/pay", request, HttpMethod.Post, token); + } - public virtual async Task CreatePaymentRequest(string storeId, - CreatePaymentRequestRequest request, CancellationToken token = default) - { - if (request == null) - throw new ArgumentNullException(nameof(request)); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-requests", bodyPayload: request, - method: HttpMethod.Post), token); - return await HandleResponse(response); - } + public virtual async Task CreatePaymentRequest(string storeId, + CreatePaymentRequestRequest request, CancellationToken token = default) + { + if (request == null) throw new ArgumentNullException(nameof(request)); + return await SendHttpRequest($"api/v1/stores/{storeId}/payment-requests", request, HttpMethod.Post, token); + } - public virtual async Task UpdatePaymentRequest(string storeId, string paymentRequestId, - UpdatePaymentRequestRequest request, CancellationToken token = default) - { - if (request == null) - throw new ArgumentNullException(nameof(request)); - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-requests/{paymentRequestId}", bodyPayload: request, - method: HttpMethod.Put), token); - return await HandleResponse(response); - } + public virtual async Task UpdatePaymentRequest(string storeId, string paymentRequestId, + UpdatePaymentRequestRequest request, CancellationToken token = default) + { + if (request == null) throw new ArgumentNullException(nameof(request)); + return await SendHttpRequest($"api/v1/stores/{storeId}/payment-requests/{paymentRequestId}", request, HttpMethod.Put, token); } } diff --git a/BTCPayServer.Client/BTCPayServerClient.PayoutProcessors.cs b/BTCPayServer.Client/BTCPayServerClient.PayoutProcessors.cs index 128ce8cee..857e067e8 100644 --- a/BTCPayServer.Client/BTCPayServerClient.PayoutProcessors.cs +++ b/BTCPayServer.Client/BTCPayServerClient.PayoutProcessors.cs @@ -1,18 +1,16 @@ #nullable enable using System.Collections.Generic; +using System.Net.Http; using System.Threading; using System.Threading.Tasks; using BTCPayServer.Client.Models; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + public virtual async Task> GetPayoutProcessors(CancellationToken token = default) { - public virtual async Task> GetPayoutProcessors( - CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest("api/v1/payout-processors"), token); - return await HandleResponse>(response); - } + return await SendHttpRequest>("api/v1/payout-processors", null, HttpMethod.Get, token); } } diff --git a/BTCPayServer.Client/BTCPayServerClient.PullPayments.cs b/BTCPayServer.Client/BTCPayServerClient.PullPayments.cs index 282500383..884be97f5 100644 --- a/BTCPayServer.Client/BTCPayServerClient.PullPayments.cs +++ b/BTCPayServer.Client/BTCPayServerClient.PullPayments.cs @@ -5,113 +5,90 @@ using System.Threading.Tasks; using System.Web; using BTCPayServer.Client.Models; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + public virtual async Task CreatePullPayment(string storeId, CreatePullPaymentRequest request, CancellationToken cancellationToken = default) { - public virtual async Task CreatePullPayment(string storeId, CreatePullPaymentRequest request, CancellationToken cancellationToken = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{HttpUtility.UrlEncode(storeId)}/pull-payments", bodyPayload: request, method: HttpMethod.Post), cancellationToken); - return await HandleResponse(response); - } - public virtual async Task GetPullPayment(string pullPaymentId, CancellationToken cancellationToken = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/pull-payments/{HttpUtility.UrlEncode(pullPaymentId)}", method: HttpMethod.Get), cancellationToken); - return await HandleResponse(response); - } + return await SendHttpRequest($"api/v1/stores/{HttpUtility.UrlEncode(storeId)}/pull-payments", request, HttpMethod.Post, cancellationToken); + } - public virtual async Task RegisterBoltcard(string pullPaymentId, RegisterBoltcardRequest request, CancellationToken cancellationToken = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/pull-payments/{HttpUtility.UrlEncode(pullPaymentId)}/boltcards", bodyPayload: request, method: HttpMethod.Post), cancellationToken); - return await HandleResponse(response); - } + public virtual async Task GetPullPayment(string pullPaymentId, CancellationToken cancellationToken = default) + { + return await SendHttpRequest($"api/v1/pull-payments/{HttpUtility.UrlEncode(pullPaymentId)}", null, HttpMethod.Get, cancellationToken); + } - public virtual async Task GetPullPayments(string storeId, bool includeArchived = false, CancellationToken cancellationToken = default) - { - Dictionary query = new Dictionary(); - query.Add("includeArchived", includeArchived); - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{HttpUtility.UrlEncode(storeId)}/pull-payments", queryPayload: query, method: HttpMethod.Get), cancellationToken); - return await HandleResponse(response); - } + public virtual async Task RegisterBoltcard(string pullPaymentId, RegisterBoltcardRequest request, CancellationToken cancellationToken = default) + { + return await SendHttpRequest($"api/v1/pull-payments/{HttpUtility.UrlEncode(pullPaymentId)}/boltcards", request, HttpMethod.Post, cancellationToken); + } - public virtual async Task ArchivePullPayment(string storeId, string pullPaymentId, CancellationToken cancellationToken = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{HttpUtility.UrlEncode(storeId)}/pull-payments/{HttpUtility.UrlEncode(pullPaymentId)}", method: HttpMethod.Delete), cancellationToken); - await HandleResponse(response); - } + public virtual async Task GetPullPayments(string storeId, bool includeArchived = false, CancellationToken cancellationToken = default) + { + var query = new Dictionary { { "includeArchived", includeArchived } }; + return await SendHttpRequest($"api/v1/stores/{HttpUtility.UrlEncode(storeId)}/pull-payments", query, HttpMethod.Get, cancellationToken); + } - public virtual async Task GetPayouts(string pullPaymentId, bool includeCancelled = false, CancellationToken cancellationToken = default) - { - Dictionary query = new Dictionary(); - query.Add("includeCancelled", includeCancelled); - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/pull-payments/{HttpUtility.UrlEncode(pullPaymentId)}/payouts", queryPayload: query, method: HttpMethod.Get), cancellationToken); - return await HandleResponse(response); - } - public virtual async Task GetStorePayouts(string storeId, bool includeCancelled = false, CancellationToken cancellationToken = default) - { - Dictionary query = new Dictionary(); - query.Add("includeCancelled", includeCancelled); - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/payouts", queryPayload: query, method: HttpMethod.Get), cancellationToken); - return await HandleResponse(response); - } - public virtual async Task CreatePayout(string pullPaymentId, CreatePayoutRequest payoutRequest, CancellationToken cancellationToken = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/pull-payments/{HttpUtility.UrlEncode(pullPaymentId)}/payouts", bodyPayload: payoutRequest, method: HttpMethod.Post), cancellationToken); - return await HandleResponse(response); - } - public virtual async Task GetPullPaymentPayout(string pullPaymentId, string payoutId, CancellationToken cancellationToken = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/pull-payments/{HttpUtility.UrlEncode(pullPaymentId)}/payouts/{payoutId}", method: HttpMethod.Get), cancellationToken); - return await HandleResponse(response); - } - public virtual async Task GetStorePayout(string storeId, string payoutId, CancellationToken cancellationToken = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/payouts/{payoutId}", method: HttpMethod.Get), cancellationToken); - return await HandleResponse(response); - } - public virtual async Task CreatePayout(string storeId, CreatePayoutThroughStoreRequest payoutRequest, CancellationToken cancellationToken = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/payouts", bodyPayload: payoutRequest, method: HttpMethod.Post), cancellationToken); - return await HandleResponse(response); - } - public virtual async Task CancelPayout(string storeId, string payoutId, CancellationToken cancellationToken = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{HttpUtility.UrlEncode(storeId)}/payouts/{HttpUtility.UrlEncode(payoutId)}", method: HttpMethod.Delete), cancellationToken); - await HandleResponse(response); - } - public virtual async Task ApprovePayout(string storeId, string payoutId, ApprovePayoutRequest request, CancellationToken cancellationToken = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{HttpUtility.UrlEncode(storeId)}/payouts/{HttpUtility.UrlEncode(payoutId)}", bodyPayload: request, method: HttpMethod.Post), cancellationToken); - return await HandleResponse(response); - } + public virtual async Task ArchivePullPayment(string storeId, string pullPaymentId, CancellationToken cancellationToken = default) + { + await SendHttpRequest($"api/v1/stores/{HttpUtility.UrlEncode(storeId)}/pull-payments/{HttpUtility.UrlEncode(pullPaymentId)}", null, HttpMethod.Delete, cancellationToken); + } - public virtual async Task MarkPayoutPaid(string storeId, string payoutId, - CancellationToken cancellationToken = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest( - $"api/v1/stores/{HttpUtility.UrlEncode(storeId)}/payouts/{HttpUtility.UrlEncode(payoutId)}/mark-paid", - method: HttpMethod.Post), cancellationToken); - await HandleResponse(response); - } - public virtual async Task MarkPayout(string storeId, string payoutId, MarkPayoutRequest request, - CancellationToken cancellationToken = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest( - $"api/v1/stores/{HttpUtility.UrlEncode(storeId)}/payouts/{HttpUtility.UrlEncode(payoutId)}/mark", - method: HttpMethod.Post, bodyPayload: request), cancellationToken); - await HandleResponse(response); - } + public virtual async Task GetPayouts(string pullPaymentId, bool includeCancelled = false, CancellationToken cancellationToken = default) + { + var query = new Dictionary { { "includeCancelled", includeCancelled } }; + return await SendHttpRequest($"api/v1/pull-payments/{HttpUtility.UrlEncode(pullPaymentId)}/payouts", query, HttpMethod.Get, cancellationToken); + } - public virtual async Task GetPullPaymentLNURL(string pullPaymentId, - CancellationToken cancellationToken = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest( - $"/api/v1/pull-payments/{pullPaymentId}/lnurl", - method: HttpMethod.Get), cancellationToken); - return await HandleResponse(response); - } + public virtual async Task GetStorePayouts(string storeId, bool includeCancelled = false, CancellationToken cancellationToken = default) + { + var query = new Dictionary { { "includeCancelled", includeCancelled } }; + return await SendHttpRequest($"api/v1/stores/{storeId}/payouts", queryPayload: query, method: HttpMethod.Get, cancellationToken); + } + + public virtual async Task CreatePayout(string pullPaymentId, CreatePayoutRequest payoutRequest, CancellationToken cancellationToken = default) + { + return await SendHttpRequest($"api/v1/pull-payments/{HttpUtility.UrlEncode(pullPaymentId)}/payouts", bodyPayload: payoutRequest, HttpMethod.Post, cancellationToken); + } + + public virtual async Task GetPullPaymentPayout(string pullPaymentId, string payoutId, CancellationToken cancellationToken = default) + { + return await SendHttpRequest($"api/v1/pull-payments/{HttpUtility.UrlEncode(pullPaymentId)}/payouts/{payoutId}", null, HttpMethod.Get, cancellationToken); + } + + public virtual async Task GetStorePayout(string storeId, string payoutId, CancellationToken cancellationToken = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/payouts/{payoutId}", null, HttpMethod.Get, cancellationToken); + } + + public virtual async Task CreatePayout(string storeId, CreatePayoutThroughStoreRequest payoutRequest, CancellationToken cancellationToken = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/payouts", bodyPayload: payoutRequest, method: HttpMethod.Post, cancellationToken); + } + + public virtual async Task CancelPayout(string storeId, string payoutId, CancellationToken cancellationToken = default) + { + await SendHttpRequest($"api/v1/stores/{HttpUtility.UrlEncode(storeId)}/payouts/{HttpUtility.UrlEncode(payoutId)}", null, HttpMethod.Delete, cancellationToken); + } + + public virtual async Task ApprovePayout(string storeId, string payoutId, ApprovePayoutRequest request, CancellationToken cancellationToken = default) + { + return await SendHttpRequest($"api/v1/stores/{HttpUtility.UrlEncode(storeId)}/payouts/{HttpUtility.UrlEncode(payoutId)}", request, HttpMethod.Post, cancellationToken); + } + + public virtual async Task MarkPayoutPaid(string storeId, string payoutId, CancellationToken cancellationToken = default) + { + await SendHttpRequest($"api/v1/stores/{HttpUtility.UrlEncode(storeId)}/payouts/{HttpUtility.UrlEncode(payoutId)}/mark-paid", null, HttpMethod.Post, cancellationToken); + } + + public virtual async Task MarkPayout(string storeId, string payoutId, MarkPayoutRequest request, CancellationToken cancellationToken = default) + { + await SendHttpRequest($"api/v1/stores/{HttpUtility.UrlEncode(storeId)}/payouts/{HttpUtility.UrlEncode(payoutId)}/mark", request, HttpMethod.Post, cancellationToken); + } + + public virtual async Task GetPullPaymentLNURL(string pullPaymentId, CancellationToken cancellationToken = default) + { + return await SendHttpRequest($"/api/v1/pull-payments/{pullPaymentId}/lnurl", null, HttpMethod.Get, cancellationToken); } } diff --git a/BTCPayServer.Client/BTCPayServerClient.ServerInfo.cs b/BTCPayServer.Client/BTCPayServerClient.ServerInfo.cs index 333f89cce..d8fd4c324 100644 --- a/BTCPayServer.Client/BTCPayServerClient.ServerInfo.cs +++ b/BTCPayServer.Client/BTCPayServerClient.ServerInfo.cs @@ -1,22 +1,20 @@ using System.Collections.Generic; +using System.Net.Http; using System.Threading; using System.Threading.Tasks; using BTCPayServer.Client.Models; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + public virtual async Task GetServerInfo(CancellationToken token = default) { - public virtual async Task GetServerInfo(CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest("api/v1/server/info"), token); - return await HandleResponse(response); - } + return await SendHttpRequest("api/v1/server/info", null, HttpMethod.Get, token); + } - public virtual async Task> GetServerRoles(CancellationToken token = default) - { - using var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/server/roles"), token); - return await HandleResponse>(response); - } + public virtual async Task> GetServerRoles(CancellationToken token = default) + { + return await SendHttpRequest>("api/v1/server/roles", null, HttpMethod.Get, token); } } diff --git a/BTCPayServer.Client/BTCPayServerClient.StoreEmail.cs b/BTCPayServer.Client/BTCPayServerClient.StoreEmail.cs index 7932c4864..023cc93f0 100644 --- a/BTCPayServer.Client/BTCPayServerClient.StoreEmail.cs +++ b/BTCPayServer.Client/BTCPayServerClient.StoreEmail.cs @@ -3,35 +3,22 @@ using System.Threading; using System.Threading.Tasks; using BTCPayServer.Client.Models; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + public virtual async Task GetStoreEmailSettings(string storeId, CancellationToken token = default) { - public virtual async Task GetStoreEmailSettings(string storeId, - CancellationToken token = default) - { - using var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/email", method: HttpMethod.Get), - token); - return await HandleResponse(response); - } + return await SendHttpRequest($"api/v1/stores/{storeId}/email", null, HttpMethod.Get, token); + } - public virtual async Task UpdateStoreEmailSettings(string storeId, EmailSettingsData request, - CancellationToken token = default) - { - using var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/email", bodyPayload: request, method: HttpMethod.Put), - token); - return await HandleResponse(response); - } + public virtual async Task UpdateStoreEmailSettings(string storeId, EmailSettingsData request, CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/email", request, method: HttpMethod.Put, token); + } - public virtual async Task SendEmail(string storeId, SendEmailRequest request, - CancellationToken token = default) - { - using var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/email/send", bodyPayload: request, method: HttpMethod.Post), - token); - await HandleResponse(response); - } + public virtual async Task SendEmail(string storeId, SendEmailRequest request, CancellationToken token = default) + { + await SendHttpRequest($"api/v1/stores/{storeId}/email/send", request, HttpMethod.Post, token); } } diff --git a/BTCPayServer.Client/BTCPayServerClient.StorePaymentMethods.cs b/BTCPayServer.Client/BTCPayServerClient.StorePaymentMethods.cs index a3a057ae9..84541b841 100644 --- a/BTCPayServer.Client/BTCPayServerClient.StorePaymentMethods.cs +++ b/BTCPayServer.Client/BTCPayServerClient.StorePaymentMethods.cs @@ -4,64 +4,42 @@ using System.Threading; using System.Threading.Tasks; using BTCPayServer.Client.Models; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + public virtual async Task UpdateStorePaymentMethod(string storeId, string paymentMethodId, UpdatePaymentMethodRequest request, CancellationToken token = default) { - public virtual async Task UpdateStorePaymentMethod( - string storeId, - string paymentMethodId, - UpdatePaymentMethodRequest request, - CancellationToken token = default) + return await SendHttpRequest($"api/v1/stores/{storeId}/payment-methods/{paymentMethodId}", request, HttpMethod.Put, token); + } + + public virtual async Task RemoveStorePaymentMethod(string storeId, string paymentMethodId) + { + await SendHttpRequest($"api/v1/stores/{storeId}/payment-methods/{paymentMethodId}", null, HttpMethod.Delete, CancellationToken.None); + } + + public virtual async Task GetStorePaymentMethod(string storeId, string paymentMethodId, bool? includeConfig = null, CancellationToken token = default) + { + var query = new Dictionary(); + if (includeConfig != null) { - var response = - await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/{paymentMethodId}", bodyPayload: request, method: HttpMethod.Put), - token); - return await HandleResponse(response); + query.Add(nameof(includeConfig), includeConfig); } - public virtual async Task RemoveStorePaymentMethod(string storeId, string paymentMethodId) + return await SendHttpRequest($"api/v1/stores/{storeId}/payment-methods/{paymentMethodId}", query, HttpMethod.Get, token); + } + + public virtual async Task GetStorePaymentMethods(string storeId, bool? onlyEnabled = null, bool? includeConfig = null, CancellationToken token = default) + { + var query = new Dictionary(); + if (onlyEnabled != null) { - var response = - await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/{paymentMethodId}", method: HttpMethod.Delete), - CancellationToken.None); - await HandleResponse(response); + query.Add(nameof(onlyEnabled), onlyEnabled); + } + if (includeConfig != null) + { + query.Add(nameof(includeConfig), includeConfig); } - public virtual async Task GetStorePaymentMethod(string storeId, - string paymentMethodId, bool? includeConfig = null, CancellationToken token = default) - { - var query = new Dictionary(); - if (includeConfig != null) - { - query.Add(nameof(includeConfig), includeConfig); - } - - var response = - await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods/{paymentMethodId}", - query), token); - return await HandleResponse(response); - } - public virtual async Task GetStorePaymentMethods(string storeId, - bool? onlyEnabled = null, bool? includeConfig = null, CancellationToken token = default) - { - var query = new Dictionary(); - if (onlyEnabled != null) - { - query.Add(nameof(onlyEnabled), onlyEnabled); - } - if (includeConfig != null) - { - query.Add(nameof(includeConfig), includeConfig); - } - - var response = - await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/payment-methods", - query), token); - return await HandleResponse(response); - } + return await SendHttpRequest($"api/v1/stores/{storeId}/payment-methods", query, HttpMethod.Get, token); } } diff --git a/BTCPayServer.Client/BTCPayServerClient.StorePayoutProcessors.cs b/BTCPayServer.Client/BTCPayServerClient.StorePayoutProcessors.cs index 02317c78d..175f717b2 100644 --- a/BTCPayServer.Client/BTCPayServerClient.StorePayoutProcessors.cs +++ b/BTCPayServer.Client/BTCPayServerClient.StorePayoutProcessors.cs @@ -5,44 +5,37 @@ using System.Threading; using System.Threading.Tasks; using BTCPayServer.Client.Models; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + public virtual async Task> GetPayoutProcessors(string storeId, CancellationToken token = default) { - public virtual async Task> GetPayoutProcessors(string storeId, - CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/payout-processors"), token); - return await HandleResponse>(response); - } - public virtual async Task RemovePayoutProcessor(string storeId, string processor, string paymentMethod, CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/payout-processors/{processor}/{paymentMethod}", null, HttpMethod.Delete), token); - await HandleResponse(response); - } + return await SendHttpRequest>($"api/v1/stores/{storeId}/payout-processors", null, HttpMethod.Get, token); + } - public virtual async Task> GetStoreLightningAutomatedPayoutProcessors(string storeId, string? paymentMethod = null, - CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/payout-processors/LightningAutomatedPayoutSenderFactory{(paymentMethod is null ? string.Empty : $"/{paymentMethod}")}"), token); - return await HandleResponse>(response); - } - public virtual async Task UpdateStoreLightningAutomatedPayoutProcessors(string storeId, string paymentMethod, LightningAutomatedPayoutSettings request, CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/payout-processors/LightningAutomatedPayoutSenderFactory/{paymentMethod}", null, request, HttpMethod.Put), token); - return await HandleResponse(response); - } - public virtual async Task UpdateStoreOnChainAutomatedPayoutProcessors(string storeId, string paymentMethod, OnChainAutomatedPayoutSettings request, CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/payout-processors/OnChainAutomatedPayoutSenderFactory/{paymentMethod}", null, request, HttpMethod.Put), token); - return await HandleResponse(response); - } + public virtual async Task RemovePayoutProcessor(string storeId, string processor, string paymentMethod, CancellationToken token = default) + { + await SendHttpRequest($"api/v1/stores/{storeId}/payout-processors/{processor}/{paymentMethod}", null, HttpMethod.Delete, token); + } - public virtual async Task> GetStoreOnChainAutomatedPayoutProcessors(string storeId, string? paymentMethod = null, - CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/payout-processors/OnChainAutomatedPayoutSenderFactory{(paymentMethod is null ? string.Empty : $"/{paymentMethod}")}"), token); - return await HandleResponse>(response); - } + public virtual async Task> GetStoreLightningAutomatedPayoutProcessors(string storeId, string? paymentMethod = null, CancellationToken token = default) + { + return await SendHttpRequest>($"api/v1/stores/{storeId}/payout-processors/LightningAutomatedPayoutSenderFactory{(paymentMethod is null ? string.Empty : $"/{paymentMethod}")}", null, HttpMethod.Get, token); + } + + public virtual async Task UpdateStoreLightningAutomatedPayoutProcessors(string storeId, string paymentMethod, LightningAutomatedPayoutSettings request, CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/payout-processors/LightningAutomatedPayoutSenderFactory/{paymentMethod}", request, HttpMethod.Put, token); + } + + public virtual async Task UpdateStoreOnChainAutomatedPayoutProcessors(string storeId, string paymentMethod, OnChainAutomatedPayoutSettings request, CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/payout-processors/OnChainAutomatedPayoutSenderFactory/{paymentMethod}", request, HttpMethod.Put, token); + } + + public virtual async Task> GetStoreOnChainAutomatedPayoutProcessors(string storeId, string? paymentMethod = null, CancellationToken token = default) + { + return await SendHttpRequest>($"api/v1/stores/{storeId}/payout-processors/OnChainAutomatedPayoutSenderFactory{(paymentMethod is null ? string.Empty : $"/{paymentMethod}")}", null, HttpMethod.Get, token); } } diff --git a/BTCPayServer.Client/BTCPayServerClient.StoreRatesConfiguration.cs b/BTCPayServer.Client/BTCPayServerClient.StoreRatesConfiguration.cs index 1701ef189..0848bfb18 100644 --- a/BTCPayServer.Client/BTCPayServerClient.StoreRatesConfiguration.cs +++ b/BTCPayServer.Client/BTCPayServerClient.StoreRatesConfiguration.cs @@ -4,61 +4,34 @@ using System.Threading; using System.Threading.Tasks; using BTCPayServer.Client.Models; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + public virtual async Task GetStoreRateConfiguration(string storeId, CancellationToken token = default) { - public virtual async Task GetStoreRateConfiguration(string storeId, - CancellationToken token = default) - { - using var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/rates/configuration", method: HttpMethod.Get), - token); - return await HandleResponse(response); - } + return await SendHttpRequest($"api/v1/stores/{storeId}/rates/configuration", null, HttpMethod.Get, token); + } - public virtual async Task> GetRateSources( - CancellationToken token = default) - { - using var response = await _httpClient.SendAsync( - CreateHttpRequest($"misc/rate-sources", method: HttpMethod.Get), - token); - return await HandleResponse>(response); - } + public virtual async Task> GetRateSources(CancellationToken token = default) + { + return await SendHttpRequest>("misc/rate-sources", null, HttpMethod.Get, token); + } - public virtual async Task UpdateStoreRateConfiguration(string storeId, - StoreRateConfiguration request, - CancellationToken token = default) - { - using var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/rates/configuration", bodyPayload: request, - method: HttpMethod.Put), - token); - return await HandleResponse(response); - } + public virtual async Task UpdateStoreRateConfiguration(string storeId, StoreRateConfiguration request, CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/rates/configuration", request, HttpMethod.Put, token); + } - public virtual async Task> PreviewUpdateStoreRateConfiguration(string storeId, - StoreRateConfiguration request, - string[] currencyPair, - CancellationToken token = default) - { - using var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/rates/configuration/preview", bodyPayload: request, - queryPayload: new Dictionary() { { "currencyPair", currencyPair } }, - method: HttpMethod.Post), - token); - return await HandleResponse>(response); - } + public virtual async Task> PreviewUpdateStoreRateConfiguration(string storeId, StoreRateConfiguration request, string[] currencyPair = null, CancellationToken token = default) + { + var queryPayload = currencyPair == null ? null : new Dictionary { { "currencyPair", currencyPair } }; + return await SendHttpRequest>($"api/v1/stores/{storeId}/rates/configuration/preview", queryPayload, request, HttpMethod.Post, token); + } - public virtual async Task> GetStoreRates(string storeId, string[] currencyPair, - CancellationToken token = default) - { - using var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/rates", - queryPayload: new Dictionary() { { "currencyPair", currencyPair } }, - method: HttpMethod.Get), - token); - return await HandleResponse>(response); - } + public virtual async Task> GetStoreRates(string storeId, string[] currencyPair = null, CancellationToken token = default) + { + var queryPayload = currencyPair == null ? null : new Dictionary { { "currencyPair", currencyPair } }; + return await SendHttpRequest>($"api/v1/stores/{storeId}/rates", queryPayload, HttpMethod.Get, token); } } diff --git a/BTCPayServer.Client/BTCPayServerClient.StoreUsers.cs b/BTCPayServer.Client/BTCPayServerClient.StoreUsers.cs index 7940ae50b..116cd6be6 100644 --- a/BTCPayServer.Client/BTCPayServerClient.StoreUsers.cs +++ b/BTCPayServer.Client/BTCPayServerClient.StoreUsers.cs @@ -5,40 +5,28 @@ using System.Threading; using System.Threading.Tasks; using BTCPayServer.Client.Models; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + public virtual async Task> GetStoreRoles(string storeId, CancellationToken token = default) { - public virtual async Task> GetStoreRoles(string storeId, - CancellationToken token = default) - { - using var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/roles"), token); - return await HandleResponse>(response); - } + return await SendHttpRequest>($"api/v1/stores/{storeId}/roles", null, HttpMethod.Get,token); + } - public virtual async Task> GetStoreUsers(string storeId, - CancellationToken token = default) - { - using var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/users"), token); - return await HandleResponse>(response); - } + public virtual async Task> GetStoreUsers(string storeId, CancellationToken token = default) + { + return await SendHttpRequest>($"api/v1/stores/{storeId}/users", null, HttpMethod.Get, token); + } - public virtual async Task RemoveStoreUser(string storeId, string userId, CancellationToken token = default) - { - using var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/users/{userId}", method: HttpMethod.Delete), token); - await HandleResponse(response); - } + public virtual async Task RemoveStoreUser(string storeId, string userId, CancellationToken token = default) + { + await SendHttpRequest($"api/v1/stores/{storeId}/users/{userId}", null, HttpMethod.Delete, token); + } - public virtual async Task AddStoreUser(string storeId, StoreUserData request, - CancellationToken token = default) - { - if (request == null) - throw new ArgumentNullException(nameof(request)); - using var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}/users", bodyPayload: request, method: HttpMethod.Post), - token); - await HandleResponse(response); - } + public virtual async Task AddStoreUser(string storeId, StoreUserData request, CancellationToken token = default) + { + if (request == null) throw new ArgumentNullException(nameof(request)); + await SendHttpRequest($"api/v1/stores/{storeId}/users", request, HttpMethod.Post, token); } } diff --git a/BTCPayServer.Client/BTCPayServerClient.Stores.cs b/BTCPayServer.Client/BTCPayServerClient.Stores.cs index 56e3a2a07..c331a10a8 100644 --- a/BTCPayServer.Client/BTCPayServerClient.Stores.cs +++ b/BTCPayServer.Client/BTCPayServerClient.Stores.cs @@ -5,47 +5,36 @@ using System.Threading; using System.Threading.Tasks; using BTCPayServer.Client.Models; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + public virtual async Task> GetStores(CancellationToken token = default) { - public virtual async Task> GetStores(CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest("api/v1/stores"), token); - return await HandleResponse>(response); - } - - public virtual async Task GetStore(string storeId, CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}"), token); - return await HandleResponse(response); - } - - public virtual async Task RemoveStore(string storeId, CancellationToken token = default) - { - var response = await _httpClient.SendAsync( - CreateHttpRequest($"api/v1/stores/{storeId}", method: HttpMethod.Delete), token); - await HandleResponse(response); - } - - public virtual async Task CreateStore(CreateStoreRequest request, CancellationToken token = default) - { - if (request == null) - throw new ArgumentNullException(nameof(request)); - var response = await _httpClient.SendAsync(CreateHttpRequest("api/v1/stores", bodyPayload: request, method: HttpMethod.Post), token); - return await HandleResponse(response); - } - - public virtual async Task UpdateStore(string storeId, UpdateStoreRequest request, CancellationToken token = default) - { - if (request == null) - throw new ArgumentNullException(nameof(request)); - if (storeId == null) - throw new ArgumentNullException(nameof(storeId)); - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}", bodyPayload: request, method: HttpMethod.Put), token); - return await HandleResponse(response); - } - + return await SendHttpRequest>("api/v1/stores", null, HttpMethod.Get, token); } + + public virtual async Task GetStore(string storeId, CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}", null, HttpMethod.Get, token); + } + + public virtual async Task RemoveStore(string storeId, CancellationToken token = default) + { + await SendHttpRequest($"api/v1/stores/{storeId}", null, HttpMethod.Delete, token); + } + + public virtual async Task CreateStore(CreateStoreRequest request, CancellationToken token = default) + { + if (request == null) throw new ArgumentNullException(nameof(request)); + return await SendHttpRequest("api/v1/stores", request, HttpMethod.Post, token); + } + + public virtual async Task UpdateStore(string storeId, UpdateStoreRequest request, CancellationToken token = default) + { + if (request == null) throw new ArgumentNullException(nameof(request)); + if (storeId == null) throw new ArgumentNullException(nameof(storeId)); + return await SendHttpRequest($"api/v1/stores/{storeId}", request, HttpMethod.Put, token); + } + } diff --git a/BTCPayServer.Client/BTCPayServerClient.Users.cs b/BTCPayServer.Client/BTCPayServerClient.Users.cs index b224d57f6..8e9d899c3 100644 --- a/BTCPayServer.Client/BTCPayServerClient.Users.cs +++ b/BTCPayServer.Client/BTCPayServerClient.Users.cs @@ -4,60 +4,53 @@ using System.Threading; using System.Threading.Tasks; using BTCPayServer.Client.Models; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + public virtual async Task GetCurrentUser(CancellationToken token = default) { - public virtual async Task GetCurrentUser(CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest("api/v1/users/me"), token); - return await HandleResponse(response); - } + return await SendHttpRequest("api/v1/users/me", null, HttpMethod.Get, token); + } - public virtual async Task CreateUser(CreateApplicationUserRequest request, - CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest("api/v1/users", null, request, HttpMethod.Post), token); - return await HandleResponse(response); - } + public virtual async Task CreateUser(CreateApplicationUserRequest request, CancellationToken token = default) + { + return await SendHttpRequest("api/v1/users", request, HttpMethod.Post, token); + } - public virtual async Task DeleteUser(string userId, CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/users/{userId}", null, HttpMethod.Delete), token); - await HandleResponse(response); - } + public virtual async Task DeleteUser(string userId, CancellationToken token = default) + { + await SendHttpRequest($"api/v1/users/{userId}", null, HttpMethod.Delete, token); + } - public virtual async Task GetUserByIdOrEmail(string idOrEmail, CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/users/{idOrEmail}", null, HttpMethod.Get), token); - return await HandleResponse(response); - } + public virtual async Task GetUserByIdOrEmail(string idOrEmail, CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/users/{idOrEmail}", null, HttpMethod.Get, token); + } - public virtual async Task LockUser(string idOrEmail, bool locked, CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/users/{idOrEmail}/lock", null, - new LockUserRequest { Locked = locked }, HttpMethod.Post), token); - await HandleResponse(response); - return response.IsSuccessStatusCode; - } + public virtual async Task LockUser(string idOrEmail, bool locked, CancellationToken token = default) + { + var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/users/{idOrEmail}/lock", null, + new LockUserRequest { Locked = locked }, HttpMethod.Post), token); + await HandleResponse(response); + return response.IsSuccessStatusCode; + } - public virtual async Task ApproveUser(string idOrEmail, bool approved, CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/users/{idOrEmail}/approve", null, - new ApproveUserRequest { Approved = approved }, HttpMethod.Post), token); - await HandleResponse(response); - return response.IsSuccessStatusCode; - } + public virtual async Task ApproveUser(string idOrEmail, bool approved, CancellationToken token = default) + { + var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/users/{idOrEmail}/approve", null, + new ApproveUserRequest { Approved = approved }, HttpMethod.Post), token); + await HandleResponse(response); + return response.IsSuccessStatusCode; + } - public virtual async Task GetUsers(CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/users/", null, HttpMethod.Get), token); - return await HandleResponse(response); - } + public virtual async Task GetUsers(CancellationToken token = default) + { + return await SendHttpRequest("api/v1/users/", null, HttpMethod.Get, token); + } - public virtual async Task DeleteCurrentUser(CancellationToken token = default) - { - await DeleteUser("me", token); - } + public virtual async Task DeleteCurrentUser(CancellationToken token = default) + { + await DeleteUser("me", token); } } diff --git a/BTCPayServer.Client/BTCPayServerClient.Webhooks.cs b/BTCPayServer.Client/BTCPayServerClient.Webhooks.cs index 189048f02..b3d19233e 100644 --- a/BTCPayServer.Client/BTCPayServerClient.Webhooks.cs +++ b/BTCPayServer.Client/BTCPayServerClient.Webhooks.cs @@ -3,61 +3,56 @@ using System.Threading; using System.Threading.Tasks; using BTCPayServer.Client.Models; -namespace BTCPayServer.Client -{ - public partial class BTCPayServerClient - { - public virtual async Task CreateWebhook(string storeId, Client.Models.CreateStoreWebhookRequest create, CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/webhooks", bodyPayload: create, method: HttpMethod.Post), token); - return await HandleResponse(response); - } - public virtual async Task GetWebhook(string storeId, string webhookId, CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/webhooks/{webhookId}"), token); - if (response.StatusCode == System.Net.HttpStatusCode.NotFound) - return null; - return await HandleResponse(response); - } - public virtual async Task UpdateWebhook(string storeId, string webhookId, Models.UpdateStoreWebhookRequest update, CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/webhooks/{webhookId}", bodyPayload: update, method: HttpMethod.Put), token); - return await HandleResponse(response); - } - public virtual async Task DeleteWebhook(string storeId, string webhookId, CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/webhooks/{webhookId}", method: HttpMethod.Delete), token); - return response.IsSuccessStatusCode; - } - public virtual async Task GetWebhooks(string storeId, CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/webhooks"), token); - return await HandleResponse(response); - } - public virtual async Task GetWebhookDeliveries(string storeId, string webhookId, CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/webhooks/{webhookId}/deliveries"), token); - return await HandleResponse(response); - } - public virtual async Task GetWebhookDelivery(string storeId, string webhookId, string deliveryId, CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/webhooks/{webhookId}/deliveries/{deliveryId}"), token); - if (response.StatusCode == System.Net.HttpStatusCode.NotFound) - return null; - return await HandleResponse(response); - } - public virtual async Task RedeliverWebhook(string storeId, string webhookId, string deliveryId, CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/webhooks/{webhookId}/deliveries/{deliveryId}/redeliver", null, HttpMethod.Post), token); - return await HandleResponse(response); - } +namespace BTCPayServer.Client; - public virtual async Task GetWebhookDeliveryRequest(string storeId, string webhookId, string deliveryId, CancellationToken token = default) - { - var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/webhooks/{webhookId}/deliveries/{deliveryId}/request"), token); - if (response.StatusCode == System.Net.HttpStatusCode.NotFound) - return null; - return await HandleResponse(response); - } +public partial class BTCPayServerClient +{ + public virtual async Task CreateWebhook(string storeId, CreateStoreWebhookRequest create, CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/webhooks", create, HttpMethod.Post, token); + } + + public virtual async Task GetWebhook(string storeId, string webhookId, CancellationToken token = default) + { + var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/webhooks/{webhookId}"), token); + return response.StatusCode == System.Net.HttpStatusCode.NotFound ? null : await HandleResponse(response); + } + + public virtual async Task UpdateWebhook(string storeId, string webhookId, UpdateStoreWebhookRequest update, CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/webhooks/{webhookId}", update, HttpMethod.Put, token); + } + + public virtual async Task DeleteWebhook(string storeId, string webhookId, CancellationToken token = default) + { + var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/webhooks/{webhookId}", method: HttpMethod.Delete), token); + return response.IsSuccessStatusCode; + } + + public virtual async Task GetWebhooks(string storeId, CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/webhooks", null, HttpMethod.Get, token); + } + + public virtual async Task GetWebhookDeliveries(string storeId, string webhookId, CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/webhooks/{webhookId}/deliveries", null, HttpMethod.Get, token); + } + + public virtual async Task GetWebhookDelivery(string storeId, string webhookId, string deliveryId, CancellationToken token = default) + { + var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/webhooks/{webhookId}/deliveries/{deliveryId}"), token); + return response.StatusCode == System.Net.HttpStatusCode.NotFound ? null : await HandleResponse(response); + } + + public virtual async Task RedeliverWebhook(string storeId, string webhookId, string deliveryId, CancellationToken token = default) + { + return await SendHttpRequest($"api/v1/stores/{storeId}/webhooks/{webhookId}/deliveries/{deliveryId}/redeliver", null, HttpMethod.Post, token); + } + + public virtual async Task GetWebhookDeliveryRequest(string storeId, string webhookId, string deliveryId, CancellationToken token = default) + { + var response = await _httpClient.SendAsync(CreateHttpRequest($"api/v1/stores/{storeId}/webhooks/{webhookId}/deliveries/{deliveryId}/request"), token); + return response.StatusCode == System.Net.HttpStatusCode.NotFound ? null : await HandleResponse(response); } } diff --git a/BTCPayServer.Client/BTCPayServerClient.cs b/BTCPayServer.Client/BTCPayServerClient.cs index 4ca23a191..79372f418 100644 --- a/BTCPayServer.Client/BTCPayServerClient.cs +++ b/BTCPayServer.Client/BTCPayServerClient.cs @@ -9,154 +9,179 @@ using System.Threading; using System.Threading.Tasks; using Newtonsoft.Json; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public partial class BTCPayServerClient { - public partial class BTCPayServerClient + private readonly string _apiKey; + private readonly Uri _btcpayHost; + private readonly string _username; + private readonly string _password; + protected readonly HttpClient _httpClient; + public Uri Host => _btcpayHost; + + public string APIKey => _apiKey; + + public BTCPayServerClient(Uri btcpayHost, HttpClient httpClient = null) { - private readonly string _apiKey; - private readonly Uri _btcpayHost; - private readonly string _username; - private readonly string _password; - private readonly HttpClient _httpClient; - public Uri Host => _btcpayHost; + if (btcpayHost == null) throw new ArgumentNullException(nameof(btcpayHost)); + _btcpayHost = btcpayHost; + _httpClient = httpClient ?? new HttpClient(); + } - public string APIKey => _apiKey; + public BTCPayServerClient(Uri btcpayHost, string APIKey, HttpClient httpClient = null) + { + _apiKey = APIKey; + _btcpayHost = btcpayHost; + _httpClient = httpClient ?? new HttpClient(); + } - public BTCPayServerClient(Uri btcpayHost, HttpClient httpClient = null) - { - if (btcpayHost == null) - throw new ArgumentNullException(nameof(btcpayHost)); - _btcpayHost = btcpayHost; - _httpClient = httpClient ?? new HttpClient(); - } - public BTCPayServerClient(Uri btcpayHost, string APIKey, HttpClient httpClient = null) - { - _apiKey = APIKey; - _btcpayHost = btcpayHost; - _httpClient = httpClient ?? new HttpClient(); - } + public BTCPayServerClient(Uri btcpayHost, string username, string password, HttpClient httpClient = null) + { + _apiKey = APIKey; + _btcpayHost = btcpayHost; + _username = username; + _password = password; + _httpClient = httpClient ?? new HttpClient(); + } - public BTCPayServerClient(Uri btcpayHost, string username, string password, HttpClient httpClient = null) + protected async Task HandleResponse(HttpResponseMessage message) + { + if (!message.IsSuccessStatusCode && message.Content?.Headers?.ContentType?.MediaType?.StartsWith("application/json", StringComparison.OrdinalIgnoreCase) is true) { - _apiKey = APIKey; - _btcpayHost = btcpayHost; - _username = username; - _password = password; - _httpClient = httpClient ?? new HttpClient(); - } - - protected async Task HandleResponse(HttpResponseMessage message) - { - if (!message.IsSuccessStatusCode && message.Content?.Headers?.ContentType?.MediaType?.StartsWith("application/json", StringComparison.OrdinalIgnoreCase) is true) + if (message.StatusCode == System.Net.HttpStatusCode.UnprocessableEntity) { - if (message.StatusCode == System.Net.HttpStatusCode.UnprocessableEntity) - { - var aa = await message.Content.ReadAsStringAsync(); - var err = JsonConvert.DeserializeObject(aa); - throw new GreenfieldValidationException(err); - } - if (message.StatusCode == System.Net.HttpStatusCode.Forbidden) - { - var err = JsonConvert.DeserializeObject(await message.Content.ReadAsStringAsync()); - throw new GreenfieldAPIException((int)message.StatusCode, err); - } - else - { - var err = JsonConvert.DeserializeObject(await message.Content.ReadAsStringAsync()); - if (err.Code != null) - throw new GreenfieldAPIException((int)message.StatusCode, err); - } + var aa = await message.Content.ReadAsStringAsync(); + var err = JsonConvert.DeserializeObject(aa); + throw new GreenfieldValidationException(err); } - message.EnsureSuccessStatusCode(); - } - - protected async Task HandleResponse(HttpResponseMessage message) - { - await HandleResponse(message); - var str = await message.Content.ReadAsStringAsync(); - return JsonConvert.DeserializeObject(str); - } - - public async Task SendHttpRequest(string path, - Dictionary queryPayload = null, - HttpMethod method = null, CancellationToken cancellationToken = default) - { - using var resp = await _httpClient.SendAsync(CreateHttpRequest(path, queryPayload, method), cancellationToken); - return await HandleResponse(resp); - } - public async Task SendHttpRequest(string path, - object bodyPayload = null, - HttpMethod method = null, CancellationToken cancellationToken = default) - { - using var resp = await _httpClient.SendAsync(CreateHttpRequest(path: path, bodyPayload: bodyPayload, method: method), cancellationToken); - return await HandleResponse(resp); - } - protected virtual HttpRequestMessage CreateHttpRequest(string path, - Dictionary queryPayload = null, - HttpMethod method = null) - { - UriBuilder uriBuilder = new UriBuilder(_btcpayHost) { Path = path }; - if (queryPayload != null && queryPayload.Any()) + if (message.StatusCode == System.Net.HttpStatusCode.Forbidden) { - AppendPayloadToQuery(uriBuilder, queryPayload); - } - - var httpRequest = new HttpRequestMessage(method ?? HttpMethod.Get, uriBuilder.Uri); - if (_apiKey != null) - httpRequest.Headers.Authorization = new AuthenticationHeaderValue("token", _apiKey); - else if (!string.IsNullOrEmpty(_username)) - { - httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Basic", System.Convert.ToBase64String(Encoding.ASCII.GetBytes(_username + ":" + _password))); - } - - - return httpRequest; - } - - protected virtual HttpRequestMessage CreateHttpRequest(string path, - Dictionary queryPayload = null, - T bodyPayload = default, HttpMethod method = null) - { - var request = CreateHttpRequest(path, queryPayload, method); - if (typeof(T).IsPrimitive || !EqualityComparer.Default.Equals(bodyPayload, default(T))) - { - request.Content = new StringContent(JsonConvert.SerializeObject(bodyPayload), Encoding.UTF8, "application/json"); - } - - return request; - } - - public static void AppendPayloadToQuery(UriBuilder uri, KeyValuePair keyValuePair) - { - if (uri.Query.Length > 1) - uri.Query += "&"; - - UriBuilder uriBuilder = uri; - if (!(keyValuePair.Value is string) && - keyValuePair.Value.GetType().GetInterfaces().Contains((typeof(IEnumerable)))) - { - foreach (var item in (IEnumerable)keyValuePair.Value) - { - uriBuilder.Query = uriBuilder.Query + Uri.EscapeDataString(keyValuePair.Key) + "=" + - Uri.EscapeDataString(item.ToString()) + "&"; - } + var err = JsonConvert.DeserializeObject(await message.Content.ReadAsStringAsync()); + throw new GreenfieldAPIException((int)message.StatusCode, err); } else { - uriBuilder.Query = uriBuilder.Query + Uri.EscapeDataString(keyValuePair.Key) + "=" + - Uri.EscapeDataString(keyValuePair.Value.ToString()) + "&"; + var err = JsonConvert.DeserializeObject(await message.Content.ReadAsStringAsync()); + if (err.Code != null) + throw new GreenfieldAPIException((int)message.StatusCode, err); } - uri.Query = uri.Query.Trim('&'); + } + message.EnsureSuccessStatusCode(); + } + + protected virtual async Task HandleResponse(HttpResponseMessage message) + { + await HandleResponse(message); + var str = await message.Content.ReadAsStringAsync(); + return JsonConvert.DeserializeObject(str); + } + + public virtual async Task SendHttpRequest(string path, + Dictionary queryPayload = null, + HttpMethod method = null, CancellationToken cancellationToken = default) + { + using var resp = await _httpClient.SendAsync(CreateHttpRequest(path, queryPayload, method), cancellationToken); + await HandleResponse(resp); + } + + public virtual async Task SendHttpRequest(string path, + Dictionary queryPayload = null, + HttpMethod method = null, CancellationToken cancellationToken = default) + { + using var resp = await _httpClient.SendAsync(CreateHttpRequest(path, queryPayload, method), cancellationToken); + return await HandleResponse(resp); + } + + public virtual async Task SendHttpRequest(string path, + object bodyPayload = null, + HttpMethod method = null, CancellationToken cancellationToken = default) + { + using var resp = await _httpClient.SendAsync(CreateHttpRequest(path: path, bodyPayload: bodyPayload, method: method), cancellationToken); + await HandleResponse(resp); + } + + protected virtual async Task SendHttpRequest(string path, + object bodyPayload = null, + HttpMethod method = null, CancellationToken cancellationToken = default) + { + using var resp = await _httpClient.SendAsync(CreateHttpRequest(path: path, bodyPayload: bodyPayload, method: method), cancellationToken); + return await HandleResponse(resp); + } + + protected virtual async Task SendHttpRequest(string path, + Dictionary queryPayload = null, + TReq bodyPayload = default, HttpMethod method = null, CancellationToken cancellationToken = default) + { + using var resp = await _httpClient.SendAsync(CreateHttpRequest(path: path, bodyPayload: bodyPayload, queryPayload: queryPayload, method: method), cancellationToken); + return await HandleResponse(resp); + } + + protected virtual HttpRequestMessage CreateHttpRequest(string path, + Dictionary queryPayload = null, + HttpMethod method = null) + { + var uriBuilder = new UriBuilder(_btcpayHost); + uriBuilder.Path += (uriBuilder.Path.EndsWith("/") || path.StartsWith("/") ? "" : "/") + path; + if (queryPayload != null && queryPayload.Any()) + { + AppendPayloadToQuery(uriBuilder, queryPayload); } - public static void AppendPayloadToQuery(UriBuilder uri, Dictionary payload) + var httpRequest = new HttpRequestMessage(method ?? HttpMethod.Get, uriBuilder.Uri); + if (_apiKey != null) + httpRequest.Headers.Authorization = new AuthenticationHeaderValue("token", _apiKey); + else if (!string.IsNullOrEmpty(_username)) { - if (uri.Query.Length > 1) - uri.Query += "&"; - foreach (KeyValuePair keyValuePair in payload) + httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(_username + ":" + _password))); + } + + return httpRequest; + } + + protected virtual HttpRequestMessage CreateHttpRequest(string path, + Dictionary queryPayload = null, + T bodyPayload = default, HttpMethod method = null) + { + var request = CreateHttpRequest(path, queryPayload, method); + if (typeof(T).IsPrimitive || !EqualityComparer.Default.Equals(bodyPayload, default(T))) + { + request.Content = new StringContent(JsonConvert.SerializeObject(bodyPayload), Encoding.UTF8, "application/json"); + } + + return request; + } + + public static void AppendPayloadToQuery(UriBuilder uri, KeyValuePair keyValuePair) + { + if (uri.Query.Length > 1) + uri.Query += "&"; + + UriBuilder uriBuilder = uri; + if (!(keyValuePair.Value is string) && + keyValuePair.Value.GetType().GetInterfaces().Contains((typeof(IEnumerable)))) + { + foreach (var item in (IEnumerable)keyValuePair.Value) { - AppendPayloadToQuery(uri, keyValuePair); + uriBuilder.Query = uriBuilder.Query + Uri.EscapeDataString(keyValuePair.Key) + "=" + + Uri.EscapeDataString(item.ToString()) + "&"; } } + else + { + uriBuilder.Query = uriBuilder.Query + Uri.EscapeDataString(keyValuePair.Key) + "=" + + Uri.EscapeDataString(keyValuePair.Value.ToString()) + "&"; + } + uri.Query = uri.Query.Trim('&'); + } + + public static void AppendPayloadToQuery(UriBuilder uri, Dictionary payload) + { + if (uri.Query.Length > 1) + uri.Query += "&"; + foreach (KeyValuePair keyValuePair in payload) + { + AppendPayloadToQuery(uri, keyValuePair); + } } } diff --git a/BTCPayServer.Client/GreenFieldAPIException.cs b/BTCPayServer.Client/GreenFieldAPIException.cs index 25e739e5d..98f60a5a4 100644 --- a/BTCPayServer.Client/GreenFieldAPIException.cs +++ b/BTCPayServer.Client/GreenFieldAPIException.cs @@ -1,17 +1,15 @@ using System; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public class GreenfieldAPIException : Exception { - public class GreenfieldAPIException : Exception + public GreenfieldAPIException(int httpCode, Models.GreenfieldAPIError error) : base(error.Message) { - public GreenfieldAPIException(int httpCode, Models.GreenfieldAPIError error) : base(error.Message) - { - if (error == null) - throw new ArgumentNullException(nameof(error)); - HttpCode = httpCode; - APIError = error; - } - public Models.GreenfieldAPIError APIError { get; } - public int HttpCode { get; set; } + if (error == null) throw new ArgumentNullException(nameof(error)); + HttpCode = httpCode; + APIError = error; } + public Models.GreenfieldAPIError APIError { get; } + public int HttpCode { get; set; } } diff --git a/BTCPayServer.Client/GreenFieldValidationException.cs b/BTCPayServer.Client/GreenFieldValidationException.cs index 7f0799536..d1db34dd3 100644 --- a/BTCPayServer.Client/GreenFieldValidationException.cs +++ b/BTCPayServer.Client/GreenFieldValidationException.cs @@ -2,27 +2,25 @@ using System; using System.Text; using BTCPayServer.Client.Models; -namespace BTCPayServer.Client +namespace BTCPayServer.Client; + +public class GreenfieldValidationException : Exception { - public class GreenfieldValidationException : Exception + public GreenfieldValidationException(GreenfieldValidationError[] errors) : base(BuildMessage(errors)) { - public GreenfieldValidationException(Models.GreenfieldValidationError[] errors) : base(BuildMessage(errors)) - { - ValidationErrors = errors; - } - - private static string BuildMessage(GreenfieldValidationError[] errors) - { - if (errors == null) - throw new ArgumentNullException(nameof(errors)); - StringBuilder builder = new StringBuilder(); - foreach (var error in errors) - { - builder.AppendLine($"{error.Path}: {error.Message}"); - } - return builder.ToString(); - } - - public Models.GreenfieldValidationError[] ValidationErrors { get; } + ValidationErrors = errors; } + + private static string BuildMessage(GreenfieldValidationError[] errors) + { + if (errors == null) throw new ArgumentNullException(nameof(errors)); + var builder = new StringBuilder(); + foreach (var error in errors) + { + builder.AppendLine($"{error.Path}: {error.Message}"); + } + return builder.ToString(); + } + + public GreenfieldValidationError[] ValidationErrors { get; } } diff --git a/BTCPayServer.Client/Models/GreenfieldAPIError.cs b/BTCPayServer.Client/Models/GreenfieldAPIError.cs index b07bac14d..c78c0fed2 100644 --- a/BTCPayServer.Client/Models/GreenfieldAPIError.cs +++ b/BTCPayServer.Client/Models/GreenfieldAPIError.cs @@ -11,8 +11,7 @@ namespace BTCPayServer.Client.Models public GreenfieldAPIError(string code, string message) { code = code ?? "generic-error"; - if (message == null) - throw new ArgumentNullException(nameof(message)); + if (message == null) throw new ArgumentNullException(nameof(message)); Code = code; Message = message; } diff --git a/BTCPayServer.Client/Models/GreenfieldValidationError.cs b/BTCPayServer.Client/Models/GreenfieldValidationError.cs index f4afaf9bf..c966cc190 100644 --- a/BTCPayServer.Client/Models/GreenfieldValidationError.cs +++ b/BTCPayServer.Client/Models/GreenfieldValidationError.cs @@ -6,14 +6,12 @@ namespace BTCPayServer.Client.Models { public GreenfieldValidationError() { - } + public GreenfieldValidationError(string path, string message) { - if (path == null) - throw new ArgumentNullException(nameof(path)); - if (message == null) - throw new ArgumentNullException(nameof(message)); + if (path == null) throw new ArgumentNullException(nameof(path)); + if (message == null) throw new ArgumentNullException(nameof(message)); Path = path; Message = message; } diff --git a/BTCPayServer/Controllers/GreenField/LocalBTCPayServerClient.cs b/BTCPayServer/Controllers/GreenField/LocalBTCPayServerClient.cs index e8642b34a..86596cbba 100644 --- a/BTCPayServer/Controllers/GreenField/LocalBTCPayServerClient.cs +++ b/BTCPayServer/Controllers/GreenField/LocalBTCPayServerClient.cs @@ -1128,14 +1128,14 @@ namespace BTCPayServer.Controllers.Greenfield } public override async Task> GetStoreRates(string storeId, - string[] currencyPair, CancellationToken token = default) + string[] currencyPair = null, CancellationToken token = default) { return GetFromActionResult>(await GetController().GetStoreRates(currencyPair)); } public override async Task> PreviewUpdateStoreRateConfiguration(string storeId, StoreRateConfiguration request, - string[] currencyPair, + string[] currencyPair = null, CancellationToken token = default) { return GetFromActionResult>(