OSDN Git Service

add get vote result (#345)
[bytom/vapor.git] / toolbar / apinode / node.go
1 package apinode
2
3 import (
4         "encoding/json"
5
6         "github.com/vapor/errors"
7         "github.com/vapor/toolbar/common"
8 )
9
10 // Node can invoke the api which provide by the full node server
11 type Node struct {
12         hostPort string
13 }
14
15 // NewNode create a api client with target server
16 func NewNode(hostPort string) *Node {
17         return &Node{hostPort: hostPort}
18 }
19
20 type response struct {
21         Status    string          `json:"status"`
22         Data      json.RawMessage `json:"data"`
23         ErrDetail string          `json:"error_detail"`
24 }
25
26 func (n *Node) request(path string, payload []byte, respData interface{}) error {
27         resp := &response{}
28         if err := common.Post(n.hostPort+path, payload, resp); err != nil {
29                 return err
30         }
31
32         if resp.Status != "success" {
33                 return errors.New(resp.ErrDetail)
34         }
35
36         return json.Unmarshal(resp.Data, respData)
37 }