jsonrpc

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2023 License: MIT Imports: 8 Imported by: 2

Documentation

Overview

Package jsonrpc is a minimal JSON-RPC implementation

Package jsonrpc is a minimal JSON-RPC implementation

Index

Constants

View Source
const (
	ErrParse          int = -32700
	ErrInvalidRequest int = -32600
	ErrMethodNotFound int = -32601
	ErrInvalidParams  int = -32602
	ErrInternal       int = -32603
)

As per JSON-RPC 2.0 Specification https://www.jsonrpc.org/specification#error_object

Variables

This section is empty.

Functions

func SendJSONRPCRequestAndParseResult

func SendJSONRPCRequestAndParseResult(req JSONRPCRequest, url string, reply interface{}) (err error)

SendJSONRPCRequestAndParseResult sends the request and decodes the response into the reply interface. If the JSON-RPC response contains an Error property, the it's returned as this function's error.

Types

type DataError added in v0.4.3

type DataError interface {
	Error() string          // returns the message
	ErrorData() interface{} // returns the error data
}

type Error added in v0.4.3

type Error interface {
	Error() string  // returns the message
	ErrorCode() int // returns the code
}

Error wraps RPC errors, which contain an error code in addition to the message.

type JSONRPCError

type JSONRPCError struct {
	// A Number that indicates the error type that occurred.
	Code int `json:"code"`

	// A String providing a short description of the error.
	// The message SHOULD be limited to a concise single sentence.
	Message string `json:"message"`

	// A Primitive or Structured value that contains additional information about the error.
	Data interface{} `json:"data,omitempty"` /* optional */
}

JSONRPCError as per spec: https://www.jsonrpc.org/specification#error_object

func (*JSONRPCError) Error

func (err *JSONRPCError) Error() string

func (*JSONRPCError) ErrorCode added in v0.4.3

func (err *JSONRPCError) ErrorCode() int

func (*JSONRPCError) ErrorData added in v0.4.3

func (err *JSONRPCError) ErrorData() interface{}

type JSONRPCRequest

type JSONRPCRequest struct {
	ID      interface{}   `json:"id"`
	Method  string        `json:"method"`
	Params  []interface{} `json:"params"`
	Version string        `json:"jsonrpc,omitempty"`
}

func NewJSONRPCRequest

func NewJSONRPCRequest(id interface{}, method string, args interface{}) *JSONRPCRequest

type JSONRPCResponse

type JSONRPCResponse struct {
	ID      interface{}     `json:"id"`
	Result  json.RawMessage `json:"result,omitempty"`
	Error   *JSONRPCError   `json:"error,omitempty"`
	Version string          `json:"jsonrpc"`
}

func NewJSONRPCErrorResponse

func NewJSONRPCErrorResponse(id interface{}, code int, message string) *JSONRPCResponse

func NewJSONRPCResponse

func NewJSONRPCResponse(id interface{}, result json.RawMessage) *JSONRPCResponse

func SendJSONRPCRequest

func SendJSONRPCRequest(req JSONRPCRequest, url string) (res *JSONRPCResponse, err error)

SendJSONRPCRequest sends the request to URL and returns the general JsonRpcResponse, or an error (note: not the JSONRPCError)

func SendNewJSONRPCRequest added in v0.2.1

func SendNewJSONRPCRequest(id interface{}, method string, args interface{}, url string) (res *JSONRPCResponse, err error)

SendNewJSONRPCRequest constructs a request and sends it to the URL

type MockJSONRPCServer added in v0.3.1

type MockJSONRPCServer struct {
	Handlers       map[string]func(req *JSONRPCRequest) (interface{}, error)
	RequestCounter sync.Map

	URL string
	// contains filtered or unexported fields
}

func NewMockJSONRPCServer added in v0.3.1

func NewMockJSONRPCServer() *MockJSONRPCServer

func (*MockJSONRPCServer) GetRequestCount added in v0.4.4

func (s *MockJSONRPCServer) GetRequestCount(method string) int

func (*MockJSONRPCServer) IncrementRequestCounter added in v0.4.3

func (s *MockJSONRPCServer) IncrementRequestCounter(method string)

func (*MockJSONRPCServer) SetHandler added in v0.4.3

func (s *MockJSONRPCServer) SetHandler(method string, handler func(req *JSONRPCRequest) (interface{}, error))

Jump to

Keyboard shortcuts

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