6 "github.com/bytom/vapor/errors"
7 "github.com/bytom/vapor/netsync/peers"
8 "github.com/bytom/vapor/toolbar/common"
11 // Node can invoke the api which provide by the full node server
16 // NewNode create a api client with target server
17 func NewNode(hostPort string) *Node {
18 return &Node{hostPort: hostPort}
21 type response struct {
22 Status string `json:"status"`
23 Data json.RawMessage `json:"data"`
24 ErrDetail string `json:"error_detail"`
27 func (n *Node) request(path string, payload []byte, respData interface{}) error {
29 if err := common.Post(n.hostPort+path, payload, resp); err != nil {
33 if resp.Status != "success" {
34 return errors.New(resp.ErrDetail)
41 return json.Unmarshal(resp.Data, respData)
44 func (n *Node) DisconnectPeer(peerID string) error {
45 url := "/disconnect-peer"
46 payload, err := json.Marshal(struct {
47 PeerID string `json:"peer_id"`
55 return n.request(url, payload, nil)
59 func (n *Node) ConnectPeer(ip string, port uint16) (*peers.Peer, error) {
60 url := "/connect-peer"
61 payload, err := json.Marshal(struct {
63 Port uint16 `json:"port"`
69 return nil, errors.Wrap(err, "json marshal")
73 return res, n.request(url, payload, res)