github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/pagination/offset.go (about) 1 package pagination 2 3 import ( 4 "fmt" 5 "strconv" 6 ) 7 8 type OffsetPage interface { 9 // LastElement returning index of the last element of the page 10 LastElement() int 11 } 12 13 type OffsetPageBase struct { 14 Offset int 15 Limit int 16 17 PageResult 18 } 19 20 func (p OffsetPageBase) LastElement() int { 21 q := p.URL.Query() 22 offset, err := strconv.Atoi(q.Get("offset")) 23 if err != nil { 24 offset = p.Offset 25 q.Set("offset", strconv.Itoa(offset)) 26 } 27 limit, err := strconv.Atoi(q.Get("limit")) 28 if err != nil { 29 limit = p.Limit 30 q.Set("limit", strconv.Itoa(limit)) 31 } 32 return offset + limit 33 } 34 35 func (p OffsetPageBase) NextPageURL() (string, error) { 36 currentURL := p.URL 37 q := currentURL.Query() 38 if q.Get("offset") == "" && q.Get("limit") == "" { 39 // without offset and limit it's just a SinglePageBase 40 return "", nil 41 } 42 q.Set("offset", strconv.Itoa(p.LastElement())) 43 currentURL.RawQuery = q.Encode() 44 return currentURL.String(), nil 45 } 46 47 // IsEmpty returns true if this Page has no items in it. 48 func (p OffsetPageBase) IsEmpty() (bool, error) { 49 body, err := p.GetBodyAsSlice() 50 if err != nil { 51 return false, fmt.Errorf("error converting page body to slice: %w", err) 52 } 53 54 return len(body) == 0, nil 55 } 56 57 // GetBody returns the Page Body. This is used in the `AllPages` method. 58 func (p OffsetPageBase) GetBody() []byte { 59 return p.Body 60 }