This repository has been archived by the owner on Aug 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
190 lines (166 loc) · 5.29 KB
/
main.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"bufio"
"encoding/json"
"fmt"
"github.com/kardianos/osext"
"io/ioutil"
"kumex-api-demo/sdk"
"log"
"os"
"reflect"
"strings"
)
var (
kumex *sdk.ApiService
FILE = "/config.json"
)
func main() {
//1. get api authentication info from user input or configuration file.
var apiURI, apiKey, apiSecret, apiPassphrase string
var apiSkipVerifyTls bool
// read config from json file
var config ApiConfig
folderPath, err := osext.ExecutableFolder()
file, er := ioutil.ReadFile(folderPath + FILE)
if er != nil {
log.Println("Failed to read configuration from json file:", er)
log.Println("Now please input api information manually.")
}else{
// unmarshal config data
err := json.Unmarshal(file, &config)
if err != nil {
log.Fatal("error when unmarshal config from json file:", err)
return
}
}
// if we have configuration from json file
if len(config.ApiBaseURI) > 0 {
apiURI = config.ApiBaseURI
} else {
log.Println("please input api base URI,such as:https://api.kumex.com")
fmt.Scanf("%s", &apiURI)
}
if len(config.ApiKey) > 0 {
apiKey = config.ApiKey
} else {
log.Println("please input your api key...")
fmt.Scanf("%s", &apiKey)
}
if len(config.ApiSecret) > 0 {
apiSecret = config.ApiSecret
} else {
log.Println("please input your api secret...")
fmt.Scanf("%s", &apiSecret)
}
if len(config.ApiPassphrase) > 0 {
apiPassphrase = config.ApiPassphrase
} else {
log.Println("please input your api passphrase...")
fmt.Scanf("%s", &apiPassphrase)
}
apiSkipVerifyTls = config.ApiSkipVerifyTls
initApiService(apiURI, apiKey, apiSecret, apiPassphrase, apiSkipVerifyTls)
// 2. api function map to user input.
apiMap := initApiMap()
// 3. get api info from user input.
var apiName string
log.Println("please input the api name which you want to test...")
fmt.Scanf("%s", &apiName)
log.Println("please input the api parameters,use ',' to spilt parameters.")
log.Println("if it is no parameter api,just enter to skip...")
reader := bufio.NewReader(os.Stdin)
inputs, err := reader.ReadString('\n')
if err != nil {
log.Fatal("input parameter error:", err)
}
var params []string
if len(strings.TrimSpace(inputs)) > 0 {
params = strings.Split(strings.TrimSpace(inputs), ",")
}
// 4. call api function which user choose to test.
call(apiMap, apiName, params)
}
// initial api configuration information from json file.
type ApiConfig struct {
ApiBaseURI string `json:ApiBaseURI`
ApiKey string `json:ApiKey`
ApiSecret string `json:ApiSecret`
ApiPassphrase string `json:ApiPassphrase`
ApiSkipVerifyTls bool `json:ApiSkipVerifyTls`
}
// initApiService init an api service
func initApiService(apiURI, apiKey, apiSecret, apiPassphrase string, apiSkipVerifyTls bool) {
kumex = sdk.NewApiService(
sdk.ApiBaseURIOption(apiURI),
sdk.ApiKeyOption(apiKey),
sdk.ApiSecretOption(apiSecret),
sdk.ApiPassPhraseOption(apiPassphrase),
sdk.ApiSkipVerifyTlsOption(apiSkipVerifyTls),
)
sdk.DebugMode = true
}
// call according to string input to call function which named the input.
func call(m map[string]interface{}, name string, params []string) {
// if string input is a function
f := reflect.ValueOf(m[name])
if f.Kind() != reflect.Func {
log.Fatal("input error,your input is not a name of api.")
return
}
in := make([]reflect.Value, len(params))
// if the function has no parameters
if len(params) > 0 {
for k, v := range params {
fmt.Println(v)
in[k] = reflect.ValueOf(v)
}
} else {
in = nil
}
f.Call(in)
}
// initApiMap init api function mapping
func initApiMap() map[string]interface{} {
return map[string]interface{}{
"AccountOverview": kumex.AccountOverview,
"TransactionHistory": kumex.TransactionHistory,
"DepositAddresses": kumex.DepositAddresses,
"Deposits": kumex.Deposits,
"WithdrawalQuotas": kumex.WithdrawalQuotas,
"ApplyWithdrawal": kumex.ApplyWithdrawal,
"Withdrawals": kumex.Withdrawals,
"CancelWithdrawal": kumex.CancelWithdrawal,
"TransferOut": kumex.TransferOut,
"TransferOutV2": kumex.TransferOutV2,
"TransferList": kumex.TransferList,
"CancelTransfer": kumex.CancelTransfer,
"Fills": kumex.Fills,
"RecentFills": kumex.RecentFills,
"OpenOrderStatistics": kumex.OpenOrderStatistics,
"CreateOrder": kumex.CreateOrder,
"CancelOrder": kumex.CancelOrder,
"CancelOrders": kumex.CancelOrders,
"StopOrders": kumex.StopOrders,
"GetStopOrders": kumex.GetStopOrders,
"Orders": kumex.Orders,
"Order": kumex.Order,
"RecentDoneOrders": kumex.RecentDoneOrders,
"Ticker": kumex.Ticker,
"Level2Snapshot": kumex.Level2Snapshot,
"Level2MessageQuery": kumex.Level2MessageQuery,
"Level3Snapshot": kumex.Level3Snapshot,
"Level3MessageQuery": kumex.Level3MessageQuery,
"TradeHistory": kumex.TradeHistory,
"InterestQuery": kumex.InterestQuery,
"IndexQuery": kumex.IndexQuery,
"MarkPrice": kumex.MarkPrice,
"PremiumQuery": kumex.PremiumQuery,
"FundingRate": kumex.FundingRate,
"ActiveContracts": kumex.ActiveContracts,
"Contracts": kumex.Contracts,
"WebSocketPublicToken": kumex.WebSocketPublicToken,
"WebSocketPrivateToken": kumex.WebSocketPrivateToken,
"NewWebSocketClient": kumex.NewWebSocketClient,
}
}