jsonrpc

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2023 License: MIT Imports: 13 Imported by: 1

README

JSON-RPC 1.0 partial implementation for wired connections

This library implements basic features JSON-RPC protocol. It is suitable for building client application communicating over raw network connection. Also, it has some support for RPC server implementation.

Example

// Create connection
conn := NewConnection("unix", "path_to_socket", zapShugaredLogger)

// Call method with arbitrary arguments (synchronous)
resultRawJson, err := conn.Call(ctx, "some_method", "arg1", struct{name string, val int}{name: "arg2", val: 3})

// Send request with arbitrary arguments (asynchronous)
respChan, id, err := conn.Send(ctx, "some_method", "arg1", struct{name string, val int}{name: "arg2", val: 3})
// Wait for response
resp := <-respChan

// Send notification with arbitrary arguments
err := conn.Notify(ctx, "some_method", "arg1", struct{name string, val int}{name: "arg2", val: 3})

// Handle notification from server (params are raw JSON of notification params)
conn.Handle("echo", func(params []byte) {
    var msg string
    if err := json.Unmarshal(params, &msg); err != nil {
        return
    }
    fmt.Println(msg)
})

// Handle call from server (params are raw JSON of call params)
conn.HandleCall("echo", func(req *Response, resp chan<- *Response) {
    var msg string
    if err := json.Unmarshal(params, &msg); err != nil {
        return nil, err
    }
    fmt.Println(msg)
    resp <- &Response{
        ID: req.ID,
        Res: json.RawMessage(`"echoed"`)
    }
})

API

NewConnection(network, addr string, _log *zap.SugaredLogger) *Connection

Creates new connection

Error() error

Error returns error if connection is in failed state (closed or failed due some other reason), nil otherwise

Close() error

Close closes connection

Notify(ctx context.Context, method string, params ...any) error

Send notification to server

Send(ctx context.Context, method string, params ...any) (<-chan *Response, string, error)

Send request to server. Returns channel to receive response and request ID

Call(ctx context.Context, method string, params ...any) (json.RawMessage, error)

Call method on server. Returns result part of response and error if any

DropPending(id string)

Drop pending request with given ID

Handle(method string, handler NotificationHandler) error

Register handler for notification, on receiving notification with given method handler will be called with raw JSON of notification params as argument. NotificationHandler is defined as: type NotificationHandler func(params []byte)

HandleCall(method string, handler CallHandler) error

Register handler for call, on receiving call with given method handler will be called with raw JSON of call params as argument. CallHandler is defined as: type CallHandler func(req *Response, resp chan<- *Response)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CallHandler

type CallHandler func(response *Response, respChan chan<- *Response)

type Connection

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

A Connection is a JSON-RPC connection.

func NewConnection

func NewConnection(network, addr string, _log *zap.SugaredLogger) (*Connection, error)

NewConnection creates a new Connection

func (*Connection) Call

func (c *Connection) Call(ctx context.Context, method string, params ...any) (json.RawMessage, error)

Call sends a single JSON-RPC request synchronously.

func (*Connection) Close

func (c *Connection) Close() error

Close closes the connection.

func (*Connection) DropPending

func (c *Connection) DropPending(id string)

func (*Connection) Error

func (c *Connection) Error() error

func (*Connection) Handle

func (c *Connection) Handle(method string, handler NotificationHandler) error

Handle sets notification handler for incoming JSON-RPC notification.

func (*Connection) HandleCall

func (c *Connection) HandleCall(method string, handler CallHandler) error

HandleCall sets call handler for incoming JSON-RPC call.

func (*Connection) Notify

func (c *Connection) Notify(ctx context.Context, method string, params ...any) error

func (*Connection) Send

func (c *Connection) Send(ctx context.Context, method string, params ...any) (<-chan *Response, string, error)

Send sends a single JSON-RPC request asynchronously.

type NotificationHandler

type NotificationHandler func(params []byte)

type Response

type Response struct {
	ID *string `json:"id"` // ID Non-null for response; null for request notification.

	// used for requests
	Res json.RawMessage `json:"result,omitempty"` // Res here is raw results from JSON-RPC server.
	Err interface{}     `json:"error,omitempty"`

	// used for notifications
	Method string          `json:"method,omitempty"` // Method is method name notification.
	Params json.RawMessage `json:"params,omitempty"` // Params is notification object
}

Response can be JSON-RPC response, or JSON-RPC request notification

func (*Response) Error

func (r *Response) Error() error

Err returns response error if any

func (*Response) IsCall

func (r *Response) IsCall() bool

func (*Response) IsNotification

func (r *Response) IsNotification() bool

IsNotification returns true if response is a notification

Jump to

Keyboard shortcuts

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