-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrpc.go
65 lines (52 loc) · 1.43 KB
/
rpc.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
package jitorpc
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
func (c *JitoJsonRpcClient) sendRequest(endpoint, method string, params interface{}) (json.RawMessage, error) {
url := fmt.Sprintf("%s%s", c.BaseURL, endpoint)
request := JsonRpcRequest{
JsonRpc: "2.0",
ID: 1,
Method: method,
Params: params,
}
requestBody, err := json.Marshal(request)
if err != nil {
return nil, fmt.Errorf("error marshaling request: %w", err)
}
if c.isDebugEnabled() {
fmt.Printf("Sending request to: %s\n", url)
fmt.Printf("Request body: %s\n", string(requestBody))
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
if c.UUID != "" {
req.Header.Set("x-jito-auth", c.UUID)
}
resp, err := c.Client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %w", err)
}
defer resp.Body.Close()
if c.isDebugEnabled() {
fmt.Printf("Response status: %s\n", resp.Status)
}
var jsonResp JsonRpcResponse
err = json.NewDecoder(resp.Body).Decode(&jsonResp)
if err != nil {
return nil, fmt.Errorf("error decoding response: %w", err)
}
if jsonResp.Error != nil {
return nil, fmt.Errorf("RPC error: %s", jsonResp.Error.Message)
}
if c.isDebugEnabled() {
fmt.Printf("Response body: %s\n", string(jsonResp.Result))
}
return jsonResp.Result, nil
}