-
Notifications
You must be signed in to change notification settings - Fork 7
/
request.go
62 lines (50 loc) · 974 Bytes
/
request.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
package infura
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
)
type Request struct {
Request *http.Request
Error error
Data interface{}
}
func (r *Request) Call() error {
if r.Error != nil {
return r.Error
}
resp, err := http.DefaultClient.Do(r.Request)
if err != nil {
return err
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(bodyBytes, r.Data)
if err != nil {
return err
}
return nil
}
func (i *Infura) newRequest(input input, output interface{}) (req *Request) {
args := map[string]interface{}{
"jsonrpc": "2.0",
"id": 1,
"method": input.method(),
"params": input.params(),
}
body, err := json.Marshal(args)
if err != nil {
body = []byte{}
}
httpReq, err := http.NewRequest("POST", i.network.URL()+i.projectID, bytes.NewReader(body))
req = &Request{
Request: httpReq,
Error: err,
Data: output,
}
return
}