jsonrpc

package module
v0.0.0-...-3857973 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2017 License: MIT Imports: 11 Imported by: 0

README

jsonrpc

JSONRPC on echo

Install

Install Server
go get -u github.com/plimble/jsonrpc
Install client
go get -u github.com/plimble/jsonrpc/client
Install Both
go get -u github.com/plimble/jsonrpc/...

Example Server

package main

import (
	"context"

	"github.com/labstack/echo"
	"github.com/plimble/jsonrpc"
)

type Adder struct{}

type AddReq struct {
	A, B int
}

type AddRes struct {
	Val int
}

func (a *Adder) Add(ctx context.Context, req *AddReq) (*AddRes, error) {
	val := req.A + req.B
	return &AddRes{
		Val: val,
	}, nil
}

func (a *Adder) Multiply(ctx context.Context, req *AddReq) (*AddRes, error) {
	val := req.A * req.B
	return &AddRes{
		Val: val,
	}, nil
}

func main() {
	j := jsonrpc.New()
	j.Register(new(Adder), "adder")
	e := echo.New()
	e.POST("/", j.Handle)
	e.POST("/batch", j.HandleBatch)

	e.Start(":3000")
}

Example Client

package main

import (
	"fmt"

	"github.com/plimble/jsonrpc/client"
)

func main() {
	c := client.New("http://localhost:3000", "/", "/batch")
	res, err := c.Request("adder.Add", client.Params{"a": 1, "b": 4})
	if err != nil {
		panic(err)
	}
	if res.Error != nil {
		panic(res.Error)
	}

	result := make(map[string]interface{})
	res.UnmarshalResult(&result)
	fmt.Println("Result:", result)

	ress, err := c.Requests(&client.Requests{
		client.NewRequest("adder.Add", client.Params{"a": 1, "b": 2}),
		client.NewRequest("adder.Add", client.Params{"a": 2, "b": 2}),
		client.NewRequest("adder.Add", client.Params{"a": 3, "b": 2}),
		client.NewRequest("adder.Multiply", client.Params{"a": 4, "b": 2}),
	})
	if err != nil {
		panic(err)
	}

	for _, res := range ress {
		result := make(map[string]interface{})
		res.UnmarshalResult(&result)
		fmt.Println("Batch Result:", result)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JsonRpc

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

func New

func New() *JsonRpc

func (*JsonRpc) Handle

func (rpc *JsonRpc) Handle(c echo.Context) error

func (*JsonRpc) HandleBatch

func (rpc *JsonRpc) HandleBatch(c echo.Context) error

func (*JsonRpc) Register

func (rpc *JsonRpc) Register(service interface{}, name string)

type Params

type Params map[string]interface{}

type Request

type Request struct {
	Id     string          `json:"id"`
	Method string          `json:"method"`
	Params json.RawMessage `json:"params"`
}

type Requests

type Requests []*Request

type Response

type Response struct {
	Id     string         `json:"id,omitempty"`
	Error  *ResponseError `json:"error,omitempty"`
	Result interface{}    `json:"result,omitempty"`
}

func (*Response) SetErr

func (r *Response) SetErr(code int, message string)

type ResponseError

type ResponseError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

type Responses

type Responses []*Response

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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