jrc

package module
v0.0.0-...-c4e9041 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 13, 2022 License: MIT Imports: 6 Imported by: 2

README

JRC: Json Rpc Client

A JSON RPC 2.0 client focused on blockchain clients

This project was primarily created to support Hive and Hive Engine projects, but should work fine as a basic client for any JSON RPC 2.0 server

Overview

  1. Instantiate server
  2. Assemble RpcRequest(s)
  3. Execute the request(s)
  4. Parse the return

One of the project goals is to allow for optimal speeds when handling large amounts of return data, so minimal parsing is done by this client. If the caller so chooses, the unparsed RPC response ([][]byte) can be returned with Server.ExecBatchFast

Instantiate a server to query

With default options:

srv, _ := jrc.NewServer("https://api.hive-engine.com")

With custom max connections:

srv, _ := jrc.NewServer("https://api.hive-engine.com", jrc.MaxCon(10))

Modify existing server options:

srv.SetOption(jrc.MaxBatch(1000), jrc.MaxCon(10))

Creating requests

Params will be marshalled to JSON

A single request:

r := jrc.RpcRequest{Method: query.method, JsonRpc: "2.0", Id: 1, Params: query}

Many requests (jrc will batch according to MaxBatch):

    var rs jrc.RPCRequests
    for i, query := range queries {
        r := &jrc.RpcRequest{Method: query.method, JsonRpc: "2.0", Id: i, Params: query}
        rs = append(rs, q)
    }
Executing requests

A single request:

resp, _ := srv.Exec(r)

Multiple requests:

resps, _ := srv.ExecBatch(rs)

Multiple requests, no parsing (returns [][]byte):

resps, _ := srv.ExecBatchFast(rs)

Putting it all together
import "github.com/cfoxon/jrc"

func main() {
    //create a query
    query := struct {
        Contract string   `json:"contract"`
        Table    string   `json:"table"`
        Query    struct{} `json:"query"`
    }{
        Contract: "market",
        Table:    "metrics",
    }
    
    //instantiate a server to  be queried
    srv, _ := jrc.NewServer("https://ha.herpc.dtools.dev/contracts")
    
    //put the query in a request
    r := jrc.RpcRequest{JsonRpc: "2.0", Id: 1, Method: "find", Params: query}
    
    //execute the request and get the return value
    resp, _ := srv.Exec(r)
    
    //do something with the returned data
    print(string(resp.Result))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Address

func Address(s string) func(server *Server) error

Address sets the url of the Server

func MaxBatch

func MaxBatch(n int) func(server *Server) error

MaxBatch sets the maximum number of RPCs to batch into a single call

func MaxCon

func MaxCon(n int) func(server *Server) error

MaxCon sets the maximum number of simultaneous connections

Types

type RPCRequests

type RPCRequests []*RpcRequest

type RpcError

type RpcError struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
}

RpcError holds decoded RPC errors

type RpcRequest

type RpcRequest struct {
	JsonRpc string      `json:"jsonrpc"`
	Id      int         `json:"id"`
	Method  string      `json:"method"`
	Params  interface{} `json:"params,omitempty"`
}

RpcRequest contains a JSON RPC 2.0 request to be submitted to a Server

type RpcResponse

type RpcResponse struct {
	JSONRPC string          `json:"jsonrpc"`
	Result  json.RawMessage `json:"result,omitempty"`
	Error   *RpcError       `json:"error,omitempty"`
	ID      int             `json:"id"`
}

RpcResponse contains an RPC response with the Result field left un-decoded

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server contains information related to connecting to an RPC server

func NewServer

func NewServer(addr string, options ...func(*Server) error) (*Server, error)

NewServer creates a target for clients

func (*Server) Exec

func (srv *Server) Exec(r RpcRequest) (*RpcResponse, error)

Exec executes a single remote procedure call

func (*Server) ExecBatch

func (srv *Server) ExecBatch(rs RPCRequests) ([]RpcResponse, error)

ExecBatch executes a batch of calls and parses the JSON RPC 2.0 portion of the body

the Result field is left as json.RawMessage for further parsing by the caller

func (*Server) ExecBatchFast

func (srv *Server) ExecBatchFast(rs RPCRequests) ([][]byte, error)

ExecBatchFast returns a slice of []byte containing the responses to the remote procedure calls

func (*Server) SetOption

func (srv *Server) SetOption(options ...func(*Server) error) error

SetOption changes server configuration with options

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL