codec

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2019 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ErrServer Reserved for implementation-defined server-errors.
	ErrServer = -32000
	// ErrInvalidRequest The JSON sent is not a valid Request object.
	ErrInvalidRequest = -32600
	// ErrNoMethod The method does not exist / is not available.
	ErrNoMethod = -32601
	// ErrBadParams Invalid method parameter(s).
	ErrBadParams = -32602
	// ErrInternal Internal JSON-RPC error.
	ErrInternal = -32603
	// ErrParse Invalid JSON was received by the server.
	// An error occurred on the server while parsing the JSON text.
	ErrParse = -32700
)
View Source
const Version = "2.0"

Version of json-rpc protocol

Variables

View Source
var (
	// DefaultEncoder for request
	DefaultEncoder = &encoder{}
	// DefaultEncoderSelector for request
	DefaultEncoderSelector = &encoderSelector{}
)

Functions

func WriteError

func WriteError(w http.ResponseWriter, err error)

WriteError to ResponseWriter

Types

type CompressionSelector

type CompressionSelector struct{}

CompressionSelector generates the compressed http encoder.

func (*CompressionSelector) Select

Select method selects the correct compression encoder based on http HEADER.

type Encoder

type Encoder interface {
	Encode(w http.ResponseWriter) io.Writer
}

Encoder interface contains the encoder for http response. Eg. gzip, flate compressions.

type EncoderSelector

type EncoderSelector interface {
	Select(r *http.Request) Encoder
}

EncoderSelector interface provides a way to select encoder using the http request. Typically people can use this to check HEADER of the request and figure out client capabilities. Eg. "Accept-Encoding" tells about supported compressions.

type Error

type Error struct {
	// A Number that indicates the error type that occurred.
	Code int `json:"code"` /* required */

	// A String providing a short description of the error.
	// The message SHOULD be limited to a concise single sentence.
	Message string `json:"message"` /* required */

	// A Primitive or Structured value that contains additional information about the error.
	Data interface{} `json:"data,omitempty"` /* optional */

	Internal error `json:"-"` // ignore
}

Error is codec error representation

func (*Error) Error

func (e *Error) Error() string

type Interface

type Interface interface {
	NewRequest(http.ResponseWriter, *http.Request) (Request, error)
}

Interface codec creates a CodecRequest to process each request.

func NewCodec

func NewCodec() Interface

NewCodec returns a new JSON codec.

func NewCustom

func NewCustom(encSel EncoderSelector) Interface

NewCustom returns a new JSON codec based on passed encoder selector.

type Request

type Request interface {
	// HandleError from input and request instance
	HandleError(err error) bool
	// Reads the request and returns the RPC method name.
	Method() string
	// Reads the request filling the RPC method args.
	ReadRequest(interface{}) error
	// Writes the response using the RPC method reply.
	WriteResponse(interface{})
	// Writes an error produced by the server.
	WriteError(status int, err error)
}

Request decodes a request and encodes a response using a specific serialization scheme.

Jump to

Keyboard shortcuts

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