package rest

import (
	"fmt"
	"net/http"

	"github.com/cosmos/cosmos-sdk/client"
	sdk "github.com/cosmos/cosmos-sdk/types"
	"github.com/gorilla/mux"

	"github.com/cosmos/cosmos-sdk/types/rest"

	"github.com/joinresistance/space-pussy/x/bandwidth/types"
)

func registerQueryRoutes(cliCtx client.Context, r *mux.Router) {
	r.HandleFunc(
		"/bandwidth/parameters",
		queryParamsHandlerFn(cliCtx)).Methods("GET")
	r.HandleFunc(
		"/bandwidth/load",
		networkLoadHandlerFn(cliCtx)).Methods("GET")
	r.HandleFunc(
		"/bandwidth/price",
		priceHandlerFn(cliCtx)).Methods("GET")
	r.HandleFunc(
		"/bandwidth/total",
		totalBandwidthHandlerFn(cliCtx)).Methods("GET")
	r.HandleFunc(
		"/bandwidth/neuron/{neuron}",
		neuronBandwidthHandlerFn(cliCtx)).Methods("GET")
}

func queryParamsHandlerFn(cliCtx client.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {

		route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParameters)

		res, height, err := cliCtx.QueryWithData(route, nil)
		if err != nil {
			rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
			return
		}

		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
		if !ok {
			return
		}

		cliCtx = cliCtx.WithHeight(height)
		rest.PostProcessResponse(w, cliCtx, res)
	}
}

func networkLoadHandlerFn(cliCtx client.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {

		route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryLoad)

		res, height, err := cliCtx.QueryWithData(route, nil)
		if err != nil {
			rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
			return
		}

		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
		if !ok {
			return
		}

		cliCtx = cliCtx.WithHeight(height)
		rest.PostProcessResponse(w, cliCtx, res)
	}
}

func priceHandlerFn(cliCtx client.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {

		route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryPrice)

		res, height, err := cliCtx.QueryWithData(route, nil)
		if err != nil {
			rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
			return
		}

		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
		if !ok {
			return
		}

		cliCtx = cliCtx.WithHeight(height)
		rest.PostProcessResponse(w, cliCtx, res)
	}
}

func totalBandwidthHandlerFn(cliCtx client.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {

		route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryDesirableBandwidth)

		res, height, err := cliCtx.QueryWithData(route, nil)
		if err != nil {
			rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
			return
		}

		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
		if !ok {
			return
		}

		cliCtx = cliCtx.WithHeight(height)
		rest.PostProcessResponse(w, cliCtx, res)
	}
}

func neuronBandwidthHandlerFn(cliCtx client.Context) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)

		addr, err := sdk.AccAddressFromBech32(vars["neuron"])
		if err != nil {
			rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
			return
		}

		params := types.NewQueryAccountBandwidthParams(addr)

		bz, err := cliCtx.LegacyAmino.MarshalJSON(params)
		if rest.CheckBadRequestError(w, err) {
			return
		}

		route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryAccount)
		res, height, err := cliCtx.QueryWithData(route, bz)
		if err != nil {
			rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
			return
		}

		cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
		if !ok {
			return
		}

		cliCtx = cliCtx.WithHeight(height)
		rest.PostProcessResponse(w, cliCtx, res)
	}
}

Synonyms

space-pussy/x/rank/client/cli/query.go
space-pussy/x/rank/client/rest/query.go
space-pussy/x/resources/client/rest/query.go
go-cyber/x/dmn/client/cli/query.go
space-pussy/x/graph/client/rest/query.go
space-pussy/x/bandwidth/client/cli/query.go
go-cyber/x/liquidity/client/cli/query.go
go-cyber/x/grid/client/cli/query.go
space-pussy/x/grid/client/cli/query.go
space-pussy/x/graph/client/cli/query.go
go-cyber/x/resources/client/cli/query.go
go-cyber/x/clock/client/cli/query.go
space-pussy/x/dmn/client/rest/query.go
space-pussy/x/dmn/client/cli/query.go
go-cyber/x/rank/client/cli/query.go
go-cyber/x/bandwidth/client/cli/query.go
go-cyber/x/tokenfactory/wasm/types/query.go
go-cyber/x/graph/client/cli/query.go
space-pussy/x/resources/client/cli/query.go
go-cyber/x/tokenfactory/client/cli/query.go
space-pussy/x/grid/client/rest/query.go

Neighbours