github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/pagination/info.go (about) 1 package pagination 2 3 import ( 4 "bytes" 5 6 "github.com/opentelekomcloud/gophertelekomcloud/internal/extract" 7 ) 8 9 // PageWithInfo is a page with marker information inside `page_info` 10 type PageWithInfo struct { 11 MarkerPageBase 12 } 13 14 type pageInfo struct { 15 PreviousMarker string `json:"previous_marker"` 16 NextMarker string `json:"next_marker"` 17 CurrentCount int `json:"current_count"` 18 } 19 20 func (p PageWithInfo) LastMarker() (string, error) { 21 var info pageInfo 22 err := extract.IntoStructPtr(bytes.NewReader(p.Body), &info, "page_info") 23 if err != nil { 24 return "", err 25 } 26 return info.NextMarker, nil 27 } 28 29 // NextPageURL generates the URL for the page of results after this one. 30 func (p PageWithInfo) NextPageURL() (string, error) { 31 currentURL := p.URL 32 33 mark, err := p.Owner.LastMarker() 34 if err != nil { 35 return "", err 36 } 37 if mark == "" { 38 return "", nil 39 } 40 41 q := currentURL.Query() 42 q.Set("marker", mark) 43 currentURL.RawQuery = q.Encode() 44 45 return currentURL.String(), nil 46 } 47 48 func NewPageWithInfo(r PageResult) PageWithInfo { 49 p := PageWithInfo{MarkerPageBase: MarkerPageBase{ 50 PageResult: r, 51 }} 52 p.Owner = &p 53 return p 54 }