-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
150 lines (123 loc) · 3.25 KB
/
api.go
File metadata and controls
150 lines (123 loc) · 3.25 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
package eos_go_trace_api_plugin_wrapper
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/eoscanada/eos-go"
"github.com/eoscanada/eos-go/token"
"io"
"net"
"net/http"
"strings"
"time"
)
type TraceAPI struct{
HttpClient *http.Client
BaseURL string
// Header is one or more headers to be added to all outgoing calls
Header http.Header
}
func New(baseURL string) *TraceAPI {
api := &TraceAPI{
HttpClient: &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
DisableKeepAlives: true, // default behavior, because of `nodeos`'s lack of support for Keep alives.
},
},
BaseURL: strings.TrimRight(baseURL, "/"),
Header: make(http.Header),
}
return api
}
func (api *TraceAPI) GetBlockByID(num uint32) (out *BlockResp, err error) {
err = api.call("trace_api", "get_block", eos.M{"block_num": fmt.Sprintf("%d", num)}, &out)
return
}
func (api *TraceAPI) call(baseAPI string, endpoint string, body interface{}, out interface{}) error {
jsonBody, err := enc(body)
if err != nil {
return err
}
targetURL := fmt.Sprintf("%s/v1/%s/%s", api.BaseURL, baseAPI, endpoint)
req, err := http.NewRequest("POST", targetURL, jsonBody)
if err != nil {
return fmt.Errorf("NewRequest: %w", err)
}
for k, v := range api.Header {
if req.Header == nil {
req.Header = http.Header{}
}
req.Header[k] = append(req.Header[k], v...)
}
resp, err := api.HttpClient.Do(req)
if err != nil {
return fmt.Errorf("%s: %w", req.URL.String(), err)
}
defer resp.Body.Close()
var cnt bytes.Buffer
_, err = io.Copy(&cnt, resp.Body)
if err != nil {
return fmt.Errorf("Copy: %w", err)
}
if resp.StatusCode == 404 {
var apiErr eos.APIError
if err := json.Unmarshal(cnt.Bytes(), &apiErr); err != nil {
return eos.ErrNotFound
}
return apiErr
}
if resp.StatusCode > 299 {
var apiErr eos.APIError
if err := json.Unmarshal(cnt.Bytes(), &apiErr); err != nil {
return fmt.Errorf("%s: status code=%d, body=%s", req.URL.String(), resp.StatusCode, cnt.String())
}
// Handle cases where some API calls (/v1/chain/get_account for example) returns a 500
// error when retrieving data that does not exist.
if apiErr.IsUnknownKeyError() {
return eos.ErrNotFound
}
return apiErr
}
if err := json.Unmarshal(cnt.Bytes(), &out); err != nil {
return fmt.Errorf("Unmarshal: %w", err)
}
return nil
}
func enc(v interface{}) (io.Reader, error) {
if v == nil {
return nil, nil
}
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
err := encoder.Encode(v)
if err != nil {
return nil, err
}
return buffer, nil
}
func DecodeTransfer(hexString string) (*token.Transfer, error){
rawData, err := hex.DecodeString(hexString)
if err != nil {
fmt.Println(err)
panic(err)
}
decoder := eos.NewDecoder(rawData)
decoder.DecodeActions(false)
transfer := &token.Transfer{}
err = decoder.Decode(transfer)
if err != nil {
panic(err)
}
return transfer, nil
}