jsonrpc2

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2019 License: MIT Imports: 9 Imported by: 0

README

jsonrpc2

It is simple jsonrpc2 client.

Usage:

	client := NewClient(serverRpcUrl)
	var val int
	err := client.Call(context.Background(), "Divide", struct {
		A,B int
	}{4,2}, &val)

Documentation

Index

Constants

View Source
const (
	// ErrCodeServer indicates that client could not unmarshal response from server.
	ErrCodeParseError = -32700
)

Variables

This section is empty.

Functions

func WithHttpClient

func WithHttpClient(httpClient HTTPClient) clientOption

WithHttpClient is an option which sets http client implementation for jsonrpc2 client.

Types

type Client

type Client interface {
	Call(ctx context.Context, methodName string, params interface{}, result interface{}) error
}

Client represents jsonrpc caller interface. It have only one method call which satisfies simple case of jsonrpc2 usage.

func NewClient

func NewClient(rpcEndpointURL string, options ...clientOption) Client

NewClient returns jsonrpc2 client.

type HTTPClient

type HTTPClient interface {
	Post(ctx context.Context, url string, body []byte) ([]byte, error)
}

HTTPClient is a convenient interface for http worker. It allows to use a custom http transport level implementation.

func NewHttpClient

func NewHttpClient(c *http.Client) HTTPClient

NewHttpClient returns wiring of standard http.Client.

type Request

type Request struct {
	Version string      `json:"jsonrpc"`
	Method  string      `json:"method"`
	Params  interface{} `json:"params"`
	Id      uint64      `json:"id"`
}

Request represents a jsonrpc2 request.

func (Request) MarshalEasyJSON

func (v Request) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (Request) MarshalJSON

func (v Request) MarshalJSON() ([]byte, error)

MarshalJSON supports json.Marshaler interface

func (*Request) UnmarshalEasyJSON

func (v *Request) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*Request) UnmarshalJSON

func (v *Request) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type Response

type Response struct {
	Version string           `json:"jsonrpc"`
	Result  *json.RawMessage `json:"result"`
	Error   *json.RawMessage `json:"error"`
}

Request represents a jsonrpc2 response.

func (Response) MarshalEasyJSON

func (v Response) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (Response) MarshalJSON

func (v Response) MarshalJSON() ([]byte, error)

MarshalJSON supports json.Marshaler interface

func (*Response) UnmarshalEasyJSON

func (v *Response) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*Response) UnmarshalJSON

func (v *Response) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type ResponseError

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

ResponseError is a struct which represents a typical jsonrpc2 error according to specification.

func (*ResponseError) Error

func (e *ResponseError) Error() string

Error returns an error message of ResponseError.

func (ResponseError) MarshalEasyJSON

func (v ResponseError) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (ResponseError) MarshalJSON

func (v ResponseError) MarshalJSON() ([]byte, error)

MarshalJSON supports json.Marshaler interface

func (*ResponseError) UnmarshalEasyJSON

func (v *ResponseError) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*ResponseError) UnmarshalJSON

func (v *ResponseError) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

Jump to

Keyboard shortcuts

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