-
Notifications
You must be signed in to change notification settings - Fork 0
/
btcPayClient.go
92 lines (75 loc) · 1.87 KB
/
btcPayClient.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
package ptcpayclient
//***********************************************
//* Copyright (c) 2021 Ulbora Labs LLC
//* Copyright (c) 2021 Ken Williamson
//***********************************************
import (
"bytes"
"crypto/ecdsa"
"net/http"
px "github.com/Ulbora/GoProxy"
lg "github.com/Ulbora/Level_Logger"
)
//BTCPayClient BTCPayClient
type BTCPayClient struct {
clientID string
userAgent string
host string
kp *ecdsa.PrivateKey
proxy px.Proxy
log *lg.Logger
headers Headers
tokens TokenData
crypto Crypto
}
//New New
func (a *BTCPayClient) New(host string, kp *ecdsa.PrivateKey, token string) Client {
var l lg.Logger
l.LogLevel = lg.OffLevel
a.log = &l
var p px.GoProxy
a.proxy = &p
a.host = host
a.kp = kp
var cryt Cryptography
a.crypto = &cryt
a.clientID = a.crypto.GetSinFromKey(a.kp)
a.userAgent = userAgent
a.tokens.Token = token
return a
}
//OverrideProxy OverrideProxy
func (a *BTCPayClient) OverrideProxy(proxy px.Proxy) {
a.proxy = proxy
}
//SetHeader SetHeader
func (a *BTCPayClient) SetHeader(head Headers) {
a.headers = head
}
//SetLogLevel SetLogLevel
func (a *BTCPayClient) SetLogLevel(level int) {
a.log.LogLevel = level
}
func (a *BTCPayClient) buildRequest(method string, url string, headers Headers, aJSON []byte) *http.Request {
var req *http.Request
var err error
headers.Set("x-accept-version", "2.0.0")
if method == http.MethodPost || method == http.MethodPut {
headers.Set("Content-Type", "application/json")
req, err = http.NewRequest(method, url, bytes.NewBuffer(aJSON))
} else {
req, err = http.NewRequest(method, url, nil)
}
a.log.Debug("err in build req: ", err)
if err == nil {
for k, v := range headers.headers {
a.log.Debug("header: ", k, v)
req.Header.Set(k, v)
}
}
return req
}
//GetClientID GetClientID
func (a *BTCPayClient) GetClientID() string {
return a.clientID
}