jsonrpc2

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ParseError     = -32700
	InvalidRequest = -32600
	MethodNotFound = -32601
	InvalidParams  = -32602
	InternalError  = -32603
)

Standard error codes as defined in the JSON-RPC 2.0 specification.

View Source
const Version = "2.0"

Version represents the JSON-RPC protocol version supported by this package.

Variables

This section is empty.

Functions

func HTTPHandler added in v0.0.2

func HTTPHandler(handler Handler) http.Handler

HTTPHandler adapts a JSON-RPC Handler to a net/http.Handler.

Types

type Caller added in v0.0.2

type Caller interface {
	Call(ctx context.Context, requests ...*Request) ([]*Response, error)
}

Caller issues JSON-RPC requests and returns their responses.

type Error

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

Error represents a JSON-RPC 2.0 error object.

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface for JSON-RPC error objects.

type HTTPClient

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

HTTPClient issues JSON-RPC requests over HTTP.

func NewHTTPClient

func NewHTTPClient(endpoint string, client *http.Client) *HTTPClient

NewHTTPClient constructs an HTTP JSON-RPC client targeting endpoint. If client is nil, http.DefaultClient is used.

func (*HTTPClient) Call

func (c *HTTPClient) Call(ctx context.Context, requests ...*Request) ([]*Response, error)

Call sends the provided requests to the JSON-RPC endpoint. The payload is always encoded as a JSON array, even when only a single request is supplied. Responses are aligned with the order of the supplied requests, and notifications (requests without an id) yield nil entries in the returned slice.

type Handler

type Handler interface {
	Handle(ctx context.Context, req *Request) (*Response, error)
}

Handler serves JSON-RPC requests.

type HandlerFunc

type HandlerFunc func(ctx context.Context, req *Request) (*Response, error)

HandlerFunc adapts a function to the Handler interface.

func (HandlerFunc) Handle

func (f HandlerFunc) Handle(ctx context.Context, req *Request) (*Response, error)

Handle dispatches the request to f.

type RawClient added in v0.0.2

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

RawClient implements a JSON-RPC 2.0 client over an io.ReadWriteCloser transport.

func NewRawClient added in v0.0.2

func NewRawClient(rwc io.ReadWriteCloser) *RawClient

NewRawClient constructs a Client that communicates over rwc.

func (*RawClient) Call added in v0.0.2

func (c *RawClient) Call(ctx context.Context, requests ...*Request) ([]*Response, error)

Call sends the provided requests and returns their corresponding responses. The payload is encoded as a JSON array even when only a single request is supplied. Responses are aligned with the order of the supplied requests, and notifications (requests without an id) yield nil entries in the returned slice.

func (*RawClient) Close added in v0.0.2

func (c *RawClient) Close() error

Close terminates the underlying transport and releases resources.

func (*RawClient) CloseError added in v0.0.2

func (c *RawClient) CloseError() error

CloseError reports the error that caused the client to close, if any.

type RawServer added in v0.0.2

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

Server processes JSON-RPC requests over an io.ReadWriteCloser transport.

func NewRawServer added in v0.0.2

func NewRawServer(rwc io.ReadWriteCloser, handler Handler) *RawServer

NewRawServer constructs a Server that uses handler to process incoming requests.

func (*RawServer) Close added in v0.0.2

func (s *RawServer) Close() error

Close shuts down the server and closes the underlying transport.

func (*RawServer) CloseError added in v0.0.2

func (s *RawServer) CloseError() error

CloseError reports the error that closed the server, if any.

func (*RawServer) Serve added in v0.0.2

func (s *RawServer) Serve(ctx context.Context) error

Serve reads requests from the transport until the context is canceled or the connection terminates. It launches a goroutine per request so handler calls can run concurrently. The returned error is nil when the peer closes the connection cleanly.

type Request

type Request struct {
	JSONRPC string          `json:"jsonrpc"`
	Method  string          `json:"method"`
	Params  json.RawMessage `json:"params,omitempty"`
	ID      any             `json:"id,omitempty"`
}

Request represents a JSON-RPC 2.0 request message.

func WithRequest added in v0.0.2

func WithRequest(method string, params any, isNotify bool) *Request

type Response

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

Response represents a JSON-RPC 2.0 response message.

Jump to

Keyboard shortcuts

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