jsonrpc

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Provides a general JSON-RPC client, decodes Diem extension fields.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	// Call with requests. When given multiple requests
	Call(...*Request) (map[RequestID]*Response, error)
}

Client is interface of the JSON-RPC client

func NewClient

func NewClient(url string) Client

NewClient creates a new JSON-RPC Client. Creates http.Transport with 3 max idle connections and 30 seconds idle timeout, and 30 seconds connection timeout NewClientWithHTTPClient can be used to override the connection timeout NewClientWithTransport can be used to override the underlying transport

func NewClientWithHTTPClient

func NewClientWithHTTPClient(url string, httpClient *http.Client) Client

NewClientWithHTTPClient creates a new JSON-RPC Client with given URL and `*http.Client`

func NewClientWithTransport

func NewClientWithTransport(url string, t *http.Transport) Client

NewClientWithTransport creates a new JSON-RPC Client with given URL and `*http.Transport`

type Error

type Error struct {
	ErrorType ErrorType
	Cause     error
}

Error is a wrap of a type and underlining `Cause` error

func (*Error) Error

func (e *Error) Error() string

Error returns `ErrorType` + `Cause#Error()` as message

type ErrorType

type ErrorType string

ErrorType is type of the `Error`, which maybe caused by different underlining errors

const (
	SerializeRequestJsonError    ErrorType = "serialize request json failed"
	HttpCallError                ErrorType = "http call failed"
	ReadHttpResponseBodyError    ErrorType = "read http response body failed"
	ParseResponseJsonError       ErrorType = "parse response json failed"
	ParseResponseResultJsonError ErrorType = "parse response result json failed"
	InvalidJsonRpcResponseError  ErrorType = "invalid JSON-RPC response: missing result / error field"
)

type Method

type Method string

Method is type of JSON-RPC method

type Param

type Param interface{}

Param is type of JSON-RPC params list

type Request

type Request struct {
	Method  Method    `json:"method"`
	Params  []Param   `json:"params"`
	ID      RequestID `json:"id"`
	JsonRpc string    `json:"jsonrpc"`
}

Request is type of JSON-RPC request struct

func NewRequest

func NewRequest(m Method, params ...Param) *Request

NewRequest creates `Request` with default `JsonRpc` = "2.0" and ID = 1

func NewRequestWithID

func NewRequestWithID(id RequestID, m Method, params ...Param) *Request

NewRequestWithID creates `Request` with default `JsonRpc` = "2.0"

func (*Request) ToString

func (r *Request) ToString() string

ToString returns json format of the request

type RequestID

type RequestID uint

RequestID is type for request ID.

type Response

type Response struct {
	JsonRpc                 string           `json:"jsonrpc"`
	ID                      *RequestID       `json:"id"`
	Result                  *json.RawMessage `json:"result"`
	Error                   *ResponseError   `json:"error"`
	DiemChainID             byte             `json:"diem_chain_id"`
	DiemLedgerTimestampusec uint64           `json:"diem_ledger_timestampusec"`
	DiemLedgerVersion       uint64           `json:"diem_ledger_version"`
}

Response is type of JSON-RPC response struct

func (*Response) UnmarshalResult

func (r *Response) UnmarshalResult(result interface{}) (bool, error)

UnmarshalResult unmarshals result json into given struct. Returns true, nil for success unmarshal, otherwise first bool will always be false. Returns false, nil if `Result` is nil.

func (*Response) Validate

func (r *Response) Validate() error

Validate validates response data with JSON-RPC 2.0 spec

type ResponseError

type ResponseError struct {
	Code    int32       `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

ResponseError is type of JSON-RPC response error, it implements error interface.

func (*ResponseError) Error

func (e *ResponseError) Error() string

Error implements error interface, returns `Code` + `Message`

Directories

Path Synopsis
Provides a simple json-rpc client stub for testing client without connecting to remote server.
Provides a simple json-rpc client stub for testing client without connecting to remote server.

Jump to

Keyboard shortcuts

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