Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement pagination for x/wasm query #85

Merged
merged 13 commits into from
Mar 15, 2021
25 changes: 24 additions & 1 deletion x/wasm/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
sdk "github.com/line/lbm-sdk/types"
"github.com/line/lbm-sdk/types/rest"

"github.com/line/lbm-sdk/x/wasm/client/utils"
"github.com/line/lbm-sdk/x/wasm/internal/keeper"
"github.com/line/lbm-sdk/x/wasm/internal/types"
)
Expand Down Expand Up @@ -238,7 +239,29 @@ func queryContractHistoryFn(cliCtx context.CLIContext) http.HandlerFunc {
}

route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, keeper.QueryContractHistory, addr.String())
res, height, err := cliCtx.Query(route)
pageKey, offset, limit, page, countTotal, err := utils.ParseHTTPArgs(r)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

pageReq, err := utils.NewPageRequest(pageKey, offset, limit, page, countTotal)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

data := &types.QueryContractHistoryRequest{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Address: addr,
Pagination: pageReq,
}
bs, err := cliCtx.Codec.MarshalJSON(data)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

res, height, err := cliCtx.QueryWithData(route, bs)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down
53 changes: 53 additions & 0 deletions x/wasm/client/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import (
"bytes"
"compress/gzip"
"encoding/binary"
"errors"
"fmt"
"net/http"
"strconv"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

sdkerrors "github.com/line/lbm-sdk/types/errors"
"github.com/line/lbm-sdk/types/rest"
"github.com/line/lbm-sdk/x/wasm/internal/types"
)

Expand Down Expand Up @@ -118,3 +122,52 @@ func NewPageRequest(pageKey string, offset, limit, page uint64, countTotal bool)
CountTotal: countTotal,
}, nil
}

func ParseHTTPArgs(r *http.Request) (pageKey string, offset, limit, page uint64, countTotal bool, err error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pagination utility for HTTP args does already exist. Can we use it for API consistency?

https://github.com/line/lbm-sdk/blob/develop/types/rest/rest.go#L382

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The limit and page could be parseed from the already existing ParseHTTPArgs.

pageKey = r.FormValue("page-key")

offsetStr := r.FormValue("offset")
if offsetStr != "" {
offset, err = strconv.ParseUint(offsetStr, 10, 64)
if err != nil {
return pageKey, offset, limit, page, countTotal, err
}
if offset <= 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other than here, used else if

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return pageKey, offset, limit, page, countTotal, errors.New("offset must greater than 0")
}
}

pageStr := r.FormValue("page")
if pageStr == "" {
page = rest.DefaultPage
} else {
page, err = strconv.ParseUint(pageStr, 10, 64)
if err != nil {
return pageKey, offset, limit, page, countTotal, err
} else if page <= 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comment. As the previous doc says, the following suggestion is better.

Suggested change
} else if page <= 0 {
}
if page <= 0 {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: does page index start from 1, not 0?

return pageKey, offset, limit, page, countTotal, errors.New("page must greater than 0")
}
}

limitStr := r.FormValue("limit")
if limitStr == "" {
limit = rest.DefaultLimit
} else {
limit, err = strconv.ParseUint(limitStr, 10, 64)
if err != nil {
return pageKey, offset, limit, page, countTotal, err
} else if limit <= 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else if limit <= 0 {
}
if limit <= 0 {

ditto

return pageKey, offset, limit, page, countTotal, errors.New("limit must greater than 0")
}
}

countTotalStr := r.FormValue("count-total")
if countTotalStr != "" {
countTotal, err = strconv.ParseBool(countTotalStr)
if err != nil {
return pageKey, offset, limit, page, countTotal, err
}
}

return pageKey, offset, limit, page, countTotal, nil
}