mesg-foundation/core

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

Summary

Maintainability
B
4 hrs
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/credit/internal/types"
)

func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
    r.HandleFunc(
        "/credit/get/{address}",
        queryGetHandlerFn(cliCtx),
    ).Methods(http.MethodGet)

    r.HandleFunc(
        "/execution/parameters",
        queryParamsHandlerFn(cliCtx),
    ).Methods("GET")
}

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["address"])

        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 queryParamsHandlerFn(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.QueryParameters)

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