...

Package jsonrpc

import "github.com/go-kit/kit/transport/http/jsonrpc"
Overview
Index

Overview ▾

Package jsonrpc provides a JSON RPC (v2.0) binding for endpoints. See http://www.jsonrpc.org/specification

Index ▾

Constants
func DefaultErrorEncoder(ctx context.Context, err error, w http.ResponseWriter)
func DefaultRequestEncoder(_ context.Context, req interface{}) (json.RawMessage, error)
func DefaultResponseDecoder(_ context.Context, res Response) (interface{}, error)
func ErrorMessage(code int) string
type Client
    func NewClient(tgt *url.URL, method string, options ...ClientOption) *Client
    func (c Client) Endpoint() endpoint.Endpoint
type ClientFinalizerFunc
type ClientOption
    func BufferedStream(buffered bool) ClientOption
    func ClientAfter(after ...httptransport.ClientResponseFunc) ClientOption
    func ClientBefore(before ...httptransport.RequestFunc) ClientOption
    func ClientFinalizer(f httptransport.ClientFinalizerFunc) ClientOption
    func ClientRequestEncoder(enc EncodeRequestFunc) ClientOption
    func ClientRequestIDGenerator(g RequestIDGenerator) ClientOption
    func ClientResponseDecoder(dec DecodeResponseFunc) ClientOption
    func SetClient(client httptransport.HTTPClient) ClientOption
type DecodeRequestFunc
type DecodeResponseFunc
type EncodeRequestFunc
type EncodeResponseFunc
type EndpointCodec
type EndpointCodecMap
type Error
    func (e Error) Error() string
    func (e Error) ErrorCode() int
type ErrorCoder
type Request
type RequestFunc
type RequestID
    func (id *RequestID) Float32() (float32, error)
    func (id *RequestID) Int() (int, error)
    func (id *RequestID) MarshalJSON() ([]byte, error)
    func (id *RequestID) String() (string, error)
    func (id *RequestID) UnmarshalJSON(b []byte) error
type RequestIDGenerator
    func NewAutoIncrementID(init uint64) RequestIDGenerator
type Response
type Server
    func NewServer(ecm EndpointCodecMap, options ...ServerOption) *Server
    func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
type ServerOption
    func ServerAfter(after ...httptransport.ServerResponseFunc) ServerOption
    func ServerBefore(before ...httptransport.RequestFunc) ServerOption
    func ServerBeforeCodec(beforeCodec ...RequestFunc) ServerOption
    func ServerErrorEncoder(ee httptransport.ErrorEncoder) ServerOption
    func ServerErrorLogger(logger log.Logger) ServerOption
    func ServerFinalizer(f httptransport.ServerFinalizerFunc) ServerOption

Package files

client.go doc.go encode_decode.go error.go request_response_types.go server.go

Constants

const (
    // ParseError defines invalid JSON was received by the server.
    // An error occurred on the server while parsing the JSON text.
    ParseError int = -32700

    // InvalidRequestError defines the JSON sent is not a valid Request object.
    InvalidRequestError int = -32600

    // MethodNotFoundError defines the method does not exist / is not available.
    MethodNotFoundError int = -32601

    // InvalidParamsError defines invalid method parameter(s).
    InvalidParamsError int = -32602

    // InternalError defines a server error
    InternalError int = -32603
)
const (
    // Version defines the version of the JSON RPC implementation
    Version string = "2.0"

    // ContentType defines the content type to be served.
    ContentType string = "application/json; charset=utf-8"
)
const (
    ContextKeyRequestMethod contextKey = iota
)

func DefaultErrorEncoder

func DefaultErrorEncoder(ctx context.Context, err error, w http.ResponseWriter)

DefaultErrorEncoder writes the error to the ResponseWriter, as a json-rpc error response, with an InternalError status code. The Error() string of the error will be used as the response error message. If the error implements ErrorCoder, the provided code will be set on the response error. If the error implements Headerer, the given headers will be set.

func DefaultRequestEncoder

func DefaultRequestEncoder(_ context.Context, req interface{}) (json.RawMessage, error)

DefaultRequestEncoder marshals the given request to JSON.

func DefaultResponseDecoder

func DefaultResponseDecoder(_ context.Context, res Response) (interface{}, error)

DefaultResponseDecoder unmarshals the result to interface{}, or returns an error, if found.

func ErrorMessage

func ErrorMessage(code int) string

ErrorMessage returns a message for the JSON RPC error code. It returns the empty string if the code is unknown.

type Client

