Documentation ¶
Overview ¶
Package rest provides HTTP types and primitives for REST requests validation and responses handling.
Index ¶
- Constants
- func CheckBadRequestError(w http.ResponseWriter, err error) bool
- func CheckError(w http.ResponseWriter, status int, err error) bool
- func CheckInternalServerError(w http.ResponseWriter, err error) bool
- func CheckNotFoundError(w http.ResponseWriter, err error) bool
- func GetRequest(url string) ([]byte, error)
- func ParseFloat64OrReturnBadRequest(w http.ResponseWriter, s string, defaultIfEmpty float64) (n float64, ok bool)
- func ParseHTTPArgs(r *http.Request) (tags []string, page, limit int, err error)
- func ParseHTTPArgsWithLimit(r *http.Request, defaultLimit int) (tags []string, page, limit int, err error)
- func ParseQueryHeightOrReturnBadRequest(w http.ResponseWriter, clientCtx client.Context, r *http.Request) (client.Context, bool)
- func ParseQueryParamBool(r *http.Request, param string) bool
- func ParseResponseWithHeight(cdc *codec.LegacyAmino, bz []byte) ([]byte, error)
- func ParseUint64OrReturnBadRequest(w http.ResponseWriter, s string) (n uint64, ok bool)
- func PostProcessResponse(w http.ResponseWriter, ctx client.Context, resp interface{})
- func PostProcessResponseBare(w http.ResponseWriter, ctx client.Context, body interface{})
- func PostRequest(url string, contentType string, data []byte) ([]byte, error)
- func ReadRESTReq(w http.ResponseWriter, r *http.Request, cdc *codec.LegacyAmino, ...) bool
- func WriteErrorResponse(w http.ResponseWriter, status int, err string)
- func WriteSimulationResponse(w http.ResponseWriter, cdc *codec.LegacyAmino, gas uint64)
- type BaseReq
- type ErrorResponse
- type GasEstimateResponse
- type ResponseWithHeight
Constants ¶
const ( DefaultPage = 1 DefaultLimit = 30 // should be consistent with tendermint/tendermint/rpc/core/pipe.go:19 TxMinHeightKey = "tx.minheight" // Inclusive minimum height filter TxMaxHeightKey = "tx.maxheight" // Inclusive maximum height filter )
Variables ¶
This section is empty.
Functions ¶
func CheckBadRequestError ¶ added in v0.43.1
func CheckBadRequestError(w http.ResponseWriter, err error) bool
CheckBadRequestError attaches an error message to an HTTP 400 BAD REQUEST response. Returns false when err is nil; it returns true otherwise.
func CheckError ¶ added in v0.43.1
func CheckError(w http.ResponseWriter, status int, err error) bool
CheckError takes care of writing an error response if err is not nil. Returns false when err is nil; it returns true otherwise.
func CheckInternalServerError ¶ added in v0.43.1
func CheckInternalServerError(w http.ResponseWriter, err error) bool
CheckInternalServerError attaches an error message to an HTTP 500 INTERNAL SERVER ERROR response. Returns false when err is nil; it returns true otherwise.
func CheckNotFoundError ¶ added in v0.43.1
func CheckNotFoundError(w http.ResponseWriter, err error) bool
CheckNotFoundError attaches an error message to an HTTP 404 NOT FOUND response. Returns false when err is nil; it returns true otherwise.
func GetRequest ¶ added in v0.43.1
GetRequest defines a wrapper around an HTTP GET request with a provided URL. An error is returned if the request or reading the body fails.
func ParseFloat64OrReturnBadRequest ¶
func ParseFloat64OrReturnBadRequest(w http.ResponseWriter, s string, defaultIfEmpty float64) (n float64, ok bool)
ParseFloat64OrReturnBadRequest converts s to a float64 value. It returns a default value, defaultIfEmpty, if the string is empty.
func ParseHTTPArgs ¶ added in v0.43.1
ParseHTTPArgs parses the request's URL and returns a slice containing all arguments pairs. It separates page and limit used for pagination.
func ParseHTTPArgsWithLimit ¶ added in v0.43.1
func ParseHTTPArgsWithLimit(r *http.Request, defaultLimit int) (tags []string, page, limit int, err error)
ParseHTTPArgsWithLimit parses the request's URL and returns a slice containing all arguments pairs. It separates page and limit used for pagination where a default limit can be provided.
func ParseQueryHeightOrReturnBadRequest ¶ added in v0.43.1
func ParseQueryHeightOrReturnBadRequest(w http.ResponseWriter, clientCtx client.Context, r *http.Request) (client.Context, bool)
ParseQueryHeightOrReturnBadRequest sets the height to execute a query if set by the http request. It returns false if there was an error parsing the height.
func ParseQueryParamBool ¶ added in v0.43.1
ParseQueryParamBool parses the given param to a boolean. It returns false by default if the string is not parseable to bool.
func ParseResponseWithHeight ¶ added in v0.43.1
func ParseResponseWithHeight(cdc *codec.LegacyAmino, bz []byte) ([]byte, error)
ParseResponseWithHeight returns the raw result from a JSON-encoded ResponseWithHeight object.
func ParseUint64OrReturnBadRequest ¶
func ParseUint64OrReturnBadRequest(w http.ResponseWriter, s string) (n uint64, ok bool)
ParseUint64OrReturnBadRequest converts s to a uint64 value.
func PostProcessResponse ¶
func PostProcessResponse(w http.ResponseWriter, ctx client.Context, resp interface{})
PostProcessResponse performs post processing for a REST response. The result returned to clients will contain two fields, the height at which the resource was queried at and the original result.
func PostProcessResponseBare ¶ added in v0.43.1
func PostProcessResponseBare(w http.ResponseWriter, ctx client.Context, body interface{})
PostProcessResponseBare post processes a body similar to PostProcessResponse except it does not wrap the body and inject the height.
func PostRequest ¶ added in v0.43.1
PostRequest defines a wrapper around an HTTP POST request with a provided URL and data. An error is returned if the request or reading the body fails.
func ReadRESTReq ¶
func ReadRESTReq(w http.ResponseWriter, r *http.Request, cdc *codec.LegacyAmino, req interface{}) bool
ReadRESTReq reads and unmarshals a Request's body to the the BaseReq struct. Writes an error response to ResponseWriter and returns true if errors occurred.
func WriteErrorResponse ¶
func WriteErrorResponse(w http.ResponseWriter, status int, err string)
WriteErrorResponse prepares and writes a HTTP error given a status code and an error message.
func WriteSimulationResponse ¶
func WriteSimulationResponse(w http.ResponseWriter, cdc *codec.LegacyAmino, gas uint64)
WriteSimulationResponse prepares and writes an HTTP response for transactions simulations.
Types ¶
type BaseReq ¶
type BaseReq struct { From string `json:"from"` Memo string `json:"memo"` ChainID string `json:"chain_id"` AccountNumber uint64 `json:"account_number"` Sequence uint64 `json:"sequence"` TimeoutHeight uint64 `json:"timeout_height"` Fees sdk.Coins `json:"fees"` GasPrices sdk.DecCoins `json:"gas_prices"` Gas string `json:"gas"` GasAdjustment string `json:"gas_adjustment"` Simulate bool `json:"simulate"` }
BaseReq defines a structure that can be embedded in other request structures that all share common "base" fields.
func NewBaseReq ¶
func NewBaseReq( from, memo, chainID string, gas, gasAdjustment string, accNumber, seq uint64, fees sdk.Coins, gasPrices sdk.DecCoins, simulate bool, ) BaseReq
NewBaseReq creates a new basic request instance and sanitizes its values
func (BaseReq) ValidateBasic ¶
func (br BaseReq) ValidateBasic(w http.ResponseWriter) bool
ValidateBasic performs basic validation of a BaseReq. If custom validation logic is needed, the implementing request handler should perform those checks manually.
type ErrorResponse ¶
ErrorResponse defines the attributes of a JSON error response.
func NewErrorResponse ¶
func NewErrorResponse(code int, err string) ErrorResponse
NewErrorResponse creates a new ErrorResponse instance.
type GasEstimateResponse ¶
type GasEstimateResponse struct {
GasEstimate uint64 `json:"gas_estimate"`
}
GasEstimateResponse defines a response definition for tx gas estimation.
type ResponseWithHeight ¶ added in v0.43.1
type ResponseWithHeight struct { Height int64 `json:"height"` Result json.RawMessage `json:"result"` }
ResponseWithHeight defines a response object type that wraps an original response with a height.
func NewResponseWithHeight ¶ added in v0.43.1
func NewResponseWithHeight(height int64, result json.RawMessage) ResponseWithHeight
NewResponseWithHeight creates a new ResponseWithHeight instance