jsonrpc2

package
v0.0.18 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnknown        = NewError(-32001, "JSON RPC unknown error")
	ErrParse          = NewError(-32700, "JSON RPC parse error")
	ErrInvalidRequest = NewError(-32600, "JSON RPC invalid request")
	ErrMethodNotFound = NewError(-32601, "JSON RPC method not found")
	ErrInvalidParams  = NewError(-32602, "JSON RPC invalid params")
	ErrInternal       = NewError(-32603, "JSON RPC internal error")
)

https://www.jsonrpc.org/specification#error_object

Functions

This section is empty.

Types

type ChanObjStream

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

This is mostly intented as a testing utility

func NewChanObjStream

func NewChanObjStream(in <-chan Message, out chan<- Message) *ChanObjStream

func (*ChanObjStream) Close

func (c *ChanObjStream) Close() error

func (*ChanObjStream) ReadMessage

func (c *ChanObjStream) ReadMessage() (Message, error)

func (*ChanObjStream) WriteMessage

func (c *ChanObjStream) WriteMessage(obj Message) error

type Conn

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

func NewConn

func NewConn(objStream MessageStream, handlers ...Handler) *Conn

Starts listening asynchronously to the MessageStream and returns the connection.

By default, the server will try write concurrently to the ObjectStream

func (*Conn) Close

func (s *Conn) Close()

func (*Conn) SendNotification

func (s *Conn) SendNotification(method string, params any) error

Send a json rpc request and wait for the message to be sent. Thread safe

Will panick whenever the params object fails json.Marshal-ing

func (*Conn) SendRequest

func (s *Conn) SendRequest(method string, params any) (json.RawMessage, *ResponseError)

Send a json rpc request and wait for the response. Thread safe.

Will panick whenever the params object fails json.Marshal-ing

func (*Conn) Wait

func (c *Conn) Wait() error

blocks until connections is closed, returning its error (or nil)

type Handler

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

func NewNotificationHandler

func NewNotificationHandler[Params any](method string, strategy HandlingStrategy, handler func(params Params, conn *Conn)) Handler

Create a notification handler for the given method

The handler will be called asynchronously

func NewRequestHandler

func NewRequestHandler[Params any](method string, strategy HandlingStrategy, handler func(params Params, conn *Conn) any) Handler

Create a request handler for the given method

The handler will be called asynchronously. Will panic if not able to marshal the handler's return value

type HandlingStrategy

type HandlingStrategy uint8
const (
	SyncHandling HandlingStrategy = iota
	AsyncHandling
)

type ID

type ID interface {
	// contains filtered or unexported methods
}

func NewIntId

func NewIntId(value int64) ID

func NewStringId

func NewStringId(value string) ID

type Message

type Message interface {
	// contains filtered or unexported methods
}

func UnmarshalMessage

func UnmarshalMessage(data []byte) (Message, error)

type MessageStream

type MessageStream interface {
	io.Closer
	WriteMessage(obj Message) error
	ReadMessage() (Message, error)
}

type Request

type Request struct {
	// ID of this request, used to tie the Response back to the request.
	// This will be nil for notifications.
	ID ID
	// Method is a string containing the method name to invoke.
	Method string
	// Params is either a struct or an array with the parameters of the method.
	Params json.RawMessage
}

func (Request) IsNotification

func (r Request) IsNotification() bool

func (Request) MarshalJSON

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

type Response

type Response struct {
	// result is the content of the response.
	Result json.RawMessage
	// err is set only if the call failed.
	Error *ResponseError
	// id of the request this is a response to.
	ID ID
}

https://www.jsonrpc.org/specification#response_object

func (Response) MarshalJSON

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

type ResponseError

type ResponseError struct {
	Code    int64           `json:"code"`
	Message string          `json:"message"`
	Data    json.RawMessage `json:"data,omitempty"`
}

https://www.jsonrpc.org/specification#error_object

func NewError

func NewError(code int64, message string) ResponseError

Jump to

Keyboard shortcuts

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