jsonrpc

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2023 License: Apache-2.0 Imports: 18 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrClientQuit = errors.New("client is closed")
	ErrClientStop = errors.New("client is stopped")
)
View Source
var ErrNotFound = errors.New("no matching route was found")

ErrNotFound is returned when no route match is found.

Functions

func HandleProxyRoundTripError

func HandleProxyRoundTripError(rw http.ResponseWriter, req *http.Request, err error)

HandleProxyRoundTripError allows to transform a ProxiedRoundTrip Error

func InvalidMethod

func InvalidMethod(rw ResponseWriter, msg *RequestMsg)

InvalidMethod replies to the request with an invalid method error

func MethodNotFound

func MethodNotFound(rw ResponseWriter, msg *RequestMsg)

MethodNotFound replies to the request with a method not found error

func NotImplementedMethod

func NotImplementedMethod(rw ResponseWriter, msg *RequestMsg)

NotImplementedMethod replies to the request with a not implemented error

func NotSupportedVersion

func NotSupportedVersion(rw ResponseWriter, msg *RequestMsg)

func ProvideCaller

func ProvideCaller(callers ...interface{}) error

ProvideCaller takes a list of user defines callers as argument and automatically populates all caller's fields with RPC functions

It aims at facilitate the implementation of Web3 client connecting to downstream node

- Caller MUST be pointers to struct - All caller's fields MUST be functions mathc - Caller field func MUST accept a single input of type jsonrpc.Client and MUST return a single output which is a function - Caller field output func MUST return at most 2 outputs (if 2 the second MUST be an error)

Example of valid caller struct:

type ExampleCaller struct {
	   CtxInput_NoOutput        func(Client) func(context.Context)
	   NoInput_NoOutput         func(Client) func()
	   NonCtxInput_NoOutput     func(Client) func(int)
	   MultiInput_NoOutput      func(Client) func(context.Context, int, string)
	   NoInput_ErrorOutput      func(Client) func() error
	   NoInput_IntOutput        func(Client) func() int
	   NoInput_IntErrorOutput   func(Client) func() (int, error)
	   StructInput_StructOutput func(Client) func(context.Context, *TestParam) (*TestResult, error)
	   AllTags                  func(Client) func()                                                 `method:"exampleMethod" namespace:"eth"`
	   MethodTag                func(Client) func()                                                 `method:"exampleMethod"`
	   NamespaceTag             func(Client) func()                                                 `namespace:"eth"`
	   ObjectTag                func(Client) func(*TestParam)                                       `object:"-"`
}

func RWWithID

func RWWithID(id interface{}) func(ResponseWriter) ResponseWriter

func RWWithVersion

func RWWithVersion(v string) func(ResponseWriter) ResponseWriter

func WithIncrementalID

func WithIncrementalID(id interface{}) func(Client) Client

WithIncrementalID wraps a HTTPClient with an ID counter an increases it each time a new request comes out

func WithVersion

func WithVersion(version string) func(Client) Client

WithVersion wraps a HTTPClient to set version each time a new request comes out

func WriteError

func WriteError(rw ResponseWriter, err error) error

func WriteResult

func WriteResult(rw ResponseWriter, result interface{}) error

Types

type Client

type Client interface {
	// Do sends an jsonrpc request and returns an jsonrpc response
	Do(*RequestMsg) (*ResponseMsg, error)
}

Client is an jsonrpc HTTPClient interface

func ValidateID

func ValidateID(client Client) Client

type ErrorMsg

type ErrorMsg struct {
	Code    int
	Message string
	Data    interface{}
	// contains filtered or unexported fields
}

ErrorMsg is a struct allowing to manipulate a JSON-RPC response error

func DownstreamError

func DownstreamError(err error) *ErrorMsg

func Error

func Error(err error) *ErrorMsg

func InternalError

func InternalError(err error) *ErrorMsg

func InvalidDownstreamHTTPStatusError

func InvalidDownstreamHTTPStatusError(code int) *ErrorMsg

func InvalidDownstreamResponse

func InvalidDownstreamResponse(err error) *ErrorMsg

func InvalidMethodError

func InvalidMethodError(method string) *ErrorMsg

func InvalidParamsError

func InvalidParamsError(err error) *ErrorMsg

func InvalidRequest

func InvalidRequest(err error) *ErrorMsg

func MethodNotFoundError

func MethodNotFoundError() *ErrorMsg

func NotImplementedMethodError

func NotImplementedMethodError(method string) *ErrorMsg

func NotSupportedVersionError

func NotSupportedVersionError(version string) *ErrorMsg

func ParseError

func ParseError(err error) *ErrorMsg

func (*ErrorMsg) Error

