-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rest): defining base req and place holder rest place holder methods
Signed-off-by: Deepanshu Tripathi <[email protected]>
- Loading branch information
1 parent
b349ab7
commit 9d445f3
Showing
9 changed files
with
136 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package rest | ||
|
||
type GasEstimateResponse struct { | ||
GasEstimate uint64 `json:"gas_estimate"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters