mesg-foundation/core

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

Summary

Maintainability
C
7 hrs
Test Coverage
package rest

import (
    "fmt"
    "io/ioutil"
    "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/hash"
    "github.com/mesg-foundation/engine/instance"
    "github.com/mesg-foundation/engine/runner"
    "github.com/mesg-foundation/engine/x/runner/internal/types"
)

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

    r.HandleFunc(
        "/runner/list",
        queryListHandlerFn(cliCtx),
    ).Methods(http.MethodGet)

    r.HandleFunc(
        "/runner/hash",
        queryHashHandlerFn(cliCtx),
    ).Methods(http.MethodPost)

    r.HandleFunc(
        "/runner/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 {
    ServiceHash hash.Hash `json:"serviceHash"`
    Env         []string  `json:"env"`
    Address     string    `json:"address"`
}

// HashResponse is the response of the hash endpoint.
type HashResponse struct {
    RunnerHash   hash.Hash `json:"runnerHash"`
    InstanceHash hash.Hash `json:"instanceHash"`
    EnvHash      hash.Hash `json:"envHash"`
}

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
        }

        envHash := hash.Dump(req.Env)
        inst, err := instance.New(req.ServiceHash, envHash)
        if err != nil {
            rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
            return
        }

        run, err := runner.New(req.Address, inst.Hash)
        if err != nil {
            rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
            return
        }

        res := HashResponse{
            RunnerHash:   run.Hash,
            InstanceHash: inst.Hash,
            EnvHash:      envHash,
        }

        rest.PostProcessResponse(w, cliCtx, res)
    }
}

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