Documentation ¶
Index ¶
- Variables
- func HandleProxyRoundTripError(rw http.ResponseWriter, req *http.Request, err error)
- func InvalidMethod(rw ResponseWriter, msg *RequestMsg)
- func MethodNotFound(rw ResponseWriter, msg *RequestMsg)
- func NotImplementedMethod(rw ResponseWriter, msg *RequestMsg)
- func NotSupportedVersion(rw ResponseWriter, msg *RequestMsg)
- func ProvideCaller(callers ...interface{}) error
- func RWWithID(id interface{}) func(ResponseWriter) ResponseWriter
- func RWWithVersion(v string) func(ResponseWriter) ResponseWriter
- func WithIncrementalID(id interface{}) func(Client) Client
- func WithVersion(version string) func(Client) Client
- func WriteError(rw ResponseWriter, err error) error
- func WriteResult(rw ResponseWriter, result interface{}) error
- type Client
- type ErrorMsg
- func DownstreamError(err error) *ErrorMsg
- func Error(err error) *ErrorMsg
- func InternalError(err error) *ErrorMsg
- func InvalidDownstreamHTTPStatusError(code int) *ErrorMsg
- func InvalidDownstreamResponse(err error) *ErrorMsg
- func InvalidMethodError(method string) *ErrorMsg
- func InvalidParamsError(err error) *ErrorMsg
- func InvalidRequest(err error) *ErrorMsg
- func MethodNotFoundError() *ErrorMsg
- func NotImplementedMethodError(method string) *ErrorMsg
- func NotSupportedVersionError(version string) *ErrorMsg
- func ParseError(err error) *ErrorMsg
- type HTTPClient
- type Handler
- func DefaultRWHandler(h Handler) Handler
- func InvalidMethodHandler() Handler
- func InvalidParamsHandler(err error) Handler
- func LoggedHandler(h Handler, logger log.Logger) Handler
- func MakeHandler(f interface{}) (Handler, error)
- func MethodNotFoundHandler() Handler
- func NotImplementedMethodHandler() Handler
- func NotSupportedVersionHandler() Handler
- type HandlerFunc
- type RequestMsg
- func (msg *RequestMsg) Context() context.Context
- func (msg *RequestMsg) Copy() *RequestMsg
- func (msg *RequestMsg) MarshalJSON() ([]byte, error)
- func (msg *RequestMsg) UnmarshalID(v interface{}) error
- func (msg *RequestMsg) UnmarshalJSON(b []byte) error
- func (msg *RequestMsg) UnmarshalParams(v interface{}) error
- func (msg *RequestMsg) Validate() error
- func (msg *RequestMsg) WithContext(ctx context.Context) *RequestMsg
- func (msg *RequestMsg) WithID(id interface{}) *RequestMsg
- func (msg *RequestMsg) WithMethod(method string) *RequestMsg
- func (msg *RequestMsg) WithParams(params interface{}) *RequestMsg
- func (msg *RequestMsg) WithVersion(v string) *RequestMsg
- type ResponseMsg
- func (msg *ResponseMsg) Err() error
- func (msg *ResponseMsg) MarshalJSON() ([]byte, error)
- func (msg *ResponseMsg) UnmarshalID(v interface{}) error
- func (msg *ResponseMsg) UnmarshalJSON(b []byte) error
- func (msg *ResponseMsg) UnmarshalResult(v interface{}) error
- func (msg *ResponseMsg) Validate() error
- func (msg *ResponseMsg) WithError(err error) *ResponseMsg
- func (msg *ResponseMsg) WithID(id interface{}) *ResponseMsg
- func (msg *ResponseMsg) WithResult(result interface{}) *ResponseMsg
- func (msg *ResponseMsg) WithVersion(v string) *ResponseMsg
- type ResponseWriter
- type Route
- func (r *Route) Handle(h Handler) *Route
- func (r *Route) HandleFunc(f func(ResponseWriter, *RequestMsg)) *Route
- func (r *Route) Match(msg *RequestMsg, match *RouteMatch) bool
- func (r *Route) Method(method string) *Route
- func (r *Route) MethodPrefix(prefix string) *Route
- func (r *Route) Subrouter() *Router
- func (r *Route) Version(version string) *Route
- type RouteMatch
- type Router
- func (r *Router) DefaultHandler(handler Handler) *Router
- func (r *Router) Handle(method string, handler Handler) *Route
- func (r *Router) HandleFunc(method string, f func(ResponseWriter, *RequestMsg)) *Route
- func (r *Router) Match(msg *RequestMsg, match *RouteMatch) bool
- func (r *Router) Method(method string) *Route
- func (r *Router) MethodPrefix(prefix string) *Route
- func (r *Router) NewRoute() *Route
- func (r *Router) ServeRPC(rw ResponseWriter, msg *RequestMsg)
- func (r *Router) Version(version string) *Route
- type WebSocketClient
Constants ¶
This section is empty.
Variables ¶
var ( ErrClientQuit = errors.New("client is closed") ErrClientStop = errors.New("client is stopped") )
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 ¶
WithIncrementalID wraps a HTTPClient with an ID counter an increases it each time a new request comes out
func WithVersion ¶
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 ¶
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 InternalError ¶
func InvalidMethodError ¶
func InvalidParamsError ¶
func InvalidRequest ¶
func MethodNotFoundError ¶
func MethodNotFoundError() *ErrorMsg
func ParseError ¶
func (*ErrorMsg) UnmarshalData ¶
UnmarshalData into v
func (*ErrorMsg) UnmarshalJSON ¶
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 ¶
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 ¶
InvalidParamsHandler returns a simple handler that replies to each request with an invalid parameters error
func MakeHandler ¶
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) 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) 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) 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) HandleFunc ¶
func (r *Route) HandleFunc(f func(ResponseWriter, *RequestMsg)) *Route
func (*Route) Match ¶
func (r *Route) Match(msg *RequestMsg, match *RouteMatch) bool
func (*Route) MethodPrefix ¶
type RouteMatch ¶
RouteMatch stores information about a matched route
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
func (*Router) DefaultHandler ¶
Version registers a new route with a matcher for given JSON-RPC version
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) MethodPrefix ¶
MethodPrefix registers a new route with a matcher for given method
func (*Router) ServeRPC ¶
func (r *Router) ServeRPC(rw ResponseWriter, msg *RequestMsg)
ServeRPC dispatches the handler registered in the matched route.
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