jsonrpc

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2021 License: MIT Imports: 4 Imported by: 0

README

JSON-RPC request abstraction

Simple abstraction for JSON-RPC requests.

Initialize the client:

import "github.com/Pantani/jsonrpc"

client := jsonrpc.InitRpcClient("http://127.0.0.1:8080")

Methods

Rpc Call
var txs []Transaction
err := client.RpcCall(&txs, "getTransactionsByAddress", []string{"d48182276127b149a9710e78c436fb4bc1c4dc0b", "25"})
Rpc Batch Call
var requests jsonrpc.RpcRequests
for _, hash := range hashes {
    requests = append(requests, &jsonrpc.RpcRequest{
        Method: "GetTransaction",
        Params: []string{hash},
    })
}
responses, err := client.RpcBatchCall(requests)
if err != nil {
    panic(err)
}
for _, result := range responses {
    var tx Transaction
    if mapstructure.Decode(result.Result, &tx) != nil {
        continue
    }
    txs = append(txs, tx)
}

Documentation

Index

Constants

View Source
const (
	// Version default JSON-RPC version.
	Version = "2.0"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type RPCError

type RPCError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

RPCError represents the JSON-RPC error object.

type RPCRequest

type RPCRequest struct {
	JSONRPC string      `json:"jsonrpc"`
	Method  string      `json:"method"`
	Params  interface{} `json:"params,omitempty"`
	ID      int64       `json:"id,omitempty"`
}

RPCRequest represents the JSON-RPC request object.

type RPCRequests

type RPCRequests []*RPCRequest

RPCRequests represents a list of JSON-RPC requests.

type RPCResponse

type RPCResponse struct {
	JSONRPC string      `json:"jsonrpc"`
	Error   *RPCError   `json:"error,omitempty"`
	Result  interface{} `json:"result,omitempty"`
	ID      int64       `json:"id,omitempty"`
}

RPCResponse represents the JSON-RPC response object.

func (*RPCResponse) GetObject

func (r *RPCResponse) GetObject(toType interface{}) error

GetObject bind the request result into the struct. It returns an error if occurs.

type Request

type Request struct {
	request.Request
}

Request represents a client request wrapper.

func InitRPCClient

func InitRPCClient(baseURL string) Request

InitRPCClient initialize the rpc client. It returns the rpc request object.

func (*Request) RPCBatchCall

func (r *Request) RPCBatchCall(requests RPCRequests) ([]RPCResponse, error)

RPCBatchCall make a batch of JSON-RPC requests. E.g.:

var requests RpcRequests
for _, hash := range hashes {
	requests = append(requests, &RpcRequest{
		Method: "GetTransaction",
		Params: []string{hash},
	})
}
responses, err := c.RpcBatchCall(requests)

It returns the result and an error if occurs.

func (*Request) RPCBatchCallWithContext

func (r *Request) RPCBatchCallWithContext(ctx context.Context, requests RPCRequests) ([]RPCResponse, error)

RPCBatchCallWithContext make a batch of JSON-RPC requests with context. E.g.:

var requests RpcRequests
for _, hash := range hashes {
	requests = append(requests, &RpcRequest{
		Method: "GetTransaction",
		Params: []string{hash},
	})
}
responses, err := c.RpcBatchCall(context.Background(), requests)

It returns the result and an error if occurs.

func (*Request) RPCCall

func (r *Request) RPCCall(result interface{}, method string, params interface{}) error

RPCCall make a JSON-RPC request and bind the result into the generic interface. E.g.:

var tx []Tx
err = c.RpcCall(&tx, "getTransactionsByAddress", []string{address, "25"})

It returns an error if occurs.

func (*Request) RPCCallWithContext

func (r *Request) RPCCallWithContext(ctx context.Context, result interface{}, method string, params interface{}) error

RPCCallWithContext make a JSON-RPC request and bind the result into the generic interface passing the context. E.g.:

var tx []Tx
err = c.RpcCall(context.Background(), &tx, "getTransactionsByAddress", []string{address, "25"})

It returns an error if occurs.

Jump to

Keyboard shortcuts

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