cborrpc

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2023 License: AGPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CodeParseError     = -32700
	CodeInvalidRequest = -32600
	CodeMethodNotFound = -32601
	CodeInvalidParams  = -32602
	CodeInternalError  = -32603
	// -32000 to -32599	Server error, Reserved for implementation-defined server-errors.
	CodeServerError = -32000
)

refer to JSON-RPC 2.0. https://www.jsonrpc.org/specification

View Source
const (
	MIMEApplicationCBOR = "application/cbor"
)

Variables

This section is empty.

Functions

func ToJSONRaw added in v0.1.0

func ToJSONRaw(data cborpatch.RawMessage) (json.RawMessage, error)

ToJSONRaw try to convert a CBOR RawMessage to JSON RawMessage. If the CBOR RawMessage is invalid, it returns an error. If conversion fails, it returns a base64&checksum JSON RawMessage converting by encoding.EncodeToQuoteString.

Types

type Error

type Error = erring.Error

Error is a CBOR-RPC error.

type Handler

type Handler interface {
	// ServeRPC handles a CBOR-RPC request.
	ServeRPC(context.Context, *Request) *Response
}

type Request

type Request struct {
	ID     string          `cbor:"id"`
	Method string          `cbor:"method"`
	Params cbor.RawMessage `cbor:"params,omitempty"`
	// contains filtered or unexported fields
}

Request represents a CBOR-RPC request.

func (*Request) DecodeParams

func (req *Request) DecodeParams(params any) error

DecodeParams decodes the request parameters into the given value.

func (*Request) Error

func (req *Request) Error(err error) *Response

Error returns a response from the request with the given error.

func (*Request) Grow

func (req *Request) Grow(n int)

Grow grows the underlying buffer's capacity

func (*Request) InvalidMethod

func (req *Request) InvalidMethod() *Response

InvalidMethod returns a invalid method response from the request.

func (*Request) InvalidParams

func (req *Request) InvalidParams(msg string) *Response

InvalidParams returns a invalid params response from the request with the given message.

func (*Request) ReadFrom

func (req *Request) ReadFrom(r io.Reader) (int64, error)

ReadFrom decodes the request from the given reader. ReadFrom implements io.ReaderFrom interface.

func (*Request) Result

func (req *Request) Result(result any) *Response

Result returns a response from the request with the given result.

func (*Request) ResultRaw

func (req *Request) ResultRaw(result cbor.RawMessage) *Response

Result returns a response from the request with the given raw result.

func (*Request) String

func (req *Request) String() string

String returns the string representation of the request.

type Response

type Response struct {
	ID     string          `cbor:"id"`
	Error  *Error          `cbor:"error,omitempty"`
	Result cbor.RawMessage `cbor:"result,omitempty"`
	// contains filtered or unexported fields
}

This is a simple implementation of CBOR-RPC. Full reference to https://www.jsonrpc.org/specification Response represents a CBOR-RPC response.

func (*Response) DecodeResult

func (res *Response) DecodeResult(result any) error

DecodeResult decodes the result into the given value.

func (*Response) Grow

func (res *Response) Grow(n int)

Grow grows the underlying buffer's capacity

func (*Response) ReadFrom

func (res *Response) ReadFrom(r io.Reader) (int64, error)

ReadFrom decodes the CBOR-RPC response from the given reader. ReadFrom implements io.ReaderFrom interface.

func (*Response) String

func (res *Response) String() string

String returns the string representation of the response.

Jump to

Keyboard shortcuts

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