Client wraps a JSON RPC method and provides a method that implements endpoint.Endpoint.

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

func NewClient

func NewClient(
    tgt *url.URL,
    method string,
    options ...ClientOption,
) *Client

NewClient constructs a usable Client for a single remote method.

func (Client) Endpoint

func (c Client) Endpoint() endpoint.Endpoint

Endpoint returns a usable endpoint that invokes the remote endpoint.

type ClientFinalizerFunc

ClientFinalizerFunc can be used to perform work at the end of a client HTTP request, after the response is returned. The principal intended use is for error logging. Additional response parameters are provided in the context under keys with the ContextKeyResponse prefix. Note: err may be nil. There maybe also no additional response parameters depending on when an error occurs.

type ClientFinalizerFunc func(ctx context.Context, err error)

type ClientOption

ClientOption sets an optional parameter for clients.

type ClientOption func(*Client)

func BufferedStream

func BufferedStream(buffered bool) ClientOption

BufferedStream sets whether the Response.Body is left open, allowing it to be read from later. Useful for transporting a file as a buffered stream.

func ClientAfter

func ClientAfter(after ...httptransport.ClientResponseFunc) ClientOption

ClientAfter sets the ClientResponseFuncs applied to the server's HTTP response prior to it being decoded. This is useful for obtaining anything from the response and adding onto the context prior to decoding.

func ClientBefore

func ClientBefore(before ...httptransport.RequestFunc) ClientOption

ClientBefore sets the RequestFuncs that are applied to the outgoing HTTP request before it's invoked.

func ClientFinalizer

func ClientFinalizer(f httptransport.ClientFinalizerFunc) ClientOption

ClientFinalizer is executed at the end of every HTTP request. By default, no finalizer is registered.

func ClientRequestEncoder

func ClientRequestEncoder(enc EncodeRequestFunc) ClientOption

ClientRequestEncoder sets the func used to encode the request params to JSON. If not set, DefaultRequestEncoder is used.

func ClientRequestIDGenerator

func ClientRequestIDGenerator(g RequestIDGenerator) ClientOption

ClientRequestIDGenerator is executed before each request to generate an ID for the request. By default, AutoIncrementRequestID is used.

func ClientResponseDecoder

func ClientResponseDecoder(dec DecodeResponseFunc) ClientOption

ClientResponseDecoder sets the func used to decode the response params from JSON. If not set, DefaultResponseDecoder is used.

func SetClient

func SetClient(client httptransport.HTTPClient) ClientOption

SetClient sets the underlying HTTP client used for requests. By default, http.DefaultClient is used.

type DecodeRequestFunc

DecodeRequestFunc extracts a user-domain request object from raw JSON It's designed to be used in JSON RPC servers, for server-side endpoints. One straightforward DecodeRequestFunc could be something that unmarshals JSON from the request body to the concrete request type.

type DecodeRequestFunc func(context.Context, json.RawMessage) (request interface{}, err error)

type DecodeResponseFunc

DecodeResponseFunc extracts a user-domain response object from an JSON RPC response object. It's designed to be used in JSON RPC clients, for client-side endpoints. It is the responsibility of this function to decide whether any error present in the JSON RPC response should be surfaced to the client endpoint.

type DecodeResponseFunc func(context.Context, Response) (response interface{}, err error)

type EncodeRequestFunc

EncodeRequestFunc encodes the given request object to raw JSON. It's designed to be used in JSON RPC clients, for client-side endpoints. One straightforward EncodeResponseFunc could be something that JSON encodes the object directly.

type EncodeRequestFunc func(context.Context, interface{}) (request json.RawMessage, err error)

type EncodeResponseFunc

EncodeResponseFunc encodes the passed response object to a JSON RPC result. It's designed to be used in HTTP servers, for server-side endpoints. One straightforward EncodeResponseFunc could be something that JSON encodes the object directly.

type EncodeResponseFunc func(context.Context, interface{}) (response json.RawMessage, err error)

type EndpointCodec

EndpointCodec defines a server Endpoint and its associated codecs

type EndpointCodec struct {
    Endpoint endpoint.Endpoint
    Decode   DecodeRequestFunc
    Encode   EncodeResponseFunc
}

type EndpointCodecMap

EndpointCodecMap maps the Request.Method to the proper EndpointCodec

type EndpointCodecMap map[string]EndpointCodec

type Error

Error defines a JSON RPC error that can be returned in a Response from the spec http://www.jsonrpc.org/specification#error_object

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

func (Error) Error

func (e Error) Error() string

Error implements error.

func (Error) ErrorCode

