client.go
TEXT FILE1006 B · Public source code for developers, search crawlers and AI agents
1package client
2
3import (
4 "bytes"
5 "context"
6 "errors"
7 "io"
8 "net/http"
9 "strings"
10 "time"
11)
12
13type Client struct { BaseURL string; HTTP *http.Client }
14func (c Client) Post(ctx context.Context, path string, body []byte, walletKey string) ([]byte,error) {
15 if !strings.HasPrefix(path,"/api/v5/btc/") { return nil, errors.New("path not allowed") }
16 if strings.TrimSpace(c.BaseURL)=="" {return nil,errors.New("missing base URL")}
17 req,err:=http.NewRequestWithContext(ctx,http.MethodPost,strings.TrimRight(c.BaseURL,"/")+path,bytes.NewReader(body));if err!=nil{return nil,err}
18 req.Header.Set("Content-Type","application/json")
19 if walletKey!="" {req.Header.Set("X-BTC-KEY",walletKey)}
20 h:=c.HTTP;if h==nil{h=&http.Client{Timeout:10*time.Second}}
21 resp,err:=h.Do(req);if err!=nil{return nil,err};defer resp.Body.Close()
22 raw,err:=io.ReadAll(io.LimitReader(resp.Body,1<<20));if err!=nil{return nil,err}
23 if resp.StatusCode<200||resp.StatusCode>=300{return nil,errors.New("engine HTTP error")}
24 return raw,nil
25}Read-only source · Select a line number to link directly · Raw download above
