Skip to content

Commit

Permalink
feat: add endpoint with pagination and pagInfo object (#10)
Browse files Browse the repository at this point in the history
This endpoint powers test cases that have to use JSONPath to drill
deeper than one to get at pagination outputs.
  • Loading branch information
disintegrator authored Jul 11, 2024
1 parent 8632ebb commit ad5c46f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func main() {
r.HandleFunc("/requestbody", requestbody.HandleRequestBody).Methods(http.MethodPost)
r.HandleFunc("/vendorjson", responseHeaders.HandleVendorJsonResponseHeaders).Methods(http.MethodGet)
r.HandleFunc("/pagination/limitoffset/page", pagination.HandleLimitOffsetPage).Methods(http.MethodGet, http.MethodPut)
r.HandleFunc("/pagination/limitoffset/deep_outputs/page", pagination.HandleLimitOffsetDeepOutputsPage).Methods(http.MethodGet, http.MethodPut)
r.HandleFunc("/pagination/limitoffset/offset", pagination.HandleLimitOffsetOffset).Methods(http.MethodGet, http.MethodPut)
r.HandleFunc("/pagination/cursor", pagination.HandleCursor).Methods(http.MethodGet, http.MethodPut)
r.HandleFunc("/pagination/url", pagination.HandleURL).Methods(http.MethodGet)
Expand Down
44 changes: 44 additions & 0 deletions internal/pagination/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ type PaginationResponse struct {
Next *string `json:"next,omitempty"`
}

type PageInfo struct {
NumPages int `json:"numPages"`
Next *string `json:"next,omitempty"`
}
type PaginationResponseDeep struct {
ResultArray []interface{} `json:"resultArray"`
PageInfo PageInfo `json:"pageInfo"`
}

// Insecure reversable hashing for string cursors
func hash(s string) (int, error) {
return strconv.Atoi(s)
Expand Down Expand Up @@ -197,6 +206,41 @@ func HandleNonNumericCursor(w http.ResponseWriter, r *http.Request) {
}
}

func HandleLimitOffsetDeepOutputsPage(w http.ResponseWriter, r *http.Request) {
queryLimit := r.FormValue("limit")
queryPage := r.FormValue("page")

var pagination LimitOffsetRequest
hasBody := true
if err := json.NewDecoder(r.Body).Decode(&pagination); err != nil {
hasBody = false
}
limit := getValue(queryLimit, hasBody, pagination.Limit)
if limit == 0 {
limit = 20
}
page := getValue(queryPage, hasBody, pagination.Page)

start := (page - 1) * limit

res := PaginationResponseDeep{
PageInfo: PageInfo{
NumPages: int(math.Ceil(float64(total) / float64(limit))),
},
ResultArray: make([]interface{}, 0),
}

for i := start; i < total && len(res.ResultArray) < limit; i++ {
res.ResultArray = append(res.ResultArray, i)
}

w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(res)
if err != nil {
w.WriteHeader(500)
}
}

func getValue(queryValue string, hasBody bool, paginationValue int) int {
if hasBody {
return paginationValue
Expand Down

0 comments on commit ad5c46f

Please sign in to comment.