-
Notifications
You must be signed in to change notification settings - Fork 3
/
conn.go
145 lines (130 loc) · 3.24 KB
/
conn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package simpleBlockchain
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
type Conn struct {
conn *http.Client
url string
}
type Response struct {
Result json.RawMessage `json:"result"`
}
func NewConn(url string) *Conn{
if url == "" {
url = "http://127.0.0.1:8080"
}
return &Conn{
conn: &http.Client{
Timeout: 30 * time.Second,
},
url: url,
}
}
func (c *Conn) get(route string, result interface{}) error {
req, err := http.NewRequest("GET",fmt.Sprintf("%s/%s", c.url, route), nil)
if err != nil{
return fmt.Errorf("consturct http request error: %v\n", err)
}
res, err := c.conn.Do(req)
if err != nil {
return fmt.Errorf("connected error: %v\n", err)
}
defer res.Body.Close()
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
switch res.StatusCode {
case 200:
var response Response
err = json.Unmarshal(resBody, &response)
if err != nil {
return fmt.Errorf("json unmarshal resbody error: %v\n", err)
}
err = json.Unmarshal(response.Result, result)
if err != nil {
return fmt.Errorf("json Unmarshal response result error: %v\n", err)
}
return nil
case 500:
return fmt.Errorf("%s\n",string(resBody))
}
return nil
}
func (c *Conn) post(route string, result interface{}, msg interface{}) error {
body, err := json.Marshal(msg)
if err != nil{
fmt.Errorf("json marshal error: %v", err)
}
req, err := http.NewRequest("POST",fmt.Sprintf("%s/%s", c.url, route), bytes.NewReader(body))
req.Header.Set("Content-Type","application/json")
if err != nil{
return fmt.Errorf("consturct http request error: %v\n", err)
}
res, err := c.conn.Do(req)
if err != nil {
return fmt.Errorf("connected error: %v\n", err)
}
defer res.Body.Close()
resBody, err := ioutil.ReadAll(res.Body)
switch res.StatusCode {
case 500:
return fmt.Errorf("Server internal occured error: %s", string(resBody))
case 400:
return fmt.Errorf("Bad Request error: %s", string(resBody))
}
if err != nil {
return err
}
var response Response
err = json.Unmarshal(resBody, &response)
if err != nil {
return fmt.Errorf("json unmarshal resbody error: %v\n", err)
}
err = json.Unmarshal(response.Result, result)
if err != nil {
return fmt.Errorf("json Unmarshal response result error: %v\n", err)
}
return nil
}
func (c *Conn) MiningBlock() (block Block, err error){
err = c.get("chain/mining", &block)
return
}
func (c *Conn) GetBlocks() (blocks []*Block, err error) {
err = c.get("chain/blocks", &blocks)
return
}
func (c *Conn) GetBlockHashes() (hashes [][]byte, err error){
err = c.get("chain/hashes", &hashes)
return
}
func (c *Conn) GetBlockHeight() (height int, err error){
err = c.get("chain/height", &height)
return
}
func (c *Conn) GetUTXOs() (utxos []*UTXO, err error){
err = c.get("chain/utxos", &utxos)
return
}
func (c *Conn) GetWalletAddress() (addresses []string, err error){
err = c.get("wallet/address", &addresses)
return
}
func (c *Conn) GetWalletUTXOs() (utxos []*UTXO, err error){
err = c.get("wallet/utxos", &utxos)
return
}
func (c *Conn) GetWalletBalance() (balance int, err error){
err = c.get("wallet/balance", &balance)
return
}
func (c *Conn) SendTransaction(txobj TransactionObj) (tx Transaction, err error){
err = c.post("wallet/send", &tx, txobj)
return
}