Skip to content

Commit 322cff0

Browse files
committed
chore: add cursor envelope endpoint
1 parent 08c54f9 commit 322cff0

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

cmd/server/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func main() {
4444
r.HandleFunc("/pagination/limitoffset/deep_outputs/page", pagination.HandleLimitOffsetDeepOutputsPage).Methods(http.MethodGet, http.MethodPut)
4545
r.HandleFunc("/pagination/limitoffset/offset", pagination.HandleLimitOffsetOffset).Methods(http.MethodGet, http.MethodPut)
4646
r.HandleFunc("/pagination/cursor", pagination.HandleCursor).Methods(http.MethodGet, http.MethodPut)
47+
r.HandleFunc("/pagination/cursor/response_envelope", pagination.HandleCursorResponseEnvelope).Methods(http.MethodGet, http.MethodPut)
4748
r.HandleFunc("/pagination/url", pagination.HandleURL).Methods(http.MethodGet)
4849
r.HandleFunc("/pagination/cursor_non_numeric", pagination.HandleNonNumericCursor).Methods(http.MethodGet)
4950
r.HandleFunc("/retries", retries.HandleRetries).Methods(http.MethodGet, http.MethodPost)

internal/pagination/service.go

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,62 @@ func HandleCursor(w http.ResponseWriter, r *http.Request) {
123123
}
124124

125125
cursor := getValue(queryCursor, hasBody, pagination.Cursor)
126+
resultArray := make([]interface{}, 0)
126127

128+
for i := cursor + 1; i < total && len(resultArray) < 15; i++ {
129+
resultArray = append(resultArray, i)
130+
}
131+
132+
w.Header().Set("Content-Type", "application/json")
127133
res := PaginationResponse{
128134
NumPages: 0,
129-
ResultArray: make([]interface{}, 0),
135+
ResultArray: resultArray,
130136
}
131137

132-
for i := cursor + 1; i < total && len(res.ResultArray) < 15; i++ {
133-
res.ResultArray = append(res.ResultArray, i)
138+
err := json.NewEncoder(w).Encode(res)
139+
if err != nil {
140+
w.WriteHeader(500)
141+
}
142+
143+
}
144+
145+
func HandleCursorResponseEnvelope(w http.ResponseWriter, r *http.Request) {
146+
queryCursor := r.FormValue("cursor")
147+
148+
var pagination CursorRequest
149+
hasBody := true
150+
if err := json.NewDecoder(r.Body).Decode(&pagination); err != nil {
151+
hasBody = false
152+
}
153+
154+
cursor := getValue(queryCursor, hasBody, pagination.Cursor)
155+
resultArray := make([]interface{}, 0)
156+
157+
for i := cursor + 1; i < total && len(resultArray) < 15; i++ {
158+
resultArray = append(resultArray, i)
134159
}
135160

136161
w.Header().Set("Content-Type", "application/json")
162+
var lastItem *string
163+
if len(resultArray) > 0 {
164+
idx := strconv.Itoa(resultArray[len(resultArray)-1].(int))
165+
lastItem = &idx
166+
} else {
167+
lastItem = nil
168+
}
169+
170+
res := PaginationResponseDeep{
171+
PageInfo: PageInfo{
172+
Next: lastItem,
173+
},
174+
ResultArray: resultArray,
175+
}
176+
137177
err := json.NewEncoder(w).Encode(res)
138178
if err != nil {
139179
w.WriteHeader(500)
140180
}
181+
141182
}
142183

143184
func HandleURL(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)