OSDN Git Service

api add createAccount and createKey (#393)
[bytom/vapor.git] / toolbar / apinode / node.go
index a056723..d15c068 100644 (file)
@@ -4,6 +4,7 @@ import (
        "encoding/json"
 
        "github.com/vapor/errors"
+       "github.com/vapor/netsync/peers"
        "github.com/vapor/toolbar/common"
 )
 
@@ -33,5 +34,41 @@ func (n *Node) request(path string, payload []byte, respData interface{}) error
                return errors.New(resp.ErrDetail)
        }
 
+       if resp.Data == nil {
+               return nil
+       }
+
        return json.Unmarshal(resp.Data, respData)
 }
+
+func (n *Node) DisconnectPeer(peerID string) error {
+       url := "/disconnect-peer"
+       payload, err := json.Marshal(struct {
+               PeerID string `json:"peer_id"`
+       }{
+               PeerID: peerID,
+       })
+       if err != nil {
+               return err
+       }
+
+       return n.request(url, payload, nil)
+
+}
+
+func (n *Node) ConnectPeer(ip string, port uint16) (*peers.Peer, error) {
+       url := "/connect-peer"
+       payload, err := json.Marshal(struct {
+               Ip   string `json:"ip"`
+               Port uint16 `json:"port"`
+       }{
+               Ip:   ip,
+               Port: port,
+       })
+       if err != nil {
+               return nil, errors.Wrap(err, "json marshal")
+       }
+
+       res := &peers.Peer{}
+       return res, n.request(url, payload, res)
+}