-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlnbot.go
More file actions
244 lines (212 loc) · 5.82 KB
/
Copy pathlnbot.go
File metadata and controls
244 lines (212 loc) · 5.82 KB
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
package lnbot
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
const (
defaultBaseURL = "https://api.ln.bot"
defaultTimeout = 30 * time.Second
// Version is the SDK version sent in the User-Agent header.
Version = "1.0.0"
)
// Option configures the Client.
type Option func(*Client)
// WithBaseURL sets a custom API base URL.
func WithBaseURL(u string) Option {
return func(c *Client) { c.baseURL = strings.TrimRight(u, "/") }
}
// WithHTTPClient sets a custom *http.Client.
func WithHTTPClient(hc *http.Client) Option {
return func(c *Client) { c.http = hc }
}
// Client is the LnBot API client.
type Client struct {
apiKey string
baseURL string
http *http.Client
// Top-level (account) services
Wallets *WalletsService
Keys *KeysService
Invoices *PublicInvoicesService
Backup *BackupService
Restore *RestoreService
}
// New creates a new LnBot client.
// Pass an empty string for apiKey if not needed (registration, public invoices, restore).
func New(apiKey string, opts ...Option) *Client {
c := &Client{
apiKey: apiKey,
baseURL: defaultBaseURL,
http: http.DefaultClient,
}
for _, o := range opts {
o(c)
}
c.Wallets = &WalletsService{c: c}
c.Keys = &KeysService{c: c}
c.Invoices = &PublicInvoicesService{c: c}
c.Backup = &BackupService{c: c}
c.Restore = &RestoreService{c: c}
return c
}
// Register creates a new account. No authentication required.
func (c *Client) Register(ctx context.Context) (*RegisterResponse, error) {
var v RegisterResponse
if err := c.post(ctx, "/v1/register", nil, &v); err != nil {
return nil, err
}
return &v, nil
}
// Me returns the authenticated identity.
func (c *Client) Me(ctx context.Context) (*MeResponse, error) {
var v MeResponse
if err := c.get(ctx, "/v1/me", &v); err != nil {
return nil, err
}
return &v, nil
}
// Wallet returns a wallet handle scoped to the given wallet ID.
// All sub-resources (invoices, payments, addresses, etc.) are accessed through this handle.
func (c *Client) Wallet(id string) *WalletHandle {
prefix := fmt.Sprintf("/v1/wallets/%s", url.PathEscape(id))
return &WalletHandle{
c: c,
WalletID: id,
prefix: prefix,
Key: &WalletKeyService{c: c, prefix: prefix},
Invoices: &InvoicesService{c: c, prefix: prefix},
Payments: &PaymentsService{c: c, prefix: prefix},
Addresses: &AddressesService{c: c, prefix: prefix},
Transactions: &TransactionsService{c: c, prefix: prefix},
Webhooks: &WebhooksService{c: c, prefix: prefix},
Events: &EventsService{c: c, prefix: prefix},
L402: &L402Service{c: c, prefix: prefix},
}
}
// WalletHandle provides access to wallet-scoped resources.
type WalletHandle struct {
c *Client
WalletID string
prefix string
Key *WalletKeyService
Invoices *InvoicesService
Payments *PaymentsService
Addresses *AddressesService
Transactions *TransactionsService
Webhooks *WebhooksService
Events *EventsService
L402 *L402Service
}
// Get returns the wallet details.
func (w *WalletHandle) Get(ctx context.Context) (*Wallet, error) {
var v Wallet
if err := w.c.get(ctx, w.prefix, &v); err != nil {
return nil, err
}
return &v, nil
}
// Update modifies the wallet's settings.
func (w *WalletHandle) Update(ctx context.Context, params *UpdateWalletParams) (*Wallet, error) {
var v Wallet
if err := w.c.patch(ctx, w.prefix, params, &v); err != nil {
return nil, err
}
return &v, nil
}
// ── HTTP helpers ────────────────────────────────────────
func (c *Client) newRequest(ctx context.Context, method, path string, body any) (*http.Request, error) {
u := c.baseURL + path
var buf io.Reader
if body != nil {
b, err := json.Marshal(body)
if err != nil {
return nil, err
}
buf = bytes.NewReader(b)
}
req, err := http.NewRequestWithContext(ctx, method, u, buf)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", "lnbot-go/"+Version)
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
if c.apiKey != "" {
req.Header.Set("Authorization", "Bearer "+c.apiKey)
}
return req, nil
}
func (c *Client) do(req *http.Request, v any) error {
if _, ok := req.Context().Deadline(); !ok {
ctx, cancel := context.WithTimeout(req.Context(), defaultTimeout)
defer cancel()
req = req.WithContext(ctx)
}
resp, err := c.http.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode >= 400 {
return parseAPIError(resp.StatusCode, data)
}
if resp.StatusCode == 204 || v == nil {
return nil
}
return json.Unmarshal(data, v)
}
func (c *Client) get(ctx context.Context, path string, v any) error {
req, err := c.newRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return err
}
return c.do(req, v)
}
func (c *Client) post(ctx context.Context, path string, body, v any) error {
req, err := c.newRequest(ctx, http.MethodPost, path, body)
if err != nil {
return err
}
return c.do(req, v)
}
func (c *Client) patch(ctx context.Context, path string, body, v any) error {
req, err := c.newRequest(ctx, http.MethodPatch, path, body)
if err != nil {
return err
}
return c.do(req, v)
}
func (c *Client) del(ctx context.Context, path string) error {
req, err := c.newRequest(ctx, http.MethodDelete, path, nil)
if err != nil {
return err
}
return c.do(req, nil)
}
func addListParams(path string, limit, after *int) string {
params := url.Values{}
if limit != nil {
params.Set("limit", strconv.Itoa(*limit))
}
if after != nil {
params.Set("after", strconv.Itoa(*after))
}
if len(params) == 0 {
return path
}
return fmt.Sprintf("%s?%s", path, params.Encode())
}