Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 1 | package pagination |
| 2 | |
Ash Wilson | 7049af4 | 2014-09-16 13:04:48 -0400 | [diff] [blame] | 3 | import ( |
| 4 | "errors" |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 5 | "fmt" |
Jon Perritt | 0ed1fa9 | 2015-02-18 13:50:43 -0700 | [diff] [blame] | 6 | "net/http" |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 7 | "reflect" |
Jon Perritt | 521cc68 | 2015-02-19 08:39:01 -0700 | [diff] [blame] | 8 | "strings" |
Ash Wilson | 7049af4 | 2014-09-16 13:04:48 -0400 | [diff] [blame] | 9 | |
Krzysztof Szukiełojć | 3f41d08 | 2017-05-07 14:43:06 +0200 | [diff] [blame] | 10 | "gerrit.mcp.mirantis.net/debian/gophercloud.git" |
Ash Wilson | 7049af4 | 2014-09-16 13:04:48 -0400 | [diff] [blame] | 11 | ) |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 12 | |
| 13 | var ( |
| 14 | // ErrPageNotAvailable is returned from a Pager when a next or previous page is requested, but does not exist. |
| 15 | ErrPageNotAvailable = errors.New("The requested page does not exist.") |
| 16 | ) |
| 17 | |
| 18 | // Page must be satisfied by the result type of any resource collection. |
| 19 | // It allows clients to interact with the resource uniformly, regardless of whether or not or how it's paginated. |
| 20 | // Generally, rather than implementing this interface directly, implementors should embed one of the concrete PageBase structs, |
| 21 | // instead. |
| 22 | // Depending on the pagination strategy of a particular resource, there may be an additional subinterface that the result type |
| 23 | // will need to implement. |
| 24 | type Page interface { |
| 25 | |
| 26 | // NextPageURL generates the URL for the page of data that follows this collection. |
| 27 | // Return "" if no such page exists. |
| 28 | NextPageURL() (string, error) |
| 29 | |
| 30 | // IsEmpty returns true if this Page has no items in it. |
| 31 | IsEmpty() (bool, error) |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 32 | |
Jon Perritt | 46b71ba | 2015-02-18 22:25:55 -0700 | [diff] [blame] | 33 | // GetBody returns the Page Body. This is used in the `AllPages` method. |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 34 | GetBody() interface{} |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // Pager knows how to advance through a specific resource collection, one page at a time. |
| 38 | type Pager struct { |
Ash Wilson | 7049af4 | 2014-09-16 13:04:48 -0400 | [diff] [blame] | 39 | client *gophercloud.ServiceClient |
| 40 | |
Ash Wilson | fc4191f | 2014-10-10 15:05:27 -0400 | [diff] [blame] | 41 | initialURL string |
Ash Wilson | 5bc7ba8 | 2014-10-09 13:57:34 -0400 | [diff] [blame] | 42 | |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 43 | createPage func(r PageResult) Page |
Ash Wilson | a740247 | 2014-09-16 15:18:34 -0400 | [diff] [blame] | 44 | |
Jon Perritt | 9bd7bd9 | 2014-09-28 20:10:27 -0500 | [diff] [blame] | 45 | Err error |
| 46 | |
Ash Wilson | a740247 | 2014-09-16 15:18:34 -0400 | [diff] [blame] | 47 | // Headers supplies additional HTTP headers to populate on each paged request. |
| 48 | Headers map[string]string |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // NewPager constructs a manually-configured pager. |
| 52 | // Supply the URL for the first page, a function that requests a specific page given a URL, and a function that counts a page. |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 53 | func NewPager(client *gophercloud.ServiceClient, initialURL string, createPage func(r PageResult) Page) Pager { |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 54 | return Pager{ |
Ash Wilson | 7049af4 | 2014-09-16 13:04:48 -0400 | [diff] [blame] | 55 | client: client, |
Ash Wilson | fc4191f | 2014-10-10 15:05:27 -0400 | [diff] [blame] | 56 | initialURL: initialURL, |
| 57 | createPage: createPage, |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // WithPageCreator returns a new Pager that substitutes a different page creation function. This is |
| 62 | // useful for overriding List functions in delegation. |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 63 | func (p Pager) WithPageCreator(createPage func(r PageResult) Page) Pager { |
Ash Wilson | fc4191f | 2014-10-10 15:05:27 -0400 | [diff] [blame] | 64 | return Pager{ |
| 65 | client: p.client, |
| 66 | initialURL: p.initialURL, |
| 67 | createPage: createPage, |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Ash Wilson | 7049af4 | 2014-09-16 13:04:48 -0400 | [diff] [blame] | 71 | func (p Pager) fetchNextPage(url string) (Page, error) { |
Ash Wilson | a740247 | 2014-09-16 15:18:34 -0400 | [diff] [blame] | 72 | resp, err := Request(p.client, p.Headers, url) |
Ash Wilson | 7049af4 | 2014-09-16 13:04:48 -0400 | [diff] [blame] | 73 | if err != nil { |
| 74 | return nil, err |
| 75 | } |
| 76 | |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 77 | remembered, err := PageResultFrom(resp) |
Ash Wilson | 7049af4 | 2014-09-16 13:04:48 -0400 | [diff] [blame] | 78 | if err != nil { |
| 79 | return nil, err |
| 80 | } |
| 81 | |
Ash Wilson | fc4191f | 2014-10-10 15:05:27 -0400 | [diff] [blame] | 82 | return p.createPage(remembered), nil |
Ash Wilson | 7049af4 | 2014-09-16 13:04:48 -0400 | [diff] [blame] | 83 | } |
| 84 | |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 85 | // EachPage iterates over each page returned by a Pager, yielding one at a time to a handler function. |
| 86 | // Return "false" from the handler to prematurely stop iterating. |
| 87 | func (p Pager) EachPage(handler func(Page) (bool, error)) error { |
Jon Perritt | 6f9e4ff | 2014-09-30 13:29:47 -0500 | [diff] [blame] | 88 | if p.Err != nil { |
| 89 | return p.Err |
| 90 | } |
Ash Wilson | fc4191f | 2014-10-10 15:05:27 -0400 | [diff] [blame] | 91 | currentURL := p.initialURL |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 92 | for { |
| 93 | currentPage, err := p.fetchNextPage(currentURL) |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| 97 | |
| 98 | empty, err := currentPage.IsEmpty() |
| 99 | if err != nil { |
| 100 | return err |
| 101 | } |
| 102 | if empty { |
| 103 | return nil |
| 104 | } |
| 105 | |
| 106 | ok, err := handler(currentPage) |
| 107 | if err != nil { |
| 108 | return err |
| 109 | } |
| 110 | if !ok { |
| 111 | return nil |
| 112 | } |
| 113 | |
| 114 | currentURL, err = currentPage.NextPageURL() |
| 115 | if err != nil { |
| 116 | return err |
| 117 | } |
| 118 | if currentURL == "" { |
| 119 | return nil |
| 120 | } |
| 121 | } |
| 122 | } |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 123 | |
| 124 | // AllPages returns all the pages from a `List` operation in a single page, |
| 125 | // allowing the user to retrieve all the pages at once. |
| 126 | func (p Pager) AllPages() (Page, error) { |
| 127 | // pagesSlice holds all the pages until they get converted into as Page Body. |
| 128 | var pagesSlice []interface{} |
| 129 | // body will contain the final concatenated Page body. |
| 130 | var body reflect.Value |
| 131 | |
| 132 | // Grab a test page to ascertain the page body type. |
| 133 | testPage, err := p.fetchNextPage(p.initialURL) |
| 134 | if err != nil { |
| 135 | return nil, err |
| 136 | } |
Jon Perritt | 3d0a185 | 2015-02-19 08:51:39 -0700 | [diff] [blame] | 137 | // Store the page type so we can use reflection to create a new mega-page of |
| 138 | // that type. |
| 139 | pageType := reflect.TypeOf(testPage) |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 140 | |
jrperritt | 057373d | 2016-05-02 14:01:39 -0500 | [diff] [blame] | 141 | // if it's a single page, just return the testPage (first page) |
| 142 | if _, found := pageType.FieldByName("SinglePageBase"); found { |
| 143 | return testPage, nil |
| 144 | } |
| 145 | |
Jon Perritt | 2a3f7e8 | 2015-02-18 14:11:33 -0700 | [diff] [blame] | 146 | // Switch on the page body type. Recognized types are `map[string]interface{}`, |
| 147 | // `[]byte`, and `[]interface{}`. |
jrperritt | 2aaed7f | 2017-02-20 16:19:24 -0600 | [diff] [blame] | 148 | switch pb := testPage.GetBody().(type) { |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 149 | case map[string]interface{}: |
| 150 | // key is the map key for the page body if the body type is `map[string]interface{}`. |
| 151 | var key string |
| 152 | // Iterate over the pages to concatenate the bodies. |
Jon Perritt | 2be387a | 2016-03-31 09:31:58 -0500 | [diff] [blame] | 153 | err = p.EachPage(func(page Page) (bool, error) { |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 154 | b := page.GetBody().(map[string]interface{}) |
jrperritt | 2aaed7f | 2017-02-20 16:19:24 -0600 | [diff] [blame] | 155 | for k, v := range b { |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 156 | // If it's a linked page, we don't want the `links`, we want the other one. |
Jon Perritt | 521cc68 | 2015-02-19 08:39:01 -0700 | [diff] [blame] | 157 | if !strings.HasSuffix(k, "links") { |
jrperritt | 303bf8a | 2017-03-02 12:23:34 -0600 | [diff] [blame] | 158 | // check the field's type. we only want []interface{} (which is really []map[string]interface{}) |
jrperritt | 2aaed7f | 2017-02-20 16:19:24 -0600 | [diff] [blame] | 159 | switch vt := v.(type) { |
jrperritt | 2aaed7f | 2017-02-20 16:19:24 -0600 | [diff] [blame] | 160 | case []interface{}: |
| 161 | key = k |
| 162 | pagesSlice = append(pagesSlice, vt...) |
| 163 | } |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 166 | return true, nil |
| 167 | }) |
| 168 | if err != nil { |
| 169 | return nil, err |
| 170 | } |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 171 | // Set body to value of type `map[string]interface{}` |
| 172 | body = reflect.MakeMap(reflect.MapOf(reflect.TypeOf(key), reflect.TypeOf(pagesSlice))) |
| 173 | body.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(pagesSlice)) |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 174 | case []byte: |
| 175 | // Iterate over the pages to concatenate the bodies. |
Jon Perritt | 2be387a | 2016-03-31 09:31:58 -0500 | [diff] [blame] | 176 | err = p.EachPage(func(page Page) (bool, error) { |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 177 | b := page.GetBody().([]byte) |
| 178 | pagesSlice = append(pagesSlice, b) |
| 179 | // seperate pages with a comma |
| 180 | pagesSlice = append(pagesSlice, []byte{10}) |
| 181 | return true, nil |
| 182 | }) |
| 183 | if err != nil { |
| 184 | return nil, err |
| 185 | } |
Jon Perritt | a11f5db | 2015-06-21 21:17:06 -0600 | [diff] [blame] | 186 | if len(pagesSlice) > 0 { |
| 187 | // Remove the trailing comma. |
| 188 | pagesSlice = pagesSlice[:len(pagesSlice)-1] |
| 189 | } |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 190 | var b []byte |
| 191 | // Combine the slice of slices in to a single slice. |
| 192 | for _, slice := range pagesSlice { |
| 193 | b = append(b, slice.([]byte)...) |
| 194 | } |
| 195 | // Set body to value of type `bytes`. |
| 196 | body = reflect.New(reflect.TypeOf(b)).Elem() |
| 197 | body.SetBytes(b) |
Jon Perritt | 0ed1fa9 | 2015-02-18 13:50:43 -0700 | [diff] [blame] | 198 | case []interface{}: |
| 199 | // Iterate over the pages to concatenate the bodies. |
Jon Perritt | 2be387a | 2016-03-31 09:31:58 -0500 | [diff] [blame] | 200 | err = p.EachPage(func(page Page) (bool, error) { |
Jon Perritt | 0ed1fa9 | 2015-02-18 13:50:43 -0700 | [diff] [blame] | 201 | b := page.GetBody().([]interface{}) |
| 202 | pagesSlice = append(pagesSlice, b...) |
| 203 | return true, nil |
| 204 | }) |
| 205 | if err != nil { |
| 206 | return nil, err |
| 207 | } |
Jon Perritt | 2a3f7e8 | 2015-02-18 14:11:33 -0700 | [diff] [blame] | 208 | // Set body to value of type `[]interface{}` |
Jon Perritt | 0ed1fa9 | 2015-02-18 13:50:43 -0700 | [diff] [blame] | 209 | body = reflect.MakeSlice(reflect.TypeOf(pagesSlice), len(pagesSlice), len(pagesSlice)) |
| 210 | for i, s := range pagesSlice { |
| 211 | body.Index(i).Set(reflect.ValueOf(s)) |
| 212 | } |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 213 | default: |
jrperritt | 29ae6b3 | 2016-04-13 12:59:37 -0500 | [diff] [blame] | 214 | err := gophercloud.ErrUnexpectedType{} |
Jon Perritt | 8025197 | 2016-03-09 00:32:30 -0600 | [diff] [blame] | 215 | err.Expected = "map[string]interface{}/[]byte/[]interface{}" |
jrperritt | 2aaed7f | 2017-02-20 16:19:24 -0600 | [diff] [blame] | 216 | err.Actual = fmt.Sprintf("%T", pb) |
Jon Perritt | 8025197 | 2016-03-09 00:32:30 -0600 | [diff] [blame] | 217 | return nil, err |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | // Each `Extract*` function is expecting a specific type of page coming back, |
Jon Perritt | 3d0a185 | 2015-02-19 08:51:39 -0700 | [diff] [blame] | 221 | // otherwise the type assertion in those functions will fail. pageType is needed |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 222 | // to create a type in this method that has the same type that the `Extract*` |
| 223 | // function is expecting and set the Body of that object to the concatenated |
| 224 | // pages. |
Jon Perritt | 3d0a185 | 2015-02-19 08:51:39 -0700 | [diff] [blame] | 225 | page := reflect.New(pageType) |
Jon Perritt | 0ed1fa9 | 2015-02-18 13:50:43 -0700 | [diff] [blame] | 226 | // Set the page body to be the concatenated pages. |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 227 | page.Elem().FieldByName("Body").Set(body) |
Jon Perritt | 0ed1fa9 | 2015-02-18 13:50:43 -0700 | [diff] [blame] | 228 | // Set any additional headers that were pass along. The `objectstorage` pacakge, |
| 229 | // for example, passes a Content-Type header. |
| 230 | h := make(http.Header) |
| 231 | for k, v := range p.Headers { |
| 232 | h.Add(k, v) |
| 233 | } |
| 234 | page.Elem().FieldByName("Header").Set(reflect.ValueOf(h)) |
Jon Perritt | db319f1 | 2015-02-17 19:32:40 -0700 | [diff] [blame] | 235 | // Type assert the page to a Page interface so that the type assertion in the |
| 236 | // `Extract*` methods will work. |
| 237 | return page.Elem().Interface().(Page), err |
| 238 | } |