X Tutup
Skip to content

Commit a3fba66

Browse files
author
nate smith
committed
add and use RESTWithNext
1 parent 659a8ed commit a3fba66

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

api/client.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -316,41 +316,55 @@ func graphQLClient(h *http.Client, hostname string) *graphql.Client {
316316

317317
// REST performs a REST request and parses the response.
318318
func (c Client) REST(hostname string, method string, p string, body io.Reader, data interface{}) error {
319+
_, err := c.RESTWithNext(hostname, method, p, body, data)
320+
return err
321+
}
322+
323+
func (c Client) RESTWithNext(hostname string, method string, p string, body io.Reader, data interface{}) (string, error) {
319324
req, err := http.NewRequest(method, restURL(hostname, p), body)
320325
if err != nil {
321-
return err
326+
return "", err
322327
}
323328

324329
req.Header.Set("Content-Type", "application/json; charset=utf-8")
325330

326331
resp, err := c.http.Do(req)
327332
if err != nil {
328-
return err
333+
return "", err
329334
}
330335
defer resp.Body.Close()
331336

332337
success := resp.StatusCode >= 200 && resp.StatusCode < 300
333338
if !success {
334-
return HandleHTTPError(resp)
339+
return "", HandleHTTPError(resp)
335340
}
336341

337342
if resp.StatusCode == http.StatusNoContent {
338-
return nil
343+
return "", nil
339344
}
340345

341346
b, err := ioutil.ReadAll(resp.Body)
342347
if err != nil {
343-
return err
348+
return "", err
344349
}
345350

346351
err = json.Unmarshal(b, &data)
347352
if err != nil {
348-
return err
353+
return "", err
349354
}
350355

351-
return nil
356+
var next string
357+
for _, m := range linkRE.FindAllStringSubmatch(resp.Header.Get("Link"), -1) {
358+
if len(m) > 2 && m[2] == "next" {
359+
next = m[1]
360+
}
361+
}
362+
363+
return next, nil
352364
}
353365

366+
var linkRE = regexp.MustCompile(`<([^>]+)>;\s*rel="([^"]+)"`)
367+
354368
func restURL(hostname string, pathOrURL string) string {
355369
if strings.HasPrefix(pathOrURL, "https://") || strings.HasPrefix(pathOrURL, "http://") {
356370
return pathOrURL

pkg/cmd/status/status.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"net/http"
88
"net/url"
9+
"regexp"
910
"sort"
1011
"strings"
1112
"time"
@@ -217,6 +218,17 @@ func (s *StatusGetter) ActualMention(n Notification) (string, error) {
217218
// These are split up by endpoint since it is along that boundary we parallelize
218219
// work
219220

221+
var linkRE = regexp.MustCompile(`<([^>]+)>;\s*rel="([^"]+)"`)
222+
223+
func findNextPage(resp *http.Response) (string, bool) {
224+
for _, m := range linkRE.FindAllStringSubmatch(resp.Header.Get("Link"), -1) {
225+
if len(m) > 2 && m[2] == "next" {
226+
return m[1], true
227+
}
228+
}
229+
return "", false
230+
}
231+
220232
// Populate .Mentions
221233
func (s *StatusGetter) LoadNotifications() error {
222234
perPage := 100
@@ -233,21 +245,24 @@ func (s *StatusGetter) LoadNotifications() error {
233245
// not work with PATs right now.
234246
var ns []Notification
235247
var resp []Notification
236-
pages := 3
237-
for page := 1; page <= pages; page++ {
238-
query.Add("page", fmt.Sprintf("%d", page))
239-
p := fmt.Sprintf("notifications?%s", query.Encode())
240-
err := c.REST(ghinstance.Default(), "GET", p, nil, &resp)
248+
pages := 0
249+
p := fmt.Sprintf("notifications?%s", query.Encode())
250+
for pages < 3 {
251+
next, err := c.RESTWithNext(ghinstance.Default(), "GET", p, nil, &resp)
241252
if err != nil {
242253
var httpErr api.HTTPError
243254
if !errors.As(err, &httpErr) || httpErr.StatusCode != 404 {
244255
return fmt.Errorf("could not get notifications: %w", err)
245256
}
246257
}
247258
ns = append(ns, resp...)
248-
if len(resp) == 0 || len(resp) < perPage {
259+
260+
if next == "" || len(resp) < perPage {
249261
break
250262
}
263+
264+
pages++
265+
p = next
251266
}
252267

253268
s.Mentions = []StatusItem{}
@@ -430,21 +445,23 @@ func (s *StatusGetter) LoadEvents() error {
430445

431446
var events []Event
432447
var resp []Event
433-
pages := 2
434-
for page := 1; page <= pages; page++ {
435-
query.Add("page", fmt.Sprintf("%d", page))
436-
p := fmt.Sprintf("users/%s/received_events?%s", currentUsername, query.Encode())
437-
err := c.REST(ghinstance.Default(), "GET", p, nil, &resp)
448+
pages := 0
449+
p := fmt.Sprintf("users/%s/received_events?%s", currentUsername, query.Encode())
450+
for pages < 2 {
451+
next, err := c.RESTWithNext(ghinstance.Default(), "GET", p, nil, &resp)
438452
if err != nil {
439453
var httpErr api.HTTPError
440454
if !errors.As(err, &httpErr) || httpErr.StatusCode != 404 {
441455
return fmt.Errorf("could not get events: %w", err)
442456
}
443457
}
444458
events = append(events, resp...)
445-
if len(resp) == 0 || len(resp) < perPage {
459+
if next == "" || len(resp) < perPage {
446460
break
447461
}
462+
463+
pages++
464+
p = next
448465
}
449466

450467
s.RepoActivity = []StatusItem{}

0 commit comments

Comments
 (0)
X Tutup