-
Notifications
You must be signed in to change notification settings - Fork 658
/
requester.go
88 lines (73 loc) · 2.46 KB
/
requester.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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package rpc
import (
"bytes"
"fmt"
"net/http"
"strings"
"time"
rpc "github.com/gorilla/rpc/v2/json2"
)
type Requester interface {
SendJSONRPCRequest(endpoint string, method string, params interface{}, reply interface{}) error
}
type jsonRPCRequester struct {
uri string
client http.Client
}
func NewRPCRequester(uri string, requestTimeout time.Duration) Requester {
return &jsonRPCRequester{
uri: uri,
client: http.Client{
Timeout: requestTimeout,
},
}
}
func (requester jsonRPCRequester) SendJSONRPCRequest(endpoint string, method string, params interface{}, reply interface{}) error {
// Golang has a nasty & subtle behaviour where duplicated '//' in the URL is treated as GET, even if it's POST
// https://stackoverflow.com/questions/23463601/why-golang-treats-my-post-request-as-a-get-one
endpoint = strings.TrimLeft(endpoint, "/")
requestBodyBytes, err := rpc.EncodeClientRequest(method, params)
if err != nil {
return fmt.Errorf("problem marshaling request to endpoint '%v' with method '%v' and params '%v': %w", endpoint, method, params, err)
}
url := fmt.Sprintf("%v/%v", requester.uri, endpoint)
resp, err := requester.client.Post(url, "application/json", bytes.NewBuffer(requestBodyBytes))
if err != nil {
return fmt.Errorf("problem while making JSON RPC POST request to %s: %s", url, err)
}
statusCode := resp.StatusCode
// Return an error for any non successful status code
if statusCode < 200 || statusCode > 299 {
// Drop any error during close to report the original error
_ = resp.Body.Close()
return fmt.Errorf("received status code '%v'", statusCode)
}
if err := rpc.DecodeClientResponse(resp.Body, reply); err != nil {
return err
}
return resp.Body.Close()
}
type EndpointRequester interface {
SendRequest(method string, params interface{}, reply interface{}) error
}
type avalancheEndpointRequester struct {
requester Requester
endpoint, base string
}
func NewEndpointRequester(uri, endpoint, base string, requestTimeout time.Duration) EndpointRequester {
return &avalancheEndpointRequester{
requester: NewRPCRequester(uri, requestTimeout),
endpoint: endpoint,
base: base,
}
}
func (e *avalancheEndpointRequester) SendRequest(method string, params interface{}, reply interface{}) error {
return e.requester.SendJSONRPCRequest(
e.endpoint,
fmt.Sprintf("%s.%s", e.base, method),
params,
reply,
)
}