Skip to content

Commit

Permalink
feat(rest): defining base req and place holder rest place holder methods
Browse files Browse the repository at this point in the history
Signed-off-by: Deepanshu Tripathi <[email protected]>
  • Loading branch information
deepanshutr committed Jun 20, 2024
1 parent b349ab7 commit 9d445f3
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 5 deletions.
3 changes: 2 additions & 1 deletion helpers/base/cli_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ package base

import (
"fmt"
"github.com/AssetMantle/modules/utilities/rest"

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

"github.com/spf13/cobra"

"github.com/AssetMantle/modules/helpers"
Expand Down
3 changes: 2 additions & 1 deletion helpers/base/cli_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
package base

import (
"github.com/AssetMantle/modules/utilities/rest"
"reflect"
"testing"

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

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down
3 changes: 2 additions & 1 deletion helpers/cli_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
package helpers

import (
"github.com/AssetMantle/modules/utilities/rest"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/types/rest"

"github.com/spf13/cobra"
)

Expand Down
11 changes: 11 additions & 0 deletions utilities/client/methods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package clientUtils

import (
"github.com/cosmos/cosmos-sdk/client"
"net/http"
)

func ParseQueryHeightOrReturnBadRequest(responseWriter http.ResponseWriter, clientContext client.Context, request *http.Request) (client.Context, bool) {
// TODO correct
return clientContext, true
}
48 changes: 48 additions & 0 deletions utilities/rest/baseReq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package rest

import (
"github.com/cosmos/cosmos-sdk/types"
"net/http"
)

type BaseReq struct {
From string `json:"from"`
Memo string `json:"memo"`
TimeoutHeight uint64 `json:"timeout_height"`
AccountNumber uint64 `json:"account_number"`
Sequence uint64 `json:"sequence"`
ChainID string `json:"chain_id"`
Gas string `json:"gas"`
Fees types.Coins `json:"fees"`
GasPrices types.DecCoins `json:"gas_prices"`
Simulate bool `json:"simulate"`
GasAdjustment string `json:"gas_adjustment"`
}

func (BaseReq) Validate() error {
return nil
}

func (BaseReq) Sanitize() BaseReq {
return BaseReq{}
}

func (BaseReq) ValidateBasic(w http.ResponseWriter) bool {
return false
}

func NewBaseReq(from, memo, chainID, gas, gasAdjustment string, timeoutHeight, accountNumber, sequence uint64, fees types.Coins, gasPrices types.DecCoins, simulate bool) BaseReq {
return BaseReq{
From: from,
Memo: memo,
TimeoutHeight: timeoutHeight,
AccountNumber: accountNumber,
Sequence: sequence,
ChainID: chainID,
Gas: gas,
Fees: fees,
GasPrices: gasPrices,
Simulate: simulate,
GasAdjustment: gasAdjustment,
}
}
5 changes: 5 additions & 0 deletions utilities/rest/gas_estimation_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package rest

type GasEstimateResponse struct {
GasEstimate uint64 `json:"gas_estimate"`
}
63 changes: 63 additions & 0 deletions utilities/rest/methods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package rest

import (
"encoding/json"
clientUtils "github.com/AssetMantle/modules/utilities/client"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"io"
"net/http"
)

func PostProcessResponse(responseWriter http.ResponseWriter, context client.Context, response interface{}) {
responseWriter.Header().Set("Content-Type", "application/json")
responseWriter.WriteHeader(http.StatusOK)

_ = json.NewEncoder(responseWriter).Encode(response)
}

func ReadRESTReq(responseWriter http.ResponseWriter, httpRequest *http.Request, legacyAmino *codec.LegacyAmino, request interface{}) bool {
body, err := io.ReadAll(httpRequest.Body)
if err != nil {
responseWriter.WriteHeader(http.StatusBadRequest)
_, _ = responseWriter.Write([]byte(err.Error()))
return false
}
if err := legacyAmino.UnmarshalJSON(body, request); err != nil {
responseWriter.WriteHeader(http.StatusBadRequest)
_, _ = responseWriter.Write([]byte(err.Error()))
return false
}
return true
}

func CheckBadRequestError(responseWriter http.ResponseWriter, err error) bool {
if err != nil {
responseWriter.WriteHeader(http.StatusBadRequest)
_, _ = responseWriter.Write([]byte(err.Error()))
return true
}
return false
}

func ParseQueryHeightOrReturnBadRequest(responseWriter http.ResponseWriter, clientContext client.Context, request *http.Request) (client.Context, bool) {
clientContext, ok := clientUtils.ParseQueryHeightOrReturnBadRequest(responseWriter, clientContext, request)
if !ok {
return clientContext, false
}
return clientContext, true
}

func CheckInternalServerError(responseWriter http.ResponseWriter, err error) bool {
if err != nil {
responseWriter.WriteHeader(http.StatusInternalServerError)
_, _ = responseWriter.Write([]byte(err.Error()))
return true
}
return false
}

func WriteErrorResponse(responseWriter http.ResponseWriter, statusCode int, message string) {
responseWriter.WriteHeader(statusCode)
_, _ = responseWriter.Write([]byte(message))
}
2 changes: 1 addition & 1 deletion utilities/rest/queuing/broadcast.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package queuing

Check failure on line 1 in utilities/rest/queuing/broadcast.go

View workflow job for this annotation

GitHub Actions / golangci-lint

: import cycle not allowed in test (typecheck)

import (
"github.com/AssetMantle/modules/utilities/rest"
"reflect"
"strconv"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdkTypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"

"github.com/AssetMantle/modules/utilities/random"
)
Expand Down
3 changes: 2 additions & 1 deletion utilities/rest/queuing/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
package queuing

import (
"github.com/AssetMantle/modules/utilities/rest"
"strconv"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/rest"

"github.com/pkg/errors"
)

Expand Down

0 comments on commit 9d445f3

Please sign in to comment.