forked from lmxdawn/wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
162 lines (135 loc) · 3.06 KB
/
http.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package client
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"time"
)
// Response ...
type Response struct {
Code int `json:"code"` // 错误code码
Message string `json:"message"` // 错误信息
}
func (br Response) Success() bool {
return br.Code == 0
}
type HttpClient struct {
protocol string
coinName string
rechargeNotifyUrl string
withdrawNotifyUrl string
client *http.Client
}
// NewHttpClient 创建
func NewHttpClient(protocol, coinName, rechargeNotifyUrl, withdrawNotifyUrl string) *HttpClient {
return &HttpClient{
protocol,
coinName,
rechargeNotifyUrl,
withdrawNotifyUrl,
&http.Client{
Timeout: time.Millisecond * time.Duration(10*1000),
},
}
}
// RechargeSuccess 充值成功通知
func (h *HttpClient) RechargeSuccess(hash string, status uint, address string, value int64) error {
data := make(map[string]interface{})
data["protocol"] = h.protocol
data["coinName"] = h.coinName
data["hash"] = hash
data["status"] = status
data["address"] = address
data["value"] = value
var res Response
err := h.post(h.withdrawNotifyUrl, data, &res)
if err != nil {
return err
}
if !res.Success() {
return errors.New(res.Message)
}
return nil
}
// WithdrawSuccess 提现成功通知
func (h *HttpClient) WithdrawSuccess(hash string, status uint, orderId string, address string, value int64) error {
data := make(map[string]interface{})
data["protocol"] = h.protocol
data["coinName"] = h.coinName
data["hash"] = hash
data["status"] = status
data["orderId"] = orderId
data["address"] = address
data["value"] = value
var res Response
err := h.post(h.withdrawNotifyUrl, data, &res)
if err != nil {
return err
}
if !res.Success() {
return errors.New(res.Message)
}
return nil
}
// get 请求
func (h *HttpClient) get(urlStr string, params url.Values, res interface{}) error {
Url, err := url.Parse(urlStr)
if err != nil {
return err
}
//如果参数中有中文参数,这个方法会进行URLEncode
Url.RawQuery = params.Encode()
urlPath := Url.String()
req, err := http.NewRequest(http.MethodGet, urlPath, nil)
if err != nil {
// handle error
return err
}
resp, err := h.client.Do(req)
if err != nil {
// handle error
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(body, res)
if err != nil {
return err
}
return nil
}
// post 请求
func (h *HttpClient) post(urlStr string, data map[string]interface{}, res interface{}) error {
bytesData, err := json.Marshal(data)
if err != nil {
return err
}
req, err := http.NewRequest(http.MethodPost, urlStr, bytes.NewReader(bytesData))
if err != nil {
// handle error
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := h.client.Do(req)
if err != nil {
// handle error
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
// handle error
return err
}
err = json.Unmarshal(body, res)
if err != nil {
return err
}
return nil
}