Help us shape the future of Go! Take the 2026 Go Developer Survey

rpc

package
v0.0.32 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2021 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type API

type API interface {
	APIs() APISet
}

API defines an interface for providing and accessing API functions. Packages that offer services accessed via RPC or any service-oriented interface must implement it.

type APISet

type APISet []MethodInfo

APISet defines a collection of APIs

func (*APISet) Add

func (a *APISet) Add(api MethodInfo)

Get gets an API function by name and namespace

func (*APISet) Get

func (a *APISet) Get(name string) *MethodInfo

Get gets an API function by name and namespace

type CallContext

type CallContext struct {

	// IsLocal indicates that the request originated locally
	IsLocal bool
}

CallContext contains information about an RPC request

type Err

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

Err represents JSON RPC error object

type Handler

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

Handler is responsible for handling incoming RPC requests by routing to a method that can handle the request and return a response.

func New

func New(mux *http.ServeMux, cfg *config.AppConfig) *Handler

New creates an instance of Handler

func (*Handler) APIs

func (s *Handler) APIs() APISet

APIs returns APIs for the jsonrpc package

func (*Handler) HasAPI

func (s *Handler) HasAPI(api MethodInfo) bool

HasAPI checks whether an API with matching full name exist

func (*Handler) MergeAPISet

func (s *Handler) MergeAPISet(apiSets ...APISet)

MergeAPISet merges an API set with s current api sets

func (*Handler) Methods

func (s *Handler) Methods() (methodsInfo []MethodInfo)

Methods gets the names of all methods in the API set.

type Method

type Method func(params interface{}) *Response

type MethodInfo

type MethodInfo struct {

	// Func is the API function to be executed.
	// Must be Method or MethodWithContext
	Func interface{} `json:"-"`

	// Namespace is the namespace where the method is under
	Namespace string `json:"namespace"`

	// Name is the name of the method
	Name string `json:"name"`

	// Private indicates a requirement for a private, authenticated
	// user session before this API function is executed.
	Private bool `json:"private"`

	// Desc describes the API
	Desc string `json:"description"`
}

MethodInfo describes an RPC method.

func (*MethodInfo) FullName

func (a *MethodInfo) FullName() string

type MethodWithContext

type MethodWithContext func(params interface{}, ctx *CallContext) *Response

type OnRequestFunc

type OnRequestFunc func(r *http.Request) error

OnRequestFunc is the type of function to use as a callback when newRPCHandler requests are received

type Params

type Params map[string]interface{}

Params represent JSON API parameters

func (*Params) Scan

func (p *Params) Scan(dest interface{}) error

Scan attempts to convert the params to a struct or map type

type Request

type Request struct {
	JSONRPCVersion string      `json:"jsonrpc"`
	Method         string      `json:"method"`
	Params         interface{} `json:"params"`
	ID             interface{} `json:"id,omitempty"`
}

Request represent a JSON RPC request

func (Request) IsNotification

func (r Request) IsNotification() bool

IsNotification checks whether the request is a notification according to JSON RPC specification. When ID is nil, we assume it's a notification request.

type Response

type Response struct {
	JSONRPCVersion string      `json:"jsonrpc"`
	Result         util.Map    `json:"result"`
	Err            *Err        `json:"error,omitempty"`
	ID             interface{} `json:"id,omitempty"` // string or float64
}

Response represents a JSON RPC response

func Error

func Error(code interface{}, message string, data interface{}) *Response

Error creates an error response

func StatusOK

func StatusOK() *Response

StatusOK creates a success response with data `{status:true}`

func Success

func Success(result util.Map) *Response

Success creates a success response

func (Response) IsError

func (r Response) IsError() bool

IsError checks whether r is an error response

func (Response) ToJSON

func (r Response) ToJSON() []byte

ToJSON returns the JSON encoding of r

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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