jsonrpc2

package
v0.0.0-...-e4b607e Latest Latest
Warning

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

Go to latest
Published: May 27, 2020 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTTPHandler

func HTTPHandler(srv *rpc.Server) http.Handler

HTTPHandler returns handler for HTTP requests which will execute incoming JSON-RPC 2.0 over HTTP using srv.

If srv is nil then rpc.DefaultServer will be used.

Specification: http://www.simple-is-better.org/json-rpc/transport_http.html

func HTTPRequestFromContext

func HTTPRequestFromContext(ctx context.Context) *http.Request

HTTPRequestFromContext returns HTTP request related to this RPC (if you use HTTPHander to serve JSON RPC 2.0 over HTTP) or nil otherwise.

func NewClientCodec

func NewClientCodec(conn io.ReadWriteCloser) rpc.ClientCodec

NewClientCodec returns a new rpc.ClientCodec using JSON-RPC 2.0 on conn.

Types

type Client

type Client struct {
	*rpc.Client
	// contains filtered or unexported fields
}

Client represents a JSON RPC 2.0 Client. There may be multiple outstanding Calls associated with a single Client, and a Client may be used by multiple goroutines simultaneously.

It also provides all methods of net/rpc Client.

func Dial

func Dial(network, address string) (*Client, error)

Dial connects to a JSON-RPC 2.0 server at the specified network address.

func NewClient

func NewClient(conn io.ReadWriteCloser) *Client

NewClient returns a new Client to handle requests to the set of services at the other end of the connection.

func NewClientWithCodec

func NewClientWithCodec(codec rpc.ClientCodec) *Client

NewClientWithCodec returns a new Client using the given rpc.ClientCodec.

func NewCustomHTTPClient

func NewCustomHTTPClient(url string, doer Doer) *Client

NewCustomHTTPClient returns a new Client to handle requests to the set of services at the given url using provided doer (&http.Client{} by default).

Use doer to customize HTTP authorization/headers/etc. sent with each request (it method Do() will receive already configured POST request with url, all required headers and body set according to specification).

func NewHTTPClient

func NewHTTPClient(url string) *Client

NewHTTPClient returns a new Client to handle requests to the set of services at the given url.

func (Client) Notify

func (c Client) Notify(serviceMethod string, args interface{}) error

Notify try to invoke the named function. It return error only in case it wasn't able to send request.

type Ctx

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

Ctx can be embedded into your struct with RPC method parameters (if that method parameters type is a struct) to make it implement WithContext interface and thus have access to request context.

func (*Ctx) Context

func (c *Ctx) Context() context.Context

Context returns ctx given to preceding SetContext call.

func (*Ctx) SetContext

func (c *Ctx) SetContext(ctx context.Context)

SetContext saves ctx for succeeding Context calls.

type Doer

type Doer interface {
	Do(req *http.Request) (resp *http.Response, err error)
}

Doer is an interface for doing HTTP requests.

type DoerFunc

type DoerFunc func(req *http.Request) (resp *http.Response, err error)

The DoerFunc type is an adapter to allow the use of ordinary functions as HTTP clients. If f is a function with the appropriate signature, DoerFunc(f) is a client that calls f.

func (DoerFunc) Do

func (f DoerFunc) Do(req *http.Request) (resp *http.Response, err error)

DoerFunc calls f(req).

type Error

type Error struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
}

Error represent JSON-RPC 2.0 "Error object".

func NewError

func NewError(code int, message string) *Error

NewError returns an Error with given code and message.

func ServerError

func ServerError(rpcerr error) *Error

ServerError convert errors returned by Client.Call() into Error. User should check for rpc.ErrShutdown and io.ErrUnexpectedEOF before calling ServerError.

func (*Error) Error

func (e *Error) Error() string

Error returns JSON representation of Error.

type WithContext

type WithContext interface {
	Context() context.Context
	SetContext(ctx context.Context)
}

WithContext is an interface which should be implemented by RPC method parameters type if you need access to request context in RPC method.

Request context will be same as was provided to corresponding ServeConnContext/NewServerCodecContext or context.Background otherwise.

Jump to

Keyboard shortcuts

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