github.com/Finschia/finschia-sdk@v0.48.1/client/utils.go (about)

     1  package client
     2  
     3  import (
     4  	rpchttp "github.com/Finschia/ostracon/rpc/client/http"
     5  	"github.com/spf13/pflag"
     6  
     7  	"github.com/Finschia/finschia-sdk/client/flags"
     8  	sdkerrors "github.com/Finschia/finschia-sdk/types/errors"
     9  	"github.com/Finschia/finschia-sdk/types/query"
    10  )
    11  
    12  // Paginate returns the correct starting and ending index for a paginated query,
    13  // given that client provides a desired page and limit of objects and the handler
    14  // provides the total number of objects. The start page is assumed to be 1-indexed.
    15  // If the start page is invalid, non-positive values are returned signaling the
    16  // request is invalid; it returns non-positive values if limit is non-positive and
    17  // defLimit is negative.
    18  func Paginate(numObjs, page, limit, defLimit int) (start, end int) {
    19  	if page <= 0 {
    20  		// invalid start page
    21  		return -1, -1
    22  	}
    23  
    24  	// fallback to default limit if supplied limit is invalid
    25  	if limit <= 0 {
    26  		if defLimit < 0 {
    27  			// invalid default limit
    28  			return -1, -1
    29  		}
    30  		limit = defLimit
    31  	}
    32  
    33  	start = (page - 1) * limit
    34  	end = limit + start
    35  
    36  	if end >= numObjs {
    37  		end = numObjs
    38  	}
    39  
    40  	if start >= numObjs {
    41  		// page is out of bounds
    42  		return -1, -1
    43  	}
    44  
    45  	return start, end
    46  }
    47  
    48  // ReadPageRequest reads and builds the necessary page request flags for pagination.
    49  func ReadPageRequest(flagSet *pflag.FlagSet) (*query.PageRequest, error) {
    50  	pageKey, _ := flagSet.GetString(flags.FlagPageKey)
    51  	offset, _ := flagSet.GetUint64(flags.FlagOffset)
    52  	limit, _ := flagSet.GetUint64(flags.FlagLimit)
    53  	countTotal, _ := flagSet.GetBool(flags.FlagCountTotal)
    54  	page, _ := flagSet.GetUint64(flags.FlagPage)
    55  	reverse, _ := flagSet.GetBool(flags.FlagReverse)
    56  
    57  	return NewPageRequest(pageKey, offset, limit, page, countTotal, reverse)
    58  }
    59  
    60  func NewPageRequest(pageKey string, offset, limit, page uint64, countTotal bool, reverse bool) (*query.PageRequest, error) {
    61  	if page > 1 && offset > 0 {
    62  		return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "page and offset cannot be used together")
    63  	}
    64  
    65  	if page > 1 {
    66  		offset = (page - 1) * limit
    67  	}
    68  
    69  	return &query.PageRequest{
    70  		Key:        []byte(pageKey),
    71  		Offset:     offset,
    72  		Limit:      limit,
    73  		CountTotal: countTotal,
    74  		Reverse:    reverse,
    75  	}, nil
    76  }
    77  
    78  // NewClientFromNode sets up Client implementation that communicates with a Tendermint node over
    79  // JSON RPC and WebSockets
    80  // TODO: We might not need to manually append `/websocket`:
    81  // https://github.com/cosmos/cosmos-sdk/issues/8986
    82  func NewClientFromNode(nodeURI string) (*rpchttp.HTTP, error) {
    83  	return rpchttp.New(nodeURI, "/websocket")
    84  }