-
Notifications
You must be signed in to change notification settings - Fork 32
/
requester.go
94 lines (77 loc) · 2.65 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
// 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"
"strings"
rpc "github.com/gorilla/rpc/v2/json2"
)
type Requester interface {
SendJSONRPCRequest(ctx context.Context, endpoint string, 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, 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)
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.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{}) 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{}) error {
return e.requester.SendJSONRPCRequest(
ctx,
e.endpoint,
fmt.Sprintf("%s.%s", e.base, method),
params,
reply,
)
}