Skip to content

Commit

Permalink
Merge pull request #2 from jon4hz/test-better-req
Browse files Browse the repository at this point in the history
request function with interface arguments
  • Loading branch information
jon4hz authored Jun 23, 2021
2 parents 4744550 + 9d15c5d commit 114f83f
Show file tree
Hide file tree
Showing 17 changed files with 752 additions and 1,368 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ func main() {
ctx := context.Background()

// Create a basicAuth client
client := btcpay.CreateBasicClient("https://mybtcpayserver.com", "myUsername", "myPassword")
client := btcpay.NewBasicClient("https://mybtcpayserver.com", "myUsername", "myPassword")

// Print informations about the server, etc
fmt.Println(client.GetServerInfo(ctx))

// Does the same but with an APIKey instead of basicAuth
// Create a client with an APIKey
client2 := btcpay.CreateBasicClient("https://mybtcpayserver.com", btcpay.APIKey("myAPIKey")
client2 := btcpay.NewClient("https://mybtcpayserver.com", btcpay.APIKey("myAPIKey")

// Print informations about the server, etc again but use the APIKey based client
fmt.Println(client2.GetServerInfo(ctx))
Expand Down Expand Up @@ -102,7 +102,7 @@ Endpoint |
|`/api/v1/stores/{storeId}/payment-requests ` | ✅ Fully implemented
|`/api/v1/stores/{storeId}/pull-payments` | ✅ Fully implemented
|`/api/v1/stores/{storeId}/payment-methods/OnChain/{cryptoCode}/wallet` | ⏳ Work in progress
|`/misc/lang` | ❌ Not working, [issue](https://github.com/btcpayserver/btcpayserver/issues/2437)
|`/misc/lang` | ✅ Fully implemented
|`/i` | ✅ Fully implemented
|`/api/v1/pull-payments` | ✅ Fully implemented

Expand Down
62 changes: 8 additions & 54 deletions api_keys.go
Original file line number Diff line number Diff line change
@@ -1,91 +1,45 @@
package btcpay

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
)

func (c *Client) RevokeAPIKey(ctx context.Context, apiKey *APIKey) (int, error) {
endpoint := fmt.Sprintf("%s/api/v1/api-keys/%s", c.URL, *apiKey)
req, err := http.NewRequestWithContext(ctx, "DELETE", endpoint, nil)
if err != nil {
return 0, err
}
_, statusCode, err := c.doRequest(req)
statusCode, err := c.doRequest(ctx, endpoint, "DELETE", nil, nil)
if err != nil {
return statusCode, err
}
return statusCode, nil
}

type APIKeyResponse struct {
APIKey APIKey `json:"apiKey"`
Label string `json:"label"`
Permissions []BTCPayPermission `json:"permissions"`
}

func (c *Client) GetCurrentAPIKey(ctx context.Context) (*APIKeyResponse, int, error) {
endpoint := fmt.Sprintf("%s/api/v1/api-keys/current", c.URL)
req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil)
if err != nil {
return nil, 0, err
}
bytes, statusCode, err := c.doRequest(req)
if err != nil {
return nil, statusCode, err
}
var dataRes APIKeyResponse
err = json.Unmarshal(bytes, &dataRes)
statusCode, err := c.doRequest(ctx, endpoint, "GET", &dataRes, nil)
if err != nil {
return nil, 0, err
return nil, statusCode, err
}
return &dataRes, statusCode, nil
}

func (c *Client) RevokeCurrentAPIKey(ctx context.Context) (*APIKeyResponse, int, error) {
endpoint := fmt.Sprintf("%s/api/v1/api-keys/current", c.URL)
req, err := http.NewRequestWithContext(ctx, "DELETE", endpoint, nil)
if err != nil {
return nil, 0, err
}
bytes, statusCode, err := c.doRequest(req)
var dataRes APIKeyResponse
statusCode, err := c.doRequest(ctx, endpoint, "DELETE", &dataRes, nil)
if err != nil {
return nil, statusCode, err
}
var data APIKeyResponse
err = json.Unmarshal(bytes, &data)
if err != nil {
return nil, 0, err
}
return &data, statusCode, nil
}

type APIKeyRequest struct {
Label string `json:"label,omitempty"`
Permissions []BTCPayPermission `json:"permissions,omitempty"`
return &dataRes, statusCode, nil
}

func (c *Client) CreateAPIKey(ctx context.Context, apiKeyRequest *APIKeyRequest) (*APIKeyResponse, int, error) {
endpoint := fmt.Sprintf("%s/api/v1/api-keys", c.URL)
dataReq, err := json.Marshal(apiKeyRequest)
if err != nil {
return nil, 0, err
}
req, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(dataReq))
if err != nil {
return nil, 0, err
}
bytes, statusCode, err := c.doRequest(req)
if err != nil {
return nil, statusCode, err
}
var dataRes APIKeyResponse
err = json.Unmarshal(bytes, &dataRes)
statusCode, err := c.doRequest(ctx, endpoint, "POST", &dataRes, apiKeyRequest)
if err != nil {
return nil, 0, err
return nil, statusCode, err
}
return &dataRes, statusCode, nil
}
1 change: 1 addition & 0 deletions authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package btcpay

import "fmt"

// Enums BTCPayPermission
type BTCPayPermission string

type Permission struct {
Expand Down
24 changes: 2 additions & 22 deletions authorization.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
package btcpay

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
)

// WIP, needs testing

type AuthorizationRequest struct {
Permissions []BTCPayPermission `json:"permissions,omitempty"`
ApplicationName string `json:"applicationName,omitempty"`
Strict bool `json:"strict,omitempty"`
SelectiveStores bool `json:"selectiveStores,omitempty"`
Redirect string `json:"redirect,omitempty"`
ApplicationIdentifier string `json:"applicationIdentifier,omitempty"`
}
// !Needs testing

func (c *Client) Authorize(ctx context.Context, authRequest *AuthorizationRequest) (int, error) {
endpoint := fmt.Sprintf("%s/api-keys/authorize", c.URL)
dataReq, err := json.Marshal(authRequest)
if err != nil {
return 0, err
}
req, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(dataReq))
if err != nil {
return 0, err
}
_, statusCode, err := c.doRequest(req)
statusCode, err := c.doRequest(ctx, endpoint, "GET", nil, authRequest)
if err != nil {
return statusCode, err
}
Expand Down
154 changes: 154 additions & 0 deletions btcpay_enums.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package btcpay

// Enums SpeedPolicy
type BTCPaySpeedPolicy string

type SpeedPolicy struct {
HighSpeed BTCPaySpeedPolicy
MediumSpeed BTCPaySpeedPolicy
LowMediumSpeed BTCPaySpeedPolicy
LowSpeed BTCPaySpeedPolicy
}

func GetSpeedPolicy() *SpeedPolicy {
return &SpeedPolicy{
HighSpeed: "HighSpeed",
MediumSpeed: "MediumSpeed",
LowMediumSpeed: "LowMediumSpeed",
LowSpeed: "LowSpeed",
}
}

// Enums InvoiceStatus
type BTCPayInvoiceStatus string

type InvoiceStatus struct {
New BTCPayInvoiceStatus
Processing BTCPayInvoiceStatus
Expired BTCPayInvoiceStatus
Invalid BTCPayInvoiceStatus
Settled BTCPayInvoiceStatus
}

func GetInvoiceStatus() *InvoiceStatus {
return &InvoiceStatus{
New: "New",
Processing: "Processing",
Expired: "Expired",
Invalid: "Invalid",
Settled: "Settled",
}
}

// Enums InvoiceAdditionalStatus
type BTCPayInvoiceAdditionalStatus string

type InvoiceAdditionalStatus struct {
None BTCPayInvoiceAdditionalStatus
PaidLate BTCPayInvoiceAdditionalStatus
PaidPartial BTCPayInvoiceAdditionalStatus
Marked BTCPayInvoiceAdditionalStatus
AddInvalid BTCPayInvoiceAdditionalStatus
PaidOver BTCPayInvoiceAdditionalStatus
}

func GetInvoiceAdditionalStatus() *InvoiceAdditionalStatus {
return &InvoiceAdditionalStatus{
None: "None",
PaidLate: "PaidLate",
PaidPartial: "PaidPartial",
Marked: "Marked",
AddInvalid: "Invalid",
PaidOver: "PaidOver",
}

}

// Enums InvoiceStatusMark
type BTCPayInvoiceStatusMark string

type InvoiceStatusMark struct {
MarkInvalid BTCPayInvoiceStatusMark
MarkComplete BTCPayInvoiceStatusMark
}

func GetInvoiceStatusMark() *InvoiceStatusMark {
return &InvoiceStatusMark{
MarkInvalid: "Invalid",
MarkComplete: "Complete",
}
}

// Enums PaymentStatus
type BTCPayPaymentStatus string

type PaymentStatus struct {
New BTCPayPaymentStatus
Processing BTCPayPaymentStatus
Expired BTCPayPaymentStatus
Invalid BTCPayPaymentStatus
Settled BTCPayPaymentStatus
}

func GetPaymentStatus() *PaymentStatus {
return &PaymentStatus{
Processing: "Processing",
Invalid: "Invalid",
Settled: "Settled",
}
}

// Enums PaymentRequestStatus
type BTCPayPaymentRequestStatus string

type PaymentRequestStatus struct {
Pending BTCPayInvoiceStatus
Completed BTCPayInvoiceStatus
Expired BTCPayInvoiceStatus
}

func GetPaymentRequestStatus() *PaymentRequestStatus {
return &PaymentRequestStatus{
Pending: "Pending",
Completed: "Completed",
Expired: "Expired",
}
}

// Enums NetworkFeeMode
type BTCPayPayoutStatus string

type PayoutStatus struct {
AwaitingApproval BTCPayPayoutStatus
AwaitingPayment BTCPayPayoutStatus
InProgress BTCPayPayoutStatus
Completed BTCPayPayoutStatus
Cancelled BTCPayPayoutStatus
}

func GetPayoutStatus() *PayoutStatus {
return &PayoutStatus{
AwaitingApproval: "AwaitingApproval",
AwaitingPayment: "AwaitingPayment",
InProgress: "InProgress",
Completed: "Completed",
Cancelled: "Cancelled",
}
}

// Enums NetworkFeeMode
type BTCPayNetworkFeeMode string

type NetworkFeeMode struct {
MultiplePaymentsOnly BTCPayNetworkFeeMode
Always BTCPayNetworkFeeMode
Never BTCPayNetworkFeeMode
}

func GetNetworkFeeMode() *NetworkFeeMode {
return &NetworkFeeMode{
MultiplePaymentsOnly: "MultiplePaymentsOnly",
Always: "Always",
Never: "Never",
}
}
Loading

0 comments on commit 114f83f

Please sign in to comment.