Skip to content

Commit ad5c46f

Browse files
feat: add endpoint with pagination and pagInfo object (#10)
This endpoint powers test cases that have to use JSONPath to drill deeper than one to get at pagination outputs.
1 parent 8632ebb commit ad5c46f

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

cmd/server/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func main() {
3434
r.HandleFunc("/requestbody", requestbody.HandleRequestBody).Methods(http.MethodPost)
3535
r.HandleFunc("/vendorjson", responseHeaders.HandleVendorJsonResponseHeaders).Methods(http.MethodGet)
3636
r.HandleFunc("/pagination/limitoffset/page", pagination.HandleLimitOffsetPage).Methods(http.MethodGet, http.MethodPut)
37+
r.HandleFunc("/pagination/limitoffset/deep_outputs/page", pagination.HandleLimitOffsetDeepOutputsPage).Methods(http.MethodGet, http.MethodPut)
3738
r.HandleFunc("/pagination/limitoffset/offset", pagination.HandleLimitOffsetOffset).Methods(http.MethodGet, http.MethodPut)
3839
r.HandleFunc("/pagination/cursor", pagination.HandleCursor).Methods(http.MethodGet, http.MethodPut)
3940
r.HandleFunc("/pagination/url", pagination.HandleURL).Methods(http.MethodGet)

internal/pagination/service.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ type PaginationResponse struct {
2828
Next *string `json:"next,omitempty"`
2929
}
3030

31+
type PageInfo struct {
32+
NumPages int `json:"numPages"`
33+
Next *string `json:"next,omitempty"`
34+
}
35+
type PaginationResponseDeep struct {
36+
ResultArray []interface{} `json:"resultArray"`
37+
PageInfo PageInfo `json:"pageInfo"`
38+
}
39+
3140
// Insecure reversable hashing for string cursors
3241
func hash(s string) (int, error) {
3342
return strconv.Atoi(s)
@@ -197,6 +206,41 @@ func HandleNonNumericCursor(w http.ResponseWriter, r *http.Request) {
197206
}
198207
}
199208

209+
func HandleLimitOffsetDeepOutputsPage(w http.ResponseWriter, r *http.Request) {
210+
queryLimit := r.FormValue("limit")
211+
queryPage := r.FormValue("page")
212+
213+
var pagination LimitOffsetRequest
214+
hasBody := true
215+
if err := json.NewDecoder(r.Body).Decode(&pagination); err != nil {
216+
hasBody = false
217+
}
218+
limit := getValue(queryLimit, hasBody, pagination.Limit)
219+
if limit == 0 {
220+
limit = 20
221+
}
222+
page := getValue(queryPage, hasBody, pagination.Page)
223+
224+
start := (page - 1) * limit
225+
226+
res := PaginationResponseDeep{
227+
PageInfo: PageInfo{
228+
NumPages: int(math.Ceil(float64(total) / float64(limit))),
229+
},
230+
ResultArray: make([]interface{}, 0),
231+
}
232+
233+
for i := start; i < total && len(res.ResultArray) < limit; i++ {
234+
res.ResultArray = append(res.ResultArray, i)
235+
}
236+
237+
w.Header().Set("Content-Type", "application/json")
238+
err := json.NewEncoder(w).Encode(res)
239+
if err != nil {
240+
w.WriteHeader(500)
241+
}
242+
}
243+
200244
func getValue(queryValue string, hasBody bool, paginationValue int) int {
201245
if hasBody {
202246
return paginationValue

0 commit comments

Comments
 (0)