Skip to content

Commit

Permalink
new: support for lazy pagination (#53)
Browse files Browse the repository at this point in the history
* new: added support for after param

* new: added additional validations

* added limit
  • Loading branch information
primalmotion authored Sep 25, 2019
1 parent efe3956 commit df0f9dc
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 277 deletions.
34 changes: 33 additions & 1 deletion request.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type Request struct {
Password string
Page int
PageSize int
After string
Limit int
OverrideProtection bool
Version int
ExternalTrackingID string
Expand Down Expand Up @@ -163,8 +165,9 @@ func NewRequestFromHTTPRequest(req *http.Request, manager ModelManager) (*Reques
}
}

var page, pageSize int
var page, pageSize, limit int
var recursive, override bool
var after string
var order []string

q := req.URL.Query()
Expand Down Expand Up @@ -204,6 +207,31 @@ func NewRequestFromHTTPRequest(req *http.Request, manager ModelManager) (*Reques
q.Del("order")
}

if v := q.Get("limit"); v != "" {
limit, err = strconv.Atoi(v)
if pageSize != 0 {
return nil, NewError("Bad Request", "You cannot set 'limit' and 'pagesize' at the same time", "elemental", http.StatusBadRequest)
}
if err != nil {
return nil, NewError("Bad Request", "Parameter `limit` must be an integer", "elemental", http.StatusBadRequest)
}
q.Del("limit")
}

if v := q.Get("after"); v != "" {
if v == "" || v == "\u0000" {
return nil, NewError("Bad Request", "Parameter `after` must be set when provided", "elemental", http.StatusBadRequest)
}
if len(order) > 1 {
return nil, NewError("Bad Request", "You can only order on a single field when using 'after'", "elemental", http.StatusBadRequest)
}
if page != 0 {
return nil, NewError("Bad Request", "You cannot set 'after' and 'page' at the same time", "elemental", http.StatusBadRequest)
}
after = v
q.Del("after")
}

paramsMap := Parameters{}
qKeys := map[string]struct{}{}
for k := range q {
Expand Down Expand Up @@ -254,6 +282,8 @@ func NewRequestFromHTTPRequest(req *http.Request, manager ModelManager) (*Reques
Recursive: recursive,
Page: page,
PageSize: pageSize,
After: after,
Limit: limit,
Operation: operation,
Identity: identity,
ObjectID: ID,
Expand Down Expand Up @@ -287,6 +317,8 @@ func (r *Request) Duplicate() *Request {
req.Recursive = r.Recursive
req.Page = r.Page
req.PageSize = r.PageSize
req.After = r.After
req.Limit = r.Limit
req.Operation = r.Operation
req.Identity = r.Identity
req.ObjectID = r.ObjectID
Expand Down
Loading

0 comments on commit df0f9dc

Please sign in to comment.