Skip to content

Commit 114f83f

Browse files
authored
Merge pull request #2 from jon4hz/test-better-req
request function with interface arguments
2 parents 4744550 + 9d15c5d commit 114f83f

17 files changed

Lines changed: 752 additions & 1368 deletions

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ func main() {
3131
ctx := context.Background()
3232

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

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

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

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

api_keys.go

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,45 @@
11
package btcpay
22

33
import (
4-
"bytes"
54
"context"
6-
"encoding/json"
75
"fmt"
8-
"net/http"
96
)
107

118
func (c *Client) RevokeAPIKey(ctx context.Context, apiKey *APIKey) (int, error) {
129
endpoint := fmt.Sprintf("%s/api/v1/api-keys/%s", c.URL, *apiKey)
13-
req, err := http.NewRequestWithContext(ctx, "DELETE", endpoint, nil)
14-
if err != nil {
15-
return 0, err
16-
}
17-
_, statusCode, err := c.doRequest(req)
10+
statusCode, err := c.doRequest(ctx, endpoint, "DELETE", nil, nil)
1811
if err != nil {
1912
return statusCode, err
2013
}
2114
return statusCode, nil
2215
}
2316

24-
type APIKeyResponse struct {
25-
APIKey APIKey `json:"apiKey"`
26-
Label string `json:"label"`
27-
Permissions []BTCPayPermission `json:"permissions"`
28-
}
29-
3017
func (c *Client) GetCurrentAPIKey(ctx context.Context) (*APIKeyResponse, int, error) {
3118
endpoint := fmt.Sprintf("%s/api/v1/api-keys/current", c.URL)
32-
req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil)
33-
if err != nil {
34-
return nil, 0, err
35-
}
36-
bytes, statusCode, err := c.doRequest(req)
37-
if err != nil {
38-
return nil, statusCode, err
39-
}
4019
var dataRes APIKeyResponse
41-
err = json.Unmarshal(bytes, &dataRes)
20+
statusCode, err := c.doRequest(ctx, endpoint, "GET", &dataRes, nil)
4221
if err != nil {
43-
return nil, 0, err
22+
return nil, statusCode, err
4423
}
4524
return &dataRes, statusCode, nil
4625
}
4726

4827
func (c *Client) RevokeCurrentAPIKey(ctx context.Context) (*APIKeyResponse, int, error) {
4928
endpoint := fmt.Sprintf("%s/api/v1/api-keys/current", c.URL)
50-
req, err := http.NewRequestWithContext(ctx, "DELETE", endpoint, nil)
51-
if err != nil {
52-
return nil, 0, err
53-
}
54-
bytes, statusCode, err := c.doRequest(req)
29+
var dataRes APIKeyResponse
30+
statusCode, err := c.doRequest(ctx, endpoint, "DELETE", &dataRes, nil)
5531
if err != nil {
5632
return nil, statusCode, err
5733
}
58-
var data APIKeyResponse
59-
err = json.Unmarshal(bytes, &data)
60-
if err != nil {
61-
return nil, 0, err
62-
}
63-
return &data, statusCode, nil
64-
}
65-
66-
type APIKeyRequest struct {
67-
Label string `json:"label,omitempty"`
68-
Permissions []BTCPayPermission `json:"permissions,omitempty"`
34+
return &dataRes, statusCode, nil
6935
}
7036

7137
func (c *Client) CreateAPIKey(ctx context.Context, apiKeyRequest *APIKeyRequest) (*APIKeyResponse, int, error) {
7238
endpoint := fmt.Sprintf("%s/api/v1/api-keys", c.URL)
73-
dataReq, err := json.Marshal(apiKeyRequest)
74-
if err != nil {
75-
return nil, 0, err
76-
}
77-
req, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(dataReq))
78-
if err != nil {
79-
return nil, 0, err
80-
}
81-
bytes, statusCode, err := c.doRequest(req)
82-
if err != nil {
83-
return nil, statusCode, err
84-
}
8539
var dataRes APIKeyResponse
86-
err = json.Unmarshal(bytes, &dataRes)
40+
statusCode, err := c.doRequest(ctx, endpoint, "POST", &dataRes, apiKeyRequest)
8741
if err != nil {
88-
return nil, 0, err
42+
return nil, statusCode, err
8943
}
9044
return &dataRes, statusCode, nil
9145
}

