-
Notifications
You must be signed in to change notification settings - Fork 646
/
requester.go
121 lines (104 loc) · 2.78 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
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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package rpc
import (
"bytes"
"context"
"fmt"
"net/http"
"net/url"
rpc "github.com/gorilla/rpc/v2/json2"
)
type requester interface {
sendJSONRPCRequest(
ctx context.Context,
endpoint string,
headers http.Header,
queryParams url.Values,
method string,
params interface{},
reply interface{},
) error
}
type jsonRPCRequester struct {
uri string
client http.Client
}
func newRPCRequester(uri string) requester {
return &jsonRPCRequester{
uri: uri,
client: *http.DefaultClient,
}
}
func (requester jsonRPCRequester) sendJSONRPCRequest(
ctx context.Context,
endpoint string,
headers http.Header,
queryParams url.Values,
method string,
params interface{},
reply interface{},
) error {
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)
}
queryParamsStr := queryParams.Encode()
if len(queryParamsStr) > 0 {
queryParamsStr = "?" + queryParamsStr
}
url := fmt.Sprintf("%s%s%s", requester.uri, endpoint, queryParamsStr)
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(requestBodyBytes))
if err != nil {
return fmt.Errorf("problem while creating JSON RPC POST request to %s: %s", url, err)
}
req.Header = headers
req.Header.Set("Content-Type", "application/json")
resp, err := requester.client.Do(req)
if err != nil {
return fmt.Errorf("problem while making JSON RPC POST request to %s: %w", 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(ctx context.Context, method string, params interface{}, reply interface{}, options ...Option) error
}
type avalancheEndpointRequester struct {
requester requester
endpoint, base string
}
func NewEndpointRequester(uri, endpoint, base string) EndpointRequester {
return &avalancheEndpointRequester{
requester: newRPCRequester(uri),
endpoint: endpoint,
base: base,
}
}
func (e *avalancheEndpointRequester) SendRequest(
ctx context.Context,
method string,
params interface{},
reply interface{},
options ...Option,
) error {
ops := NewOptions(options)
return e.requester.sendJSONRPCRequest(
ctx,
e.endpoint,
ops.Headers(),
ops.QueryParams(),
fmt.Sprintf("%s.%s", e.base, method),
params,
reply,
)
}