|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + |
| 7 | + "github.com/creachadair/jrpc2" |
| 8 | + "github.com/creachadair/jrpc2/jhttp" |
| 9 | + |
| 10 | + "github.com/stellar/stellar-rpc/protocol" |
| 11 | +) |
| 12 | + |
| 13 | +type Client struct { |
| 14 | + url string |
| 15 | + cli *jrpc2.Client |
| 16 | + httpClient *http.Client |
| 17 | +} |
| 18 | + |
| 19 | +func NewClient(url string, httpClient *http.Client) *Client { |
| 20 | + c := &Client{url: url, httpClient: httpClient} |
| 21 | + c.refreshClient() |
| 22 | + return c |
| 23 | +} |
| 24 | + |
| 25 | +func (c *Client) Close() error { |
| 26 | + return c.cli.Close() |
| 27 | +} |
| 28 | + |
| 29 | +func (c *Client) refreshClient() { |
| 30 | + if c.cli != nil { |
| 31 | + c.cli.Close() |
| 32 | + } |
| 33 | + var opts *jhttp.ChannelOptions |
| 34 | + if c.httpClient != nil { |
| 35 | + opts = &jhttp.ChannelOptions{ |
| 36 | + Client: c.httpClient, |
| 37 | + } |
| 38 | + } |
| 39 | + ch := jhttp.NewChannel(c.url, opts) |
| 40 | + c.cli = jrpc2.NewClient(ch, nil) |
| 41 | +} |
| 42 | + |
| 43 | +func (c *Client) callResult(ctx context.Context, method string, params, result any) error { |
| 44 | + err := c.cli.CallResult(ctx, method, params, result) |
| 45 | + if err != nil { |
| 46 | + // This is needed because of https://github.com/creachadair/jrpc2/issues/118 |
| 47 | + c.refreshClient() |
| 48 | + } |
| 49 | + return err |
| 50 | +} |
| 51 | + |
| 52 | +func (c *Client) GetEvents(ctx context.Context, |
| 53 | + request protocol.GetEventsRequest, |
| 54 | +) (protocol.GetEventsResponse, error) { |
| 55 | + var result protocol.GetEventsResponse |
| 56 | + err := c.callResult(ctx, protocol.GetEventsMethodName, request, &result) |
| 57 | + if err != nil { |
| 58 | + return protocol.GetEventsResponse{}, err |
| 59 | + } |
| 60 | + return result, nil |
| 61 | +} |
| 62 | + |
| 63 | +func (c *Client) GetFeeStats(ctx context.Context) (protocol.GetFeeStatsResponse, error) { |
| 64 | + var result protocol.GetFeeStatsResponse |
| 65 | + err := c.callResult(ctx, protocol.GetFeeStatsMethodName, nil, &result) |
| 66 | + if err != nil { |
| 67 | + return protocol.GetFeeStatsResponse{}, err |
| 68 | + } |
| 69 | + return result, nil |
| 70 | +} |
| 71 | + |
| 72 | +func (c *Client) GetHealth(ctx context.Context) (protocol.GetHealthResponse, error) { |
| 73 | + var result protocol.GetHealthResponse |
| 74 | + err := c.callResult(ctx, protocol.GetHealthMethodName, nil, &result) |
| 75 | + if err != nil { |
| 76 | + return protocol.GetHealthResponse{}, err |
| 77 | + } |
| 78 | + return result, nil |
| 79 | +} |
| 80 | + |
| 81 | +func (c *Client) GetLatestLedger(ctx context.Context) (protocol.GetLatestLedgerResponse, error) { |
| 82 | + var result protocol.GetLatestLedgerResponse |
| 83 | + err := c.callResult(ctx, protocol.GetLatestLedgerMethodName, nil, &result) |
| 84 | + if err != nil { |
| 85 | + return protocol.GetLatestLedgerResponse{}, err |
| 86 | + } |
| 87 | + return result, nil |
| 88 | +} |
| 89 | + |
| 90 | +func (c *Client) GetLedgerEntries(ctx context.Context, |
| 91 | + request protocol.GetLedgerEntriesRequest, |
| 92 | +) (protocol.GetLedgerEntriesResponse, error) { |
| 93 | + var result protocol.GetLedgerEntriesResponse |
| 94 | + err := c.callResult(ctx, protocol.GetLedgerEntriesMethodName, request, &result) |
| 95 | + if err != nil { |
| 96 | + return protocol.GetLedgerEntriesResponse{}, err |
| 97 | + } |
| 98 | + return result, nil |
| 99 | +} |
| 100 | + |
| 101 | +func (c *Client) GetLedgers(ctx context.Context, |
| 102 | + request protocol.GetLedgersRequest, |
| 103 | +) (protocol.GetLedgersResponse, error) { |
| 104 | + var result protocol.GetLedgersResponse |
| 105 | + err := c.callResult(ctx, protocol.GetLedgersMethodName, request, &result) |
| 106 | + if err != nil { |
| 107 | + return protocol.GetLedgersResponse{}, err |
| 108 | + } |
| 109 | + return result, nil |
| 110 | +} |
| 111 | + |
| 112 | +func (c *Client) GetNetwork(ctx context.Context, |
| 113 | +) (protocol.GetNetworkResponse, error) { |
| 114 | + // phony |
| 115 | + var request protocol.GetNetworkRequest |
| 116 | + var result protocol.GetNetworkResponse |
| 117 | + err := c.callResult(ctx, protocol.GetNetworkMethodName, request, &result) |
| 118 | + if err != nil { |
| 119 | + return protocol.GetNetworkResponse{}, err |
| 120 | + } |
| 121 | + return result, nil |
| 122 | +} |
| 123 | + |
| 124 | +func (c *Client) GetTransaction(ctx context.Context, |
| 125 | + request protocol.GetTransactionRequest, |
| 126 | +) (protocol.GetTransactionResponse, error) { |
| 127 | + var result protocol.GetTransactionResponse |
| 128 | + err := c.callResult(ctx, protocol.GetTransactionMethodName, request, &result) |
| 129 | + if err != nil { |
| 130 | + return protocol.GetTransactionResponse{}, err |
| 131 | + } |
| 132 | + return result, nil |
| 133 | +} |
| 134 | + |
| 135 | +func (c *Client) GetTransactions(ctx context.Context, |
| 136 | + request protocol.GetTransactionsRequest, |
| 137 | +) (protocol.GetTransactionsResponse, error) { |
| 138 | + var result protocol.GetTransactionsResponse |
| 139 | + err := c.callResult(ctx, protocol.GetTransactionsMethodName, request, &result) |
| 140 | + if err != nil { |
| 141 | + return protocol.GetTransactionsResponse{}, err |
| 142 | + } |
| 143 | + return result, nil |
| 144 | +} |
| 145 | + |
| 146 | +func (c *Client) GetVersionInfo(ctx context.Context) (protocol.GetVersionInfoResponse, error) { |
| 147 | + var result protocol.GetVersionInfoResponse |
| 148 | + err := c.callResult(ctx, protocol.GetVersionInfoMethodName, nil, &result) |
| 149 | + if err != nil { |
| 150 | + return protocol.GetVersionInfoResponse{}, err |
| 151 | + } |
| 152 | + return result, nil |
| 153 | +} |
| 154 | + |
| 155 | +func (c *Client) SendTransaction(ctx context.Context, |
| 156 | + request protocol.SendTransactionRequest, |
| 157 | +) (protocol.SendTransactionResponse, error) { |
| 158 | + var result protocol.SendTransactionResponse |
| 159 | + err := c.callResult(ctx, protocol.SendTransactionMethodName, request, &result) |
| 160 | + if err != nil { |
| 161 | + return protocol.SendTransactionResponse{}, err |
| 162 | + } |
| 163 | + return result, nil |
| 164 | +} |
| 165 | + |
| 166 | +func (c *Client) SimulateTransaction(ctx context.Context, |
| 167 | + request protocol.SimulateTransactionRequest, |
| 168 | +) (protocol.SimulateTransactionResponse, error) { |
| 169 | + var result protocol.SimulateTransactionResponse |
| 170 | + err := c.callResult(ctx, protocol.SimulateTransactionMethodName, request, &result) |
| 171 | + if err != nil { |
| 172 | + return protocol.SimulateTransactionResponse{}, err |
| 173 | + } |
| 174 | + return result, nil |
| 175 | +} |
0 commit comments