authentication.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package btcpay
22

33
import "fmt"
44

5+
// Enums BTCPayPermission
56
type BTCPayPermission string
67

78
type Permission struct {

authorization.go

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,15 @@
11
package btcpay
22

33
import (
4-
"bytes"
54
"context"
6-
"encoding/json"
75
"fmt"
8-
"net/http"
96
)
107

11-
// WIP, needs testing
12-
13-
type AuthorizationRequest struct {
14-
Permissions []BTCPayPermission `json:"permissions,omitempty"`
15-
ApplicationName string `json:"applicationName,omitempty"`
16-
Strict bool `json:"strict,omitempty"`
17-
SelectiveStores bool `json:"selectiveStores,omitempty"`
18-
Redirect string `json:"redirect,omitempty"`
19-
ApplicationIdentifier string `json:"applicationIdentifier,omitempty"`
20-
}
8+
// !Needs testing
219

2210
func (c *Client) Authorize(ctx context.Context, authRequest *AuthorizationRequest) (int, error) {
2311
endpoint := fmt.Sprintf("%s/api-keys/authorize", c.URL)
24-
dataReq, err := json.Marshal(authRequest)
25-
if err != nil {
26-
return 0, err
27-
}
28-
req, err := http.NewRequestWithContext(ctx, "POST", endpoint, bytes.NewBuffer(dataReq))
29-
if err != nil {
30-
return 0, err
31-
}
32-
_, statusCode, err := c.doRequest(req)
12+
statusCode, err := c.doRequest(ctx, endpoint, "GET", nil, authRequest)
3313
if err != nil {
3414
return statusCode, err
3515
}

btcpay_enums.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package btcpay
2+
3+
// Enums SpeedPolicy
4+
type BTCPaySpeedPolicy string
5+
6+
type SpeedPolicy struct {
7+
HighSpeed BTCPaySpeedPolicy
8+
MediumSpeed BTCPaySpeedPolicy
9+
LowMediumSpeed BTCPaySpeedPolicy
10+
LowSpeed BTCPaySpeedPolicy
11+
}
12+
13+
func GetSpeedPolicy() *SpeedPolicy {
14+
return &SpeedPolicy{
15+
HighSpeed: "HighSpeed",
16+
MediumSpeed: "MediumSpeed",
17+
LowMediumSpeed: "LowMediumSpeed",
18+
LowSpeed: "LowSpeed",
19+
}
20+
}
21+
22+
// Enums InvoiceStatus
23+
type BTCPayInvoiceStatus string
24+
25+
type InvoiceStatus struct {
26+
New BTCPayInvoiceStatus
27+
Processing BTCPayInvoiceStatus
28+
Expired BTCPayInvoiceStatus
29+
Invalid BTCPayInvoiceStatus
30+
Settled BTCPayInvoiceStatus
31+
}
32+
33+
func GetInvoiceStatus() *InvoiceStatus {
34+
return &InvoiceStatus{
35+
New: "New",
36+
Processing: "Processing",
37+
Expired: "Expired",
38+
Invalid: "Invalid",
39+
Settled: "Settled",
40+
}
41+
}
42+
43+
// Enums InvoiceAdditionalStatus
44+
type BTCPayInvoiceAdditionalStatus string
45+
46+
type InvoiceAdditionalStatus struct {
47+
None BTCPayInvoiceAdditionalStatus
48+
PaidLate BTCPayInvoiceAdditionalStatus
49+
PaidPartial BTCPayInvoiceAdditionalStatus
50+
Marked BTCPayInvoiceAdditionalStatus
51+
AddInvalid BTCPayInvoiceAdditionalStatus
52+
PaidOver BTCPayInvoiceAdditionalStatus
53+
}
54+
55+
func GetInvoiceAdditionalStatus() *InvoiceAdditionalStatus {
56+
return &InvoiceAdditionalStatus{
57+
None: "None",
58+
PaidLate: "PaidLate",
59+
PaidPartial: "PaidPartial",
60+
Marked: "Marked",
61+
AddInvalid: "Invalid",
62+
PaidOver: "PaidOver",
63+
}
64+
65+
}
66+
67+
// Enums InvoiceStatusMark
68+
type BTCPayInvoiceStatusMark string
69+
70+
type InvoiceStatusMark struct {
71+
MarkInvalid BTCPayInvoiceStatusMark
72+
MarkComplete BTCPayInvoiceStatusMark
73+
}
74+
75+
func GetInvoiceStatusMark() *InvoiceStatusMark {
76+
return &InvoiceStatusMark{
77+
MarkInvalid: "Invalid",
78+
MarkComplete: "Complete",
79+
}
80+
}
81+
82+
// Enums PaymentStatus
83+
type BTCPayPaymentStatus string
84+
85+
type PaymentStatus struct {
86+
New BTCPayPaymentStatus
87+
Processing BTCPayPaymentStatus
88+
Expired BTCPayPaymentStatus
89+
Invalid BTCPayPaymentStatus
90+
Settled BTCPayPaymentStatus
91+
}
92+
93+
func GetPaymentStatus() *PaymentStatus {
94+
return &PaymentStatus{
95+
Processing: "Processing",
96+
Invalid: "Invalid",
97+
Settled: "Settled",
98+
}
99+
}
100+
101+
// Enums PaymentRequestStatus
102+
type BTCPayPaymentRequestStatus string
103+
104+
type PaymentRequestStatus struct {
105+
Pending BTCPayInvoiceStatus
106+
Completed BTCPayInvoiceStatus
107+
Expired BTCPayInvoiceStatus
108+
}
109+
110+
func GetPaymentRequestStatus() *PaymentRequestStatus {
111+
return &PaymentRequestStatus{
112+
Pending: "Pending",
113+
Completed: "Completed",
114+
Expired: "Expired",
115+
}
116+
}
117+
118+
// Enums NetworkFeeMode
119+
type BTCPayPayoutStatus string
120+
121+
type PayoutStatus struct {
122+
AwaitingApproval BTCPayPayoutStatus
123+
AwaitingPayment BTCPayPayoutStatus
124+
InProgress BTCPayPayoutStatus
125+
Completed BTCPayPayoutStatus
126+
Cancelled BTCPayPayoutStatus
127+
}
128+
129+
func GetPayoutStatus() *PayoutStatus {
130+
return &PayoutStatus{
131+
AwaitingApproval: "AwaitingApproval",
132+
AwaitingPayment: "AwaitingPayment",
133+
InProgress: "InProgress",
134+
Completed: "Completed",
135+
Cancelled: "Cancelled",
136+
}
137+
}
138+
139+
// Enums NetworkFeeMode
140+
type BTCPayNetworkFeeMode string
141+
142+
type NetworkFeeMode struct {
143+
MultiplePaymentsOnly BTCPayNetworkFeeMode
144+
Always BTCPayNetworkFeeMode
145+
Never BTCPayNetworkFeeMode
146+
}
147+
148+
func GetNetworkFeeMode() *NetworkFeeMode {
149+
return &NetworkFeeMode{
150+
MultiplePaymentsOnly: "MultiplePaymentsOnly",
151+
Always: "Always",
152+
Never: "Never",
153+
}
154+
}

0 commit comments

Comments
 (0)