jsonrpc

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2021 License: GPL-3.0 Imports: 13 Imported by: 0

README

JSON-RPC 2.0 Module for golang

A go implementation of JSON RPC 2.0 over http. Work in progress.

Installing

To start using this library, install Go 1.13 or above. Run the following command to retrieve the library.

$ go get -u github.com/echovl/jsonrpc

Server

package main

import (
        "context"
        "net/http"

        "github.com/echovl/jsonrpc"
)

type User struct {
        ID   int    `json:"id"`
        Name string `json:"name"`
}

func getUser(ctx context.Context, id string) (User, error) {
        return User{ID: 1, Name: "Jhon Doe"}, nil
}

func main() {
        server := jsonrpc.NewServer()
        server.HandleFunc("getUserById", getUser)

        http.Handle("/api", server)
        http.ListenAndServe(":4545", nil)
}

Client

package main

import (
	"context"
	"fmt"

	"github.com/echovl/jsonrpc"
)

type User struct {
	ID   int    `json:"id"`
	Name string `json:"name"`
}

func main() {
	client := jsonrpc.NewClient("http://127.0.0.1:4545/api")
	user := &User{}
	resp, err := client.Call(context.Background(), "getUserById", "id")
	if err != nil {
		panic(err)
	}
    	if err := resp.Decode(user); err != nil {
		panic(err)
    	}
	fmt.Println("user: ", user)
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrorParseError   = &Error{-32700, "Parse error", nil}
	ErrInvalidRequest = &Error{-32600, "Invalid Request", nil}
	ErrMethodNotFound = &Error{-32601, "Method not found", nil}
	ErrInvalidParams  = &Error{-32602, "Invalid params", nil}
	ErrInternalError  = &Error{-32603, "Internal error", nil}
)

Functions

This section is empty.

Types

type Client

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

Client represents a JSON-RPC Client.

func NewClient

func NewClient(url string) *Client

NewClient returns a new Client to handle requests to a JSON-RPC server. TODO: support custom httpClients

func (*Client) Call

func (c *Client) Call(ctx context.Context, method string, params interface{}) (*Response, error)

Call executes the named method, waits for it to complete, and returns a JSONRPC response.

func (*Client) Notify

func (c *Client) Notify(ctx context.Context, method string, params interface{}) error

Notify executes the named method and discards the response.

type Error

type Error struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"` // defined by the server
}

Error represents a JSON-RPC error, it implements the error interface.

func (*Error) Error

func (e *Error) Error() string

Error returns the string representation of the error.

type Response added in v0.1.3

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

Response represents the Response from a JSON-RPC request.

func (*Response) Decode added in v0.1.3

func (r *Response) Decode(v interface{}) error

Decode will unmarshal the Response's result into v. If there was an error in the Response, that error will be returned.

func (*Response) Err added in v0.1.3

func (r *Response) Err() error

func (*Response) ID added in v0.1.3

func (r *Response) ID() interface{}

type Server

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

Server represents a JSON-RPC server.

Example
package main

import (
	"context"
	"net/http"

	"github.com/echovl/jsonrpc"
)

type Version struct {
	Tag string
}

func main() {
	server := jsonrpc.NewServer()
	err := server.HandleFunc("version", func(ctx context.Context) (Version, error) {
		return Version{"1.0.0"}, nil
	})
	if err != nil {
		panic(err)
	}

	http.Handle("/api", server)
	http.ListenAndServe(":4545", nil)
}
Output:

func NewServer

func NewServer() *Server

NewServer returns a new Server.

func (*Server) HandleFunc

func (s *Server) HandleFunc(method string, handler interface{}) error

HandleFunc registers the handle function for the given JSON-RPC method.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(rw http.ResponseWriter, r *http.Request)

ServeHTTP responds to an JSON-RPC request and executes the requested method.

Jump to

Keyboard shortcuts

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