-
Notifications
You must be signed in to change notification settings - Fork 1
/
whitelist_api.go
157 lines (146 loc) · 4.55 KB
/
whitelist_api.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
package metax
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
type AddDestinationRequest struct {
DestinationAddresses []string `json:"destinationAddresses"`
}
type AddDestinationResponse struct {
Code int `json:"code"`
Message string `json:"message"`
RegisteredCount int `json:"registeredCount"`
DuplicateContracts []string `json:"duplicateContracts"`
InvalidContracts []string `json:"invalidContracts"`
}
type AddProxyContractsRequest struct {
Addresses []string `json:"addresses"`
}
type ProxyContractsResponse struct {
Code int `json:"code"`
Message string `json:"message"`
}
type GetProxyContractsResponse struct {
ProxyContractsResponse
Total int `json:"total"`
Addresses []struct {
Address string `json:"address"`
Status bool `json:"status"`
} `json:"addresses"`
}
type PatchProxyContractsRequest struct {
Status int `json:"status"` // 0 => inactive, 1 => active
Address string `json:"address"`
}
func (b *Bcnmy) AddDestinationAddresses(data *AddDestinationRequest) (*AddDestinationResponse, error) {
responseCh := make(chan interface{}, 1)
errorCh := make(chan error)
body, err := json.Marshal(data)
if err != nil {
b.logger.WithError(err).Error("json marshal `AddDestinationRequest` data failed")
return nil, err
}
req, err := http.NewRequest(http.MethodPost, AddDestinationAddressesURL, bytes.NewBuffer(body))
if err != nil {
b.logger.WithError(err).Error("AddDestinationAddresses NewRequest failed")
return nil, err
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Authorization", b.GetAuthorization())
var resp AddDestinationResponse
b.asyncHttpx(req, &resp, errorCh, responseCh)
select {
case ret := <-responseCh:
resp, ok := ret.(*AddDestinationResponse)
if !ok {
return nil, fmt.Errorf("AddDestination failed")
}
return resp, nil
case err := <-errorCh:
b.logger.Error(err.Error())
return nil, err
}
}
func (b *Bcnmy) AddProxyContracts(data *AddProxyContractsRequest) (*ProxyContractsResponse, error) {
responseCh := make(chan interface{}, 1)
errorCh := make(chan error)
body, err := json.Marshal(data)
if err != nil {
b.logger.WithError(err).Error("json marshal `AddProxyContractsRequest` data failed")
return nil, err
}
req, err := http.NewRequest(http.MethodPost, ProxyContractsURL, bytes.NewBuffer(body))
if err != nil {
b.logger.WithError(err).Error("AddProxyContracts NewRequest failed")
return nil, err
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Authorization", b.GetAuthorization())
var resp ProxyContractsResponse
b.asyncHttpx(req, &resp, errorCh, responseCh)
select {
case ret := <-responseCh:
resp, ok := ret.(*ProxyContractsResponse)
if !ok {
return nil, fmt.Errorf("AddProxyContracts failed")
}
return resp, nil
case err := <-errorCh:
b.logger.Error(err.Error())
return nil, err
}
}
func (b *Bcnmy) PatchProxyContracts(data *PatchProxyContractsRequest) (*ProxyContractsResponse, error) {
responseCh := make(chan interface{}, 1)
errorCh := make(chan error)
body, err := json.Marshal(data)
if err != nil {
b.logger.WithError(err).Error("json marshal `PatchProxyContractsRequest` data failed")
return nil, err
}
req, err := http.NewRequest(http.MethodPatch, ProxyContractsURL, bytes.NewBuffer(body))
if err != nil {
b.logger.WithError(err).Error("PatchProxyContracts NewRequest failed")
return nil, err
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Authorization", b.GetAuthorization())
var resp ProxyContractsResponse
b.asyncHttpx(req, &resp, errorCh, responseCh)
select {
case ret := <-responseCh:
resp, ok := ret.(*ProxyContractsResponse)
if !ok {
return nil, fmt.Errorf("PatchProxyContracts failed")
}
return resp, nil
case err := <-errorCh:
b.logger.Error(err.Error())
return nil, err
}
}
func (b *Bcnmy) GetProxyContracts() (*GetProxyContractsResponse, error) {
responseCh := make(chan interface{}, 1)
errorCh := make(chan error)
req, err := http.NewRequest(http.MethodGet, ProxyContractsURL, nil)
if err != nil {
b.logger.WithError(err).Error("GetProxyContracts NewRequest failed")
return nil, err
}
req.Header.Set("Authorization", b.GetAuthorization())
var resp GetProxyContractsResponse
b.asyncHttpx(req, &resp, errorCh, responseCh)
select {
case ret := <-responseCh:
resp, ok := ret.(*GetProxyContractsResponse)
if !ok {
return nil, fmt.Errorf("GetProxyContracts failed")
}
return resp, nil
case err := <-errorCh:
b.logger.Error(err.Error())
return nil, err
}
}