jsonrpc

package module
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2023 License: MIT Imports: 3 Imported by: 0

README

JSONRPC Client/Server

According to the JSON-RPC 2.0 specification, JSON-RPC is a lightweight remote procedure call (RPC) protocol.

PkgGoDev Build Status Go Report Card Coverage Status GitHub issues Release

Installation

To install the package, run:

go get github.com/go-zoox/jsonrpc

Getting Started

// server
package main

import (
	"context"

	"github.com/go-zoox/jsonrpc"
	"github.com/go-zoox/jsonrpc/server"
	"github.com/go-zoox/logger"
)

func main() {
	s := server.New()

	s.Register("echo", func(ctx context.Context, params jsonrpc.Params) (jsonrpc.Result, error) {
		logger.Info("params: %s", params)

		return jsonrpc.Result{
			"name": params.Get("name"),
			"age":  18,
		}, nil
	})

	s.Run()
}
// client
package main

import (
	"context"

	"github.com/go-zoox/core-utils/cast"
	"github.com/go-zoox/jsonrpc"
	"github.com/go-zoox/jsonrpc/client"
	"github.com/go-zoox/logger"
)

func main() {
	c := client.New("http://localhost:8080")

	r, err := c.Call(context.Background(), "echo", jsonrpc.Params{
		"name": "zero",
	})
	if err != nil {
		logger.Errorf("failed to call: %s", err)
		return
	}

	logger.Info("result: %d", cast.ToInt64(r.Get("age")))
}

License

GoZoox is released under the MIT License.

Documentation

Index

Constants

View Source
const JSONRPCDefaultPath = "/jsonrpc"

JSONRPCDefaultPath is the path of JSON-RPC.

View Source
const JSONRPCVersion = "2.0"

JSONRPCVersion is the version of JSON-RPC.

Variables

View Source
var Version = "1.2.2"

Version is the current version of the jsonrpc package.

Functions

This section is empty.

Types

type Error added in v1.2.0

type Error struct {
	Code    int64  `json:"code"`
	Message string `json:"message"`
}

Error is a JSON-RPC error.

type Params added in v1.0.4

type Params map[string]any

Params is a map of params.

func (Params) Bind added in v1.2.0

func (p Params) Bind(obj any) error

Bind binds the jsonrpc params into the given struct.

func (Params) Get added in v1.2.0

func (p Params) Get(key string) any

Get returns the value of the given key.

type Request added in v1.2.0

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

Request is a JSON-RPC request.

type Response added in v1.2.0

type Response struct {
	JSONRPC string `json:"jsonrpc"`
	Result  Result `json:"result"`
	Error   *Error `json:"error"`
	ID      string `json:"id"`
}

Response is a JSON-RPC response.

type Result added in v1.0.4

type Result map[string]any

Result is a map of result.

func (Result) Bind added in v1.2.0

func (p Result) Bind(obj any) error

Bind binds the jsonrpc params into the given struct.

func (Result) Get added in v1.2.0

func (p Result) Get(key string) any

Get returns the value of the given key.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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