-
Notifications
You must be signed in to change notification settings - Fork 210
/
rest.go
143 lines (114 loc) · 3.71 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
package rest
import (
"fmt"
"net/http"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"
"github.com/akash-network/node/x/market/query"
)
// RegisterRoutes registers all query routes
func RegisterRoutes(ctx client.Context, r *mux.Router, ns string) {
// Get all orders
r.HandleFunc(fmt.Sprintf("/%s/order/list", ns), listOrdersHandler(ctx, ns)).Methods("GET")
// Get single order info
r.HandleFunc(fmt.Sprintf("/%s/order/info", ns), getOrderHandler(ctx, ns)).Methods("GET")
// Get all bids
r.HandleFunc(fmt.Sprintf("/%s/bid/list", ns), listBidsHandler(ctx, ns)).Methods("GET")
// Get single bid info
r.HandleFunc(fmt.Sprintf("/%s/bid/info", ns), getBidHandler(ctx, ns)).Methods("GET")
// Get all leases
r.HandleFunc(fmt.Sprintf("/%s/lease/list", ns), listLeasesHandler(ctx, ns)).Methods("GET")
// Get single order info
r.HandleFunc(fmt.Sprintf("/%s/lease/info", ns), getLeaseHandler(ctx, ns)).Methods("GET")
}
func listOrdersHandler(ctx client.Context, ns string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ofilters, errMsg := OrderFiltersFromRequest(r)
if len(errMsg) != 0 {
rest.WriteErrorResponse(w, http.StatusBadRequest, errMsg)
return
}
res, err := query.NewRawClient(ctx, ns).Orders(ofilters)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, "Not Found")
return
}
rest.PostProcessResponse(w, ctx, res)
}
}
func listBidsHandler(ctx client.Context, ns string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
bfilters, errMsg := BidFiltersFromRequest(r)
if len(errMsg) != 0 {
rest.WriteErrorResponse(w, http.StatusBadRequest, errMsg)
return
}
res, err := query.NewRawClient(ctx, ns).Bids(bfilters)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, "Not Found")
return
}
rest.PostProcessResponse(w, ctx, res)
}
}
func listLeasesHandler(ctx client.Context, ns string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
lfilters, errMsg := LeaseFiltersFromRequest(r)
if len(errMsg) != 0 {
rest.WriteErrorResponse(w, http.StatusBadRequest, errMsg)
return
}
res, err := query.NewRawClient(ctx, ns).Leases(lfilters)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, "Not Found")
return
}
rest.PostProcessResponse(w, ctx, res)
}
}
func getOrderHandler(ctx client.Context, ns string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, errMsg := OrderIDFromRequest(r)
if len(errMsg) != 0 {
rest.WriteErrorResponse(w, http.StatusBadRequest, errMsg)
return
}
res, err := query.NewRawClient(ctx, ns).Order(id)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, "Not Found")
return
}
rest.PostProcessResponse(w, ctx, res)
}
}
func getBidHandler(ctx client.Context, ns string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, errMsg := BidIDFromRequest(r)
if len(errMsg) != 0 {
rest.WriteErrorResponse(w, http.StatusBadRequest, errMsg)
return
}
res, err := query.NewRawClient(ctx, ns).Bid(id)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, "Not Found")
return
}
rest.PostProcessResponse(w, ctx, res)
}
}
func getLeaseHandler(ctx client.Context, ns string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, errMsg := LeaseIDFromRequest(r)
if len(errMsg) != 0 {
rest.WriteErrorResponse(w, http.StatusBadRequest, errMsg)
return
}
res, err := query.NewRawClient(ctx, ns).Lease(id)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, "Not Found")
return
}
rest.PostProcessResponse(w, ctx, res)
}
}