-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): Add configurable limit on paginated request.
This add the query parameter 'limit' on both GET /transactions and GET /accounts endpoints. The parameter is capped to a value of 1000. The default value still the same as before (15). The PR also fix some tests around pagination which was working but was not doing the right thing (see api/controllers/pagination_test.go line 209).
- Loading branch information
Showing
12 changed files
with
325 additions
and
31 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,45 @@ | ||
package controllers | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/numary/ledger/pkg/ledger" | ||
"github.com/numary/ledger/pkg/storage" | ||
) | ||
|
||
const ( | ||
MaxLimit = 1000 | ||
DefaultLimit = storage.QueryDefaultLimit | ||
) | ||
|
||
var ( | ||
ErrInvalidLimit = ledger.NewValidationError("invalid query value 'limit'") | ||
ErrNegativeLimit = ledger.NewValidationError("cannot pass negative 'limit'") | ||
) | ||
|
||
func getLimit(c *gin.Context) (uint, error) { | ||
var ( | ||
// Use int instead of uint because if the client pass a negative limit | ||
// the uint will transparently convert the value to a valid uint | ||
// and the error will not be catched | ||
limit int64 | ||
err error | ||
) | ||
if limitParam := c.Query("limit"); limitParam != "" { | ||
limit, err = strconv.ParseInt(limitParam, 10, 64) | ||
if err != nil { | ||
return 0, ErrInvalidLimit | ||
} | ||
if limit < 0 { | ||
return 0, ErrNegativeLimit | ||
} | ||
|
||
if limit > MaxLimit { | ||
limit = MaxLimit | ||
} | ||
} else { | ||
limit = DefaultLimit | ||
} | ||
return uint(limit), nil | ||
} |
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
Oops, something went wrong.