-
Notifications
You must be signed in to change notification settings - Fork 4
/
rest.go
95 lines (85 loc) · 2.86 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
package rest
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"
"github.com/zenchainprotocol/zenchain-node/x/common"
"github.com/zenchainprotocol/zenchain-node/x/order/keeper"
"github.com/zenchainprotocol/zenchain-node/x/order/types"
)
// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc("/order/depthbook", orderBookHandler(cliCtx)).Methods("GET")
r.HandleFunc("/order/{orderID}", orderDetailHandler(cliCtx)).Methods("GET")
}
func orderDetailHandler(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
orderID := vars["orderID"]
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/order/detail/%s", orderID), nil)
if err != nil {
sdkErr := common.ParseSDKError(err.Error())
common.HandleErrorMsg(w, cliCtx, sdkErr.Code, sdkErr.Message)
return
}
order2 := &types.Order{}
codec.Cdc.MustUnmarshalJSON(res, order2)
response := common.GetBaseResponse(order2)
resBytes, err2 := json.Marshal(response)
if err2 != nil {
common.HandleErrorMsg(w, cliCtx, common.CodeMarshalJSONFailed, err2.Error())
return
}
rest.PostProcessResponse(w, cliCtx, resBytes)
}
}
func orderBookHandler(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
product := r.URL.Query().Get("product")
sizeStr := r.URL.Query().Get("size")
// validate request
if product == "" {
common.HandleErrorMsg(w, cliCtx, types.CodeProductIsEmpty, "invalid params: product is required")
return
}
var size int
var err error
if sizeStr != "" {
size, err = strconv.Atoi(sizeStr)
if err != nil {
common.HandleErrorMsg(w, cliCtx, common.CodeStrconvFailed, err.Error())
return
}
}
if size < 0 {
common.HandleErrorMsg(w, cliCtx, types.CodeSizeIsInvalid, fmt.Sprintf("invalid param: size= %d", size))
return
}
params := keeper.NewQueryDepthBookParams(product, uint(size))
bz, err := cliCtx.Codec.MarshalJSON(params)
if err != nil {
common.HandleErrorMsg(w, cliCtx, common.CodeMarshalJSONFailed, err.Error())
return
}
res, _, err := cliCtx.QueryWithData("custom/order/depthbook", bz)
if err != nil {
sdkErr := common.ParseSDKError(err.Error())
common.HandleErrorMsg(w, cliCtx, sdkErr.Code, sdkErr.Message)
return
}
bookRes := &keeper.BookRes{}
codec.Cdc.MustUnmarshalJSON(res, bookRes)
response := common.GetBaseResponse(bookRes)
resBytes, err2 := json.Marshal(response)
if err2 != nil {
common.HandleErrorMsg(w, cliCtx, common.CodeMarshalJSONFailed, err2.Error())
return
}
rest.PostProcessResponse(w, cliCtx, resBytes)
}
}