-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
132 lines (113 loc) · 3.01 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package btcpay
// WIP (webhooks?)
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func NewClient(url string, apiKey APIKey) *Client {
client := &Client{
URL: url,
APIKey: apiKey,
Http: &http.Client{},
}
client.Store = &Store{Client: client}
client.Invoice = &Invoice{Client: client, Store: client.Store}
client.PaymentRequest = &PaymentRequest{Client: client, Store: client.Store}
client.PullPayment = &PullPayment{Client: client, Store: client.Store}
client.Payout = &Payout{Client: client, Store: client.Store}
client.Notification = &Notification{Client: client}
return client
}
func NewBasicClient(url, username, password string) *Client {
client := &Client{
URL: url,
Username: username,
Password: password,
Http: &http.Client{},
}
client.Store = &Store{Client: client}
client.Invoice = &Invoice{Client: client, Store: client.Store}
client.PaymentRequest = &PaymentRequest{Client: client, Store: client.Store}
client.PullPayment = &PullPayment{Client: client, Store: client.Store}
client.Payout = &Payout{Client: client, Store: client.Store}
client.Notification = &Notification{Client: client}
return client
}
func setHeaders(c *Client, r *http.Request) {
if len(c.Username) > 0 && len(c.Password) > 0 {
r.SetBasicAuth(c.Username, c.Password)
} else if len(c.APIKey) > 0 {
r.Header.Set("Authorization", fmt.Sprintf("token %s", c.APIKey))
}
r.Header.Set("Content-Type", "application/json")
}
func setQueryParam(endpoint *string, params []map[string]interface{}) {
for _, param := range params {
for i := range param {
*endpoint = fmt.Sprintf("%s?%s=%v", *endpoint, i, param[i])
}
}
}
func (c *Client) doRequest(ctx context.Context, endpoint, method string, expRes interface{}, reqData interface{}, opts ...map[string]interface{}) (int, error) {
var dataReq []byte
var err error
if reqData != nil {
dataReq, err = json.Marshal(reqData)
if err != nil {
return 0, err
}
}
if len(opts) > 0 && len(opts[0]) > 0 {
setQueryParam(&endpoint, opts)
}
fmt.Println(endpoint)
req, err := http.NewRequestWithContext(ctx, method, endpoint, bytes.NewBuffer(dataReq))
if err != nil {
return 0, err
}
setHeaders(c, req)
resp, err := c.Http.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, err
}
switch resp.StatusCode {
case 200:
if expRes != nil {
err = json.Unmarshal(body, expRes)
if err != nil {
return 0, err
}
}
return resp.StatusCode, nil
default:
return resp.StatusCode, fmt.Errorf("%s", body)
}
}
func (c *Client) doSimpleRequest(req *http.Request) ([]byte, int, error) {
setHeaders(c, req)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, 0, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, 0, err
}
switch resp.StatusCode {
case 200:
return body, resp.StatusCode, nil
default:
return nil, resp.StatusCode, fmt.Errorf("%s", body)
}
}