-
Notifications
You must be signed in to change notification settings - Fork 3
/
rest.go
274 lines (195 loc) · 6.02 KB
/
rest.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package lightningapi
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
utils "github.com/bolt-observer/go_common/utils"
)
// GetDoFunc = signature for Do function
type GetDoFunc func(req *http.Request) (*http.Response, error)
// HTTPAPI struct
type HTTPAPI struct {
DoFunc GetDoFunc
client *http.Client
}
// Do - invokes HTTP request
func (h *HTTPAPI) Do(req *http.Request) (*http.Response, error) {
if h.DoFunc != nil {
return h.DoFunc(req)
} else if h.client != nil {
return h.client.Do(req)
}
return nil, fmt.Errorf("no way to fulfill request")
}
// NewHTTPAPI returns a new HTTPAPI
func NewHTTPAPI() *HTTPAPI {
return &HTTPAPI{client: nil, DoFunc: nil}
}
// SetTransport - sets HTTP transport
func (h *HTTPAPI) SetTransport(transport *http.Transport) {
h.client = &http.Client{Transport: transport}
}
// HTTPGetInfo - invokes GetInfo method
func (h *HTTPAPI) HTTPGetInfo(ctx context.Context, req *http.Request, trans *http.Transport) (*GetInfoResponseOverride, error) {
var info GetInfoResponseOverride
req = req.WithContext(ctx)
req.Method = http.MethodGet
u, err := url.Parse(fmt.Sprintf("%s/v1/getinfo", req.URL))
if err != nil {
return nil, fmt.Errorf("invalid url %s", err)
}
req.URL = u
resp, err := h.Do(req)
if err != nil {
return nil, fmt.Errorf("http request failed %s", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http got error %d", resp.StatusCode)
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&info)
if err != nil {
return nil, fmt.Errorf("decode error %v", err)
}
return &info, nil
}
// HTTPGetChannels - invokes GetChannels method
func (h *HTTPAPI) HTTPGetChannels(ctx context.Context, req *http.Request, trans *http.Transport) (*Channels, error) {
var info Channels
req = req.WithContext(ctx)
req.Method = http.MethodGet
u, err := url.Parse(fmt.Sprintf("%s/v1/channels", req.URL))
if err != nil {
return nil, fmt.Errorf("invalid url %s", err)
}
req.URL = u
resp, err := h.Do(req)
if err != nil {
return nil, fmt.Errorf("http request failed %s", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http got error %d", resp.StatusCode)
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&info)
if err != nil {
return nil, fmt.Errorf("got error %v", err)
}
return &info, nil
}
// HTTPGetGraph - invokes GetGraph method
func (h *HTTPAPI) HTTPGetGraph(ctx context.Context, req *http.Request, trans *http.Transport, unannounced bool) (*Graph, error) {
var graph Graph
req = req.WithContext(ctx)
req.Method = http.MethodGet
u, err := url.Parse(fmt.Sprintf("%s/v1/graph?include_unannounced=%s", req.URL, strconv.FormatBool(unannounced)))
if err != nil {
return nil, fmt.Errorf("invalid url %s", err)
}
req.URL = u
resp, err := h.Do(req)
if err != nil {
return nil, fmt.Errorf("http request failed %s", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http got error %d", resp.StatusCode)
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&graph)
if err != nil {
return nil, fmt.Errorf("got error %v", err)
}
return &graph, nil
}
// HTTPGetNodeInfo - invokes GetNodeInfo method
func (h *HTTPAPI) HTTPGetNodeInfo(ctx context.Context, req *http.Request, trans *http.Transport, pubKey string, channels bool) (*GetNodeInfoOverride, error) {
var graph GetNodeInfoOverride
req = req.WithContext(ctx)
req.Method = http.MethodGet
u, err := url.Parse(fmt.Sprintf("%s/v1/graph/node/%s?include_channels=%s", req.URL, pubKey, strconv.FormatBool(channels)))
if err != nil {
return nil, fmt.Errorf("invalid url %s", err)
}
req.URL = u
resp, err := h.Do(req)
if err != nil {
return nil, fmt.Errorf("http request failed %s", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http got error %d", resp.StatusCode)
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&graph)
if err != nil {
return nil, fmt.Errorf("got error %v", err)
}
return &graph, nil
}
// HTTPGetChanInfo - invokes GetChanInfo method
func (h *HTTPAPI) HTTPGetChanInfo(ctx context.Context, req *http.Request, trans *http.Transport, chanID uint64) (*GraphEdgeOverride, error) {
var graph GraphEdgeOverride
req = req.WithContext(ctx)
req.Method = http.MethodGet
u, err := url.Parse(fmt.Sprintf("%s/v1/graph/edge/%d", req.URL, chanID))
if err != nil {
return nil, fmt.Errorf("invalid url %s", err)
}
req.URL = u
resp, err := h.Do(req)
if err != nil {
return nil, fmt.Errorf("http request failed %s", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http got error %d", resp.StatusCode)
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&graph)
if err != nil {
return nil, fmt.Errorf("got error %v", err)
}
return &graph, nil
}
// GetHTTPRequest - generic method for doing HTTP requests
func (h *HTTPAPI) GetHTTPRequest(getData GetDataCall) (*http.Request, *http.Transport, error) {
if getData == nil {
return nil, nil, fmt.Errorf("getData is nil")
}
data, err := getData()
if err != nil {
return nil, nil, err
}
myurl := data.Endpoint
if !strings.HasPrefix(data.Endpoint, "http") {
myurl = fmt.Sprintf("https://%s", data.Endpoint)
}
req, err := http.NewRequest(http.MethodGet, myurl, nil)
if err != nil {
return nil, nil, fmt.Errorf("new request %s", err.Error())
}
req.Header.Set("Grpc-Metadata-macaroon", data.MacaroonHex)
certBytes, err := utils.SafeBase64Decode(data.CertificateBase64)
if err != nil {
return nil, nil, fmt.Errorf("base64 decode error %s", err)
}
verification := PublicCAorCert
if data.CertVerificationType != nil {
verification = CertificateVerification(*data.CertVerificationType)
}
tls, err := getTLSConfig(certBytes, data.Endpoint, verification)
if err != nil {
return nil, nil, fmt.Errorf("getTlsConfig failed %v", err)
}
trans := &http.Transport{
TLSClientConfig: tls,
}
return req, trans, nil
}