You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.0 KiB
Go

6 years ago
package jsonrpc
import (
6 years ago
"context"
6 years ago
"encoding/json"
"errors"
6 years ago
"fmt"
"net/url"
6 years ago
)
// Client json rpc client
type Client struct {
// URL is remote url,
// example
// http://username:password@192.168.1.3:1001/jsonrpc
// ws://192.168.0.1:9121/
URL string
Transport Transport
6 years ago
}
type request struct {
Version string `json:"jsonrpc"`
Method string `json:"method"`
Params interface{} `json:"params"`
ID string `json:"id"`
6 years ago
}
type response struct {
Version string `json:"jsonrpc"`
Result json.RawMessage `json:"result"`
Error *Error `json:"error"`
ID string `json:"id"`
Method string `json:"method"`
Params json.RawMessage `json:"params"`
6 years ago
}
// Error rpc error
type Error struct {
Code int `json:"code"`
Message string `json:"message"`
Data json.RawMessage `json:"data"`
6 years ago
}
func (e *Error) Error() string {
return fmt.Sprintf("code: %d, message: %s, data: %s",
e.Code, e.Message, e.Data)
}
6 years ago
// NewClient create a new jsonrpc client
func NewClient(uri string) (*Client, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, err
}
t := ""
switch u.Scheme {
case "http", "https":
t = "http"
case "ws", "wss":
t = "ws"
default:
return nil, fmt.Errorf("not supported %s", u.Scheme)
}
if t == "http" {
tr, _ := NewHTTPTransport(uri, nil)
return &Client{Transport: tr, URL: uri}, nil
}
if t == "ws" {
6 years ago
tr, _ := NewWebsocketTransport(context.Background(), uri)
return &Client{Transport: tr, URL: uri}, nil
}
return nil, errors.New("not supported")
6 years ago
}
// Call invoke a method with args and return reply
func (c *Client) Call(method string, args interface{}, reply interface{}) error {
return c.Transport.Call(method, args, reply)
}
// Subscribe subscribe for change
func (c *Client) Subscribe(method, notifyMethod string,
args interface{}, reply interface{}) (chan json.RawMessage, chan *Error, error) {
6 years ago
return c.Transport.Subscribe(method, notifyMethod, args, reply)
6 years ago
}