jsonrpc

package
v0.62.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2022 License: MIT Imports: 6 Imported by: 1

Documentation

Index

Constants

View Source
const JSONRPC2 string = "2.0"

Variables

View Source
var (
	ErrOnlySupportJSONRPC2 = errors.New("the API only supports JSON-RPC 2.0")
	ErrMethodIsRequired    = errors.New("the method is required")
)

Functions

This section is empty.

Types

type API

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

func New

func New(log *zap.Logger) *API

func (*API) DispatchRequest

func (a *API) DispatchRequest(ctx context.Context, request *Request) *Response

func (*API) RegisterMethod

func (a *API) RegisterMethod(method string, handler Command)

func (*API) RegisteredMethods

func (a *API) RegisteredMethods() []string

type Command

type Command interface {
	Handle(ctx context.Context, params Params) (Result, *ErrorDetails)
}

type ErrorCode

type ErrorCode int16
const (
	// ErrorCodeParseError Invalid JSON was received by the server. An error
	// occurred on the server while parsing the JSON text.
	ErrorCodeParseError ErrorCode = -32700
	// ErrorCodeInvalidRequest The JSON sent is not a valid Request object.
	ErrorCodeInvalidRequest ErrorCode = -32600
	// ErrorCodeMethodNotFound The method does not exist / is not available.
	ErrorCodeMethodNotFound ErrorCode = -32601
	// ErrorCodeInvalidParams Invalid method parameter(s).
	ErrorCodeInvalidParams ErrorCode = -32602
	// ErrorCodeInternalError Internal JSON-RPC error.
	ErrorCodeInternalError ErrorCode = -32603

	// ErrorCodeRequestHasBeenInterrupted refers to a request that has been
	// interrupted by the server or the third-party application. It could
	// originate from a timeout or an explicit cancellation.
	ErrorCodeRequestHasBeenInterrupted ErrorCode = -32001
)

type ErrorDetails

type ErrorDetails struct {
	// Code indicates the error type that occurred.
	Code ErrorCode `json:"code"`

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

	// Data is a primitive or a structured value that contains additional
	// information about the error. This may be omitted.
	// The value of this member is defined by the Server (e.g. detailed error
	// information, nested errors etc.).
	Data string `json:"data,omitempty"`
}

ErrorDetails is returned when an RPC call encounters an error.

func NewCustomError

func NewCustomError(code ErrorCode, message string, data error) *ErrorDetails

func NewInternalError

func NewInternalError(data error) *ErrorDetails

func NewInvalidParams

func NewInvalidParams(data error) *ErrorDetails

func NewInvalidRequest

func NewInvalidRequest(data error) *ErrorDetails

func NewMethodNotFound

func NewMethodNotFound(data error) *ErrorDetails

func NewParseError

func NewParseError(data error) *ErrorDetails

func NewServerError

func NewServerError(code ErrorCode, data error) *ErrorDetails

func (ErrorDetails) IsInternalError

func (d ErrorDetails) IsInternalError() bool

type Params

type Params interface{}

Params is just a nicer way to describe what's passed to the handlers.

type Request

type Request struct {
	// Version specifies the version of the JSON-RPC protocol.
	// MUST be exactly "2.0".
	Version string `json:"jsonrpc"`

	// Method contains the name of the method to be invoked.
	Method string `json:"method"`

	// Params is a by-name Structured value that holds the parameter values to be
	// used during the invocation of the method. This member MAY be omitted.
	Params Params `json:"params,omitempty"`

	// ID is an identifier established by the Client that MUST contain a String.
	// If it is not included it is assumed to be a notification.
	// The Server MUST reply with the same value in the Response object if included.
	// This member is used to correlate the context between the two objects.
	ID string `json:"id,omitempty"`
}

func (*Request) Check

func (r *Request) Check() error

func (*Request) IsNotification

func (r *Request) IsNotification() bool

type Response

type Response struct {
	// Version specifies the version of the JSON-RPC protocol.
	// MUST be exactly "2.0".
	Version string `json:"jsonrpc"`

	// Result is REQUIRED on success. This member MUST NOT exist if there was an
	// error invoking the method.
	Result Result `json:"result,omitempty"`

	// Error is REQUIRED on error. This member MUST NOT exist if there was no
	// error triggered during invocation.
	Error *ErrorDetails `json:"error,omitempty"`

	// ID is an identifier established by the Client that MUST contain a String.
	// This member is REQUIRED. It MUST be the same as the value of the id member
	// in the Request Object.
	// If there was an error in detecting the id in the Request object (e.g.
	// Parse error/Invalid Request), it MUST be empty.
	ID string `json:"id,omitempty"`
}

func NewErrorResponse

func NewErrorResponse(id string, details *ErrorDetails) *Response

func NewSuccessfulResponse

func NewSuccessfulResponse(id string, result Result) *Response

type Result

type Result interface{}

Result is just a nicer way to describe what's expected to be returned by the handlers.

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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