func (e Error) ErrorCode() int

ErrorCode returns the JSON RPC error code associated with the error.

type ErrorCoder

ErrorCoder is checked by DefaultErrorEncoder. If an error value implements ErrorCoder, the integer result of ErrorCode() will be used as the JSONRPC error code when encoding the error.

By default, InternalError (-32603) is used.

type ErrorCoder interface {
    ErrorCode() int
}

type Request

Request defines a JSON RPC request from the spec http://www.jsonrpc.org/specification#request_object

type Request struct {
    JSONRPC string          `json:"jsonrpc"`
    Method  string          `json:"method"`
    Params  json.RawMessage `json:"params"`
    ID      *RequestID      `json:"id"`
}

type RequestFunc

RequestFunc may take information from decoded json body and place in request context. In Servers, RequestFuncs are executed after json is parsed but prior to invoking the codec

type RequestFunc func(context.Context, *http.Request, Request) context.Context

type RequestID

RequestID defines a request ID that can be string, number, or null. An identifier established by the Client that MUST contain a String, Number, or NULL value if included. If it is not included it is assumed to be a notification. The value SHOULD normally not be Null and Numbers SHOULD NOT contain fractional parts.

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

func (*RequestID) Float32

func (id *RequestID) Float32() (float32, error)

Float32 returns the ID as a float value. An error is returned if the ID can't be treated as an float.

func (*RequestID) Int

func (id *RequestID) Int() (int, error)

Int returns the ID as an integer value. An error is returned if the ID can't be treated as an int.

func (*RequestID) MarshalJSON

func (id *RequestID) MarshalJSON() ([]byte, error)

func (*RequestID) String

func (id *RequestID) String() (string, error)

String returns the ID as a string value. An error is returned if the ID can't be treated as an string.

func (*RequestID) UnmarshalJSON

func (id *RequestID) UnmarshalJSON(b []byte) error

UnmarshalJSON satisfies json.Unmarshaler

type RequestIDGenerator

RequestIDGenerator returns an ID for the request.

type RequestIDGenerator interface {
    Generate() interface{}
}

func NewAutoIncrementID

func NewAutoIncrementID(init uint64) RequestIDGenerator

NewAutoIncrementID returns an auto-incrementing request ID generator, initialised with the given value.

type Response

Response defines a JSON RPC response from the spec http://www.jsonrpc.org/specification#response_object

type Response struct {
    JSONRPC string          `json:"jsonrpc"`
    Result  json.RawMessage `json:"result,omitempty"`
    Error   *Error          `json:"error,omitempty"`
    ID      *RequestID      `json:"id"`
}

type Server

Server wraps an endpoint and implements http.Handler.

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

func NewServer

func NewServer(
    ecm EndpointCodecMap,
    options ...ServerOption,
) *Server

NewServer constructs a new server, which implements http.Server.

func (Server) ServeHTTP

func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

type ServerOption

ServerOption sets an optional parameter for servers.

type ServerOption func(*Server)

func ServerAfter

func ServerAfter(after ...httptransport.ServerResponseFunc) ServerOption

ServerAfter functions are executed on the HTTP response writer after the endpoint is invoked, but before anything is written to the client.

func ServerBefore

func ServerBefore(before ...httptransport.RequestFunc) ServerOption

ServerBefore functions are executed on the HTTP request object before the request is decoded.

func ServerBeforeCodec

func ServerBeforeCodec(beforeCodec ...RequestFunc) ServerOption

ServerBeforeCodec functions are executed after the JSON request body has been decoded, but before the method's decoder is called. This provides an opportunity for middleware to inspect the contents of the rpc request before being passed to the codec.

func ServerErrorEncoder

func ServerErrorEncoder(ee httptransport.ErrorEncoder) ServerOption

ServerErrorEncoder is used to encode errors to the http.ResponseWriter whenever they're encountered in the processing of a request. Clients can use this to provide custom error formatting and response codes. By default, errors will be written with the DefaultErrorEncoder.

func ServerErrorLogger

func ServerErrorLogger(logger log.Logger) ServerOption

ServerErrorLogger is used to log non-terminal errors. By default, no errors are logged. This is intended as a diagnostic measure. Finer-grained control of error handling, including logging in more detail, should be performed in a custom ServerErrorEncoder or ServerFinalizer, both of which have access to the context.

func ServerFinalizer

func ServerFinalizer(f httptransport.ServerFinalizerFunc) ServerOption

ServerFinalizer is executed at the end of every HTTP request. By default, no finalizer is registered.