x/service/client/rest/query.go
package rest
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/gorilla/mux"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/mesg-foundation/engine/service"
"github.com/mesg-foundation/engine/x/service/internal/types"
)
func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc(
"/service/get/{hash}",
queryGetHandlerFn(cliCtx),
).Methods(http.MethodGet)
r.HandleFunc(
"/service/list",
queryListHandlerFn(cliCtx),
).Methods(http.MethodGet)
r.HandleFunc(
"/service/hash",
queryHashHandlerFn(cliCtx),
).Methods(http.MethodPost)
r.HandleFunc(
"/service/exist/{hash}",
queryExistHandlerFn(cliCtx),
).Methods(http.MethodGet)
}
func queryGetHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}
route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, types.QueryGet, vars["hash"])
res, height, err := cliCtx.QueryWithData(route, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}
func queryListHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryList)
res, height, err := cliCtx.QueryWithData(route, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}
// HashRequest is the request of the hash endpoint.
type HashRequest struct {
Sid string `json:"sid,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Configuration service.Service_Configuration `json:"configuration"`
Tasks []*service.Service_Task `json:"tasks,omitempty"`
Events []*service.Service_Event `json:"events,omitempty"`
Dependencies []*service.Service_Dependency `json:"dependencies,omitempty"`
Repository string `json:"repository,omitempty"`
Source string `json:"source,omitempty"`
}
func queryHashHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}
data, err := ioutil.ReadAll(r.Body)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
var req HashRequest
if err := cliCtx.Codec.UnmarshalJSON(data, &req); err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
srv, err := service.New(
req.Sid,
req.Name,
req.Description,
req.Configuration,
req.Tasks,
req.Events,
req.Dependencies,
req.Repository,
req.Source,
)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
rest.PostProcessResponse(w, cliCtx, srv.Hash.String())
}
}
func queryExistHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}
route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, types.QueryExist, vars["hash"])
res, height, err := cliCtx.QueryWithData(route, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}