populatePageValues parses the HTTP Link response headers and populates the various pagination link values in the Response.
()
| 999 | // populatePageValues parses the HTTP Link response headers and populates the |
| 1000 | // various pagination link values in the Response. |
| 1001 | func (r *Response) populatePageValues() { |
| 1002 | if links, ok := r.Response.Header["Link"]; ok && len(links) > 0 { |
| 1003 | for link := range strings.SplitSeq(links[0], ",") { |
| 1004 | segments := strings.Split(strings.TrimSpace(link), ";") |
| 1005 | |
| 1006 | // link must at least have href and rel |
| 1007 | if len(segments) < 2 { |
| 1008 | continue |
| 1009 | } |
| 1010 | |
| 1011 | // ensure href is properly formatted |
| 1012 | if !strings.HasPrefix(segments[0], "<") || !strings.HasSuffix(segments[0], ">") { |
| 1013 | continue |
| 1014 | } |
| 1015 | |
| 1016 | // try to pull out page parameter |
| 1017 | url, err := url.Parse(segments[0][1 : len(segments[0])-1]) |
| 1018 | if err != nil { |
| 1019 | continue |
| 1020 | } |
| 1021 | |
| 1022 | q := url.Query() |
| 1023 | |
| 1024 | if cursor := q.Get("cursor"); cursor != "" { |
| 1025 | for _, segment := range segments[1:] { |
| 1026 | if strings.TrimSpace(segment) == `rel="next"` { |
| 1027 | r.Cursor = cursor |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | continue |
| 1032 | } |
| 1033 | |
| 1034 | page := q.Get("page") |
| 1035 | since := q.Get("since") |
| 1036 | before := q.Get("before") |
| 1037 | after := q.Get("after") |
| 1038 | |
| 1039 | if page == "" && before == "" && after == "" && since == "" { |
| 1040 | continue |
| 1041 | } |
| 1042 | |
| 1043 | if since != "" && page == "" { |
| 1044 | page = since |
| 1045 | } |
| 1046 | |
| 1047 | for _, segment := range segments[1:] { |
| 1048 | switch strings.TrimSpace(segment) { |
| 1049 | case `rel="next"`: |
| 1050 | if r.NextPage, err = strconv.Atoi(page); err != nil { |
| 1051 | r.NextPageToken = page |
| 1052 | } |
| 1053 | r.After = after |
| 1054 | case `rel="prev"`: |
| 1055 | r.PrevPage, _ = strconv.Atoi(page) |
| 1056 | r.Before = before |
| 1057 | case `rel="first"`: |
| 1058 | r.FirstPage, _ = strconv.Atoi(page) |