-
Notifications
You must be signed in to change notification settings - Fork 182
/
rest.go
163 lines (144 loc) · 5.42 KB
/
rest.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
package rest
import (
"fmt"
"net/http"
"github.com/okex/exchain/x/token/types"
"encoding/json"
"strings"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
"github.com/okex/exchain/libs/cosmos-sdk/client/context"
"github.com/okex/exchain/libs/cosmos-sdk/types/rest"
"github.com/gorilla/mux"
"github.com/okex/exchain/x/common"
)
// RegisterRoutes, a central function to define routes
// which is called by the rest module in main application
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, storeName string) {
r.HandleFunc(fmt.Sprintf("/token/{symbol}"), tokenHandler(cliCtx, storeName)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/tokens"), tokensHandler(cliCtx, storeName)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/currency/describe"), currencyDescribeHandler(cliCtx, storeName)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/accounts/{address}"), spotAccountsHandler(cliCtx, storeName)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/upload"), uploadAccountsHandler(cliCtx, storeName)).Methods("GET")
}
func tokenHandler(cliCtx context.CLIContext, storeName string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
tokenName := vars["symbol"]
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/info/%s", storeName, tokenName), nil)
if err != nil {
sdkErr := common.ParseSDKError(err.Error())
common.HandleErrorMsg(w, cliCtx, sdkErr.Code, err.Error())
return
}
result := common.GetBaseResponse("hello")
result2, err2 := json.Marshal(result)
if err2 != nil {
common.HandleErrorMsg(w, cliCtx, common.CodeMarshalJSONFailed, err2.Error())
return
}
result2 = []byte(strings.Replace(string(result2), "\"hello\"", string(res), 1))
rest.PostProcessResponse(w, cliCtx, result2)
}
}
func tokensHandler(cliCtx context.CLIContext, storeName string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ownerAddress := r.URL.Query().Get("address")
if _, err := sdk.AccAddressFromBech32(ownerAddress); err != nil {
common.HandleErrorResponseV2(w, http.StatusBadRequest, common.ErrorInvalidParam)
return
}
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/tokens/%s", storeName, ownerAddress), nil)
if err != nil {
sdkErr := common.ParseSDKError(err.Error())
common.HandleErrorMsg(w, cliCtx, sdkErr.Code, err.Error())
return
}
result := common.GetBaseResponse("hello")
result2, err2 := json.Marshal(result)
if err2 != nil {
common.HandleErrorMsg(w, cliCtx, common.CodeMarshalJSONFailed, err2.Error())
return
}
result2 = []byte(strings.Replace(string(result2), "\"hello\"", string(res), 1))
rest.PostProcessResponse(w, cliCtx, result2)
}
}
func currencyDescribeHandler(cliCtx context.CLIContext, storeName string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/currency/describe", storeName), nil)
if err != nil {
sdkErr := common.ParseSDKError(err.Error())
common.HandleErrorMsg(w, cliCtx, sdkErr.Code, err.Error())
return
}
result := common.GetBaseResponse("hello")
result2, err2 := json.Marshal(result)
if err2 != nil {
common.HandleErrorMsg(w, cliCtx, common.CodeMarshalJSONFailed, err2.Error())
return
}
result2 = []byte(strings.Replace(string(result2), "\"hello\"", string(res), 1))
rest.PostProcessResponse(w, cliCtx, result2)
}
}
func spotAccountsHandler(cliCtx context.CLIContext, storeName string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
address := vars["address"]
symbol := r.URL.Query().Get("symbol")
show := r.URL.Query().Get("show")
if show == "" {
show = "partial"
}
if show != "partial" && show != "all" {
result := common.GetErrorResponseJSON(1, "", "param show not valid")
rest.PostProcessResponse(w, cliCtx, result)
return
}
accountParam := types.AccountParam{
Symbol: symbol,
Show: show,
//QueryPage: token.QueryPage{
// Page: pageInt,
// PerPage: perPageInt,
//},
}
bz, err := cliCtx.Codec.MarshalJSON(accountParam)
if err != nil {
common.HandleErrorMsg(w, cliCtx, common.CodeMarshalJSONFailed, err.Error())
return
}
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/accounts/%s", storeName, address), bz)
if err != nil {
sdkErr := common.ParseSDKError(err.Error())
common.HandleErrorMsg(w, cliCtx, sdkErr.Code, err.Error())
return
}
result := common.GetBaseResponse("hello")
result2, err2 := json.Marshal(result)
if err2 != nil {
common.HandleErrorMsg(w, cliCtx, common.CodeMarshalJSONFailed, err2.Error())
return
}
result2 = []byte(strings.Replace(string(result2), "\"hello\"", string(res), 1))
rest.PostProcessResponse(w, cliCtx, result2)
}
}
func uploadAccountsHandler(cliCtx context.CLIContext, storeName string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/upload", storeName), nil)
if err != nil {
sdkErr := common.ParseSDKError(err.Error())
common.HandleErrorMsg(w, cliCtx, sdkErr.Code, err.Error())
return
}
result := common.GetBaseResponse("hello")
result2, err2 := json.Marshal(result)
if err2 != nil {
common.HandleErrorMsg(w, cliCtx, common.CodeMarshalJSONFailed, err2.Error())
return
}
result2 = []byte(strings.Replace(string(result2), "\"hello\"", string(res), 1))
rest.PostProcessResponse(w, cliCtx, result2)
}
}