func (msg *ErrorMsg) Error() string

Error function to match the error interface

func (*ErrorMsg) MarshalJSON

func (msg *ErrorMsg) MarshalJSON() ([]byte, error)

MarshalJSON

func (*ErrorMsg) UnmarshalData

func (msg *ErrorMsg) UnmarshalData(v interface{}) error

UnmarshalData into v

func (*ErrorMsg) UnmarshalJSON

func (msg *ErrorMsg) UnmarshalJSON(b []byte) error

func (*ErrorMsg) WithData

func (msg *ErrorMsg) WithData(data interface{}) *ErrorMsg

WithData attaches data

type HTTPClient

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

HTTPClient is a connector to a jsonrpc server

func NewHTTPClient

func NewHTTPClient(c httpclient.Client) *HTTPClient

NewHTTPClient creates a new jsonrpc HTTPClient from an HTTP HTTPClient

func (*HTTPClient) Do

func (c *HTTPClient) Do(reqMsg *RequestMsg) (*ResponseMsg, error)

Do sends a jsonrpc request over the underlying HTTP client and returns a jsonrpc response

type Handler

type Handler interface {
	ServeRPC(ResponseWriter, *RequestMsg)
}

Handler is and JSON-RPC handler to be used in a JSON-RPC server It provides the JSON-RPC abstraction over http.Handler interface

func DefaultRWHandler

func DefaultRWHandler(h Handler) Handler

DefaultRWHandler is a utility middleware that attaches request ID and Version to ResponseWriter so when developer has not to bother with response ID and Version when writing response

func InvalidMethodHandler

func InvalidMethodHandler() Handler

InvalidMethodHandler returns a simple handler that replies to each request with an invalid method error

func InvalidParamsHandler

func InvalidParamsHandler(err error) Handler

InvalidParamsHandler returns a simple handler that replies to each request with an invalid parameters error

func LoggedHandler

func LoggedHandler(h Handler, logger log.Logger) Handler

func MakeHandler

func MakeHandler(f interface{}) (Handler, error)

MakeHandler takes a function and transform it into a jsonrpc.Handler

func MethodNotFoundHandler

func MethodNotFoundHandler() Handler

MethodNotFoundHandler returns a simple handler that replies to each request with an invalid method error

func NotImplementedMethodHandler

func NotImplementedMethodHandler() Handler

NotImplementedMethodHandler returns a simple handler that replies to each request with an invalid method error

func NotSupportedVersionHandler

func NotSupportedVersionHandler() Handler

NotSupportedVersionHandler returns a simple handler that replies to each request with a not supported version request error

type HandlerFunc

type HandlerFunc func(ResponseWriter, *RequestMsg)

func (HandlerFunc) ServeRPC

func (f HandlerFunc) ServeRPC(rw ResponseWriter, msg *RequestMsg)

type RequestMsg

type RequestMsg struct {
	Version string
	Method  string
	ID      interface{}
	Params  interface{}
	// contains filtered or unexported fields
}

RequestMsg allows to manipulate a JSON-RPC v2 request

func (*RequestMsg) Context

func (msg *RequestMsg) Context() context.Context

func (*RequestMsg) Copy

func (msg *RequestMsg) Copy() *RequestMsg

func (*RequestMsg) MarshalJSON

func (msg *RequestMsg) MarshalJSON() ([]byte, error)

MarshalJSON

func (*RequestMsg) UnmarshalID

func (msg *RequestMsg) UnmarshalID(v interface{}) error

UnmarshalID into v

func (*RequestMsg) UnmarshalJSON

func (msg *RequestMsg) UnmarshalJSON(b []byte) error

UnmarshalJSON

func (*RequestMsg) UnmarshalParams

func (msg *RequestMsg) UnmarshalParams(v interface{}) error

UnmarshalParams into v

func (*RequestMsg) Validate

func (msg *RequestMsg) Validate() error

Validate JSON-Requests body

func (*RequestMsg) WithContext

func (msg *RequestMsg) WithContext(ctx context.Context) *RequestMsg

func (*RequestMsg) WithID

func (msg *RequestMsg) WithID(id interface{}) *RequestMsg

WithID attaches ID

func (*RequestMsg) WithMethod

func (msg *RequestMsg) WithMethod(method string) *RequestMsg

WithMethod attaches method

func (*RequestMsg) WithParams

func (msg *RequestMsg) WithParams(params interface{}) *RequestMsg

WithParams attaches parameters

func (*RequestMsg) WithVersion

func (msg *RequestMsg) WithVersion(v string) *RequestMsg

WithVersion attaches version

type ResponseMsg

