mesg-foundation/core

View on GitHub
x/ownership/client/rest/query.go

Summary

Maintainability
A
1 hr
Test Coverage
package rest

import (
    "fmt"
    "net/http"

    "github.com/cosmos/cosmos-sdk/client/context"
    "github.com/cosmos/cosmos-sdk/types/rest"
    "github.com/gorilla/mux"
    "github.com/mesg-foundation/engine/x/ownership/internal/types"
)

func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
    r.HandleFunc(
        "/ownership/list",
        queryListHandlerFn(cliCtx),
    ).Methods(http.MethodGet)
}

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)
    }
}