forked from akash-network/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.go
86 lines (72 loc) · 2.51 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
package rest
import (
"fmt"
"net/http"
"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"
"github.com/akash-network/node/x/audit/query"
types "github.com/akash-network/node/x/audit/types/v1beta2"
)
// RegisterRoutes registers all query routes
func RegisterRoutes(ctx client.Context, r *mux.Router, ns string) {
prefix := fmt.Sprintf("/%s/attributes", ns)
// Get all signed
r.HandleFunc(fmt.Sprintf("/%s/list", prefix), listAllSignedHandler(ctx, ns)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/owner/{providerOwner}/list", prefix), listProviderAttributes(ctx, ns)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/auditor/{auditor}/{providerOwner}", prefix), listAuditorProviderAttributes(ctx, ns)).Methods("GET")
}
func listAllSignedHandler(ctx client.Context, ns string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res, err := query.NewRawClient(ctx, ns).AllProviders()
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, "Not Found")
return
}
rest.PostProcessResponse(w, ctx, res)
}
}
func listProviderAttributes(ctx client.Context, ns string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, err := sdk.AccAddressFromBech32(mux.Vars(r)["providerOwner"])
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, "Invalid address")
return
}
res, err := query.NewRawClient(ctx, ns).Provider(id)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, "Not Found")
return
}
rest.PostProcessResponse(w, ctx, res)
}
}
func listAuditorProviderAttributes(ctx client.Context, ns string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
auditor, err := sdk.AccAddressFromBech32(mux.Vars(r)["auditor"])
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, "Invalid address")
return
}
var res []byte
if addr := mux.Vars(r)["providerOwner"]; addr == "list" {
res, err = query.NewRawClient(ctx, ns).Auditor(auditor)
} else {
var owner sdk.AccAddress
if owner, err = sdk.AccAddressFromBech32(addr); err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, "Invalid address")
return
}
res, err = query.NewRawClient(ctx, ns).ProviderID(types.ProviderID{
Owner: owner,
Auditor: auditor,
})
}
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, "Not Found")
return
}
rest.PostProcessResponse(w, ctx, res)
}
}