type ResponseMsg struct {
	Version string
	ID      interface{}
	Result  interface{}
	Error   *ErrorMsg
	// contains filtered or unexported fields
}

ResponseMsg allows to manipulate a JSON-RPC v2 response

func (*ResponseMsg) Err

func (msg *ResponseMsg) Err() error

func (*ResponseMsg) MarshalJSON

func (msg *ResponseMsg) MarshalJSON() ([]byte, error)

MarshalJSON

func (*ResponseMsg) UnmarshalID

func (msg *ResponseMsg) UnmarshalID(v interface{}) error

UnmarshalID into v

func (*ResponseMsg) UnmarshalJSON

func (msg *ResponseMsg) UnmarshalJSON(b []byte) error

func (*ResponseMsg) UnmarshalResult

func (msg *ResponseMsg) UnmarshalResult(v interface{}) error

UnmarshalResult into v

func (*ResponseMsg) Validate

func (msg *ResponseMsg) Validate() error

Validate JSON-RPC response is valid

func (*ResponseMsg) WithError

func (msg *ResponseMsg) WithError(err error) *ResponseMsg

WithError attaches error

func (*ResponseMsg) WithID

func (msg *ResponseMsg) WithID(id interface{}) *ResponseMsg

WithID attaches ID

func (*ResponseMsg) WithResult

func (msg *ResponseMsg) WithResult(result interface{}) *ResponseMsg

WithResult attaches result

func (*ResponseMsg) WithVersion

func (msg *ResponseMsg) WithVersion(v string) *ResponseMsg

WithVersion attaches version

type ResponseWriter

type ResponseWriter interface {
	WriteMsg(*ResponseMsg) error
}

func NewResponseWriter

func NewResponseWriter(w io.Writer) ResponseWriter

type Route

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

Route holds information about a json-rpc route

func (*Route) Handle

func (r *Route) Handle(h Handler) *Route

func (*Route) HandleFunc

func (r *Route) HandleFunc(f func(ResponseWriter, *RequestMsg)) *Route

func (*Route) Match

func (r *Route) Match(msg *RequestMsg, match *RouteMatch) bool

func (*Route) Method

func (r *Route) Method(method string) *Route

func (*Route) MethodPrefix

func (r *Route) MethodPrefix(prefix string) *Route

func (*Route) Subrouter

func (r *Route) Subrouter() *Router

func (*Route) Version

func (r *Route) Version(version string) *Route

type RouteMatch

type RouteMatch struct {
	Route   *Route
	Handler Handler
	Err     error
}

RouteMatch stores information about a matched route

type Router

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

func NewRouter

func NewRouter() *Router

NewRouter returns a new router instance.

func (*Router) DefaultHandler

func (r *Router) DefaultHandler(handler Handler) *Router

Version registers a new route with a matcher for given JSON-RPC version

func (*Router) Handle

func (r *Router) Handle(method string, handler Handler) *Route

Handle registers a new route for a given method

func (*Router) HandleFunc

func (r *Router) HandleFunc(method string, f func(ResponseWriter, *RequestMsg)) *Route

HandleFunc registers a new route for a given method

func (*Router) Match

func (r *Router) Match(msg *RequestMsg, match *RouteMatch) bool

func (*Router) Method

func (r *Router) Method(method string) *Route

Method registers a new route with a matcher for given method

func (*Router) MethodPrefix

func (r *Router) MethodPrefix(prefix string) *Route

MethodPrefix registers a new route with a matcher for given method

func (*Router) NewRoute

func (r *Router) NewRoute() *Route

NewRoute registers an empty route.

func (*Router) ServeRPC

func (r *Router) ServeRPC(rw ResponseWriter, msg *RequestMsg)

ServeRPC dispatches the handler registered in the matched route.

func (*Router) Version

func (r *Router) Version(version string) *Route

Version registers a new route with a matcher for given JSON-RPC version

type WebSocketClient

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

WebSocketClient is a connector to a jsonrpc server

func NewWebsocketClient

func NewWebsocketClient(conn *websocket.Conn) *WebSocketClient

NewWebsocketClient creates a new jsonrpc HTTPClient from an HTTP HTTPClient

func (*WebSocketClient) Do

func (c *WebSocketClient) Do(reqMsg *RequestMsg) (*ResponseMsg, error)

Do sends a jsonrpc request over the underlying HTTP client and returns a jsonrpc response

func (*WebSocketClient) Errors

func (c *WebSocketClient) Errors() <-chan error

func (*WebSocketClient) Start

func (*WebSocketClient) Stop

func (c *WebSocketClient) Stop(ctx context.Context) error

Stop the client from receiving new messages It finishes reading all messages before quiting

Directories

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

Jump to

Keyboard shortcuts

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