|
| 1 | +package voice |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "reflect" |
| 7 | + |
| 8 | + messagebird "github.com/messagebird/go-rest-api" |
| 9 | +) |
| 10 | + |
| 11 | +// A Paginator is used to stream the contents of a collection of some type from |
| 12 | +// the MessageBird API. |
| 13 | +// |
| 14 | +// Paginators are single use and can therefore not be reset. |
| 15 | +type Paginator struct { |
| 16 | + endpoint string |
| 17 | + nextPage int |
| 18 | + structType reflect.Type |
| 19 | + client *messagebird.Client |
| 20 | +} |
| 21 | + |
| 22 | +// newPaginator creates a new paginator. |
| 23 | +// |
| 24 | +// endpoint is called with the `page` query parameter until no more pages are |
| 25 | +// available. |
| 26 | +// |
| 27 | +// typ is the non-pointer type of a single element returned by a page. |
| 28 | +func newPaginator(client *messagebird.Client, endpoint string, typ reflect.Type) *Paginator { |
| 29 | + return &Paginator{ |
| 30 | + endpoint: endpoint, |
| 31 | + nextPage: 1, // Page indices start at 1. |
| 32 | + structType: typ, |
| 33 | + client: client, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// NextPage queries the next page from the MessageBird API. |
| 38 | +// |
| 39 | +// The interface{} contains a slice of the type this paginator handles. |
| 40 | +// |
| 41 | +// When no more items are available, an empty slice and io.EOF are returned. |
| 42 | +// If another kind of error occurs, nil and and the error are returned. |
| 43 | +func (pag *Paginator) NextPage() (interface{}, error) { |
| 44 | + type pagination struct { |
| 45 | + TotalCount int `json:"totalCount"` |
| 46 | + PageCount int `json:"pageCount"` |
| 47 | + CurrentPage int `json:"currentPage"` |
| 48 | + PerPage int `json:"perPage"` |
| 49 | + } |
| 50 | + rawType := reflect.StructOf([]reflect.StructField{ |
| 51 | + { |
| 52 | + Name: "Data", |
| 53 | + Type: reflect.SliceOf(pag.structType), |
| 54 | + Tag: "json:\"data\"", |
| 55 | + }, |
| 56 | + { |
| 57 | + Name: "Pagination", |
| 58 | + Type: reflect.TypeOf(pagination{}), |
| 59 | + Tag: "json:\"pagination\"", |
| 60 | + }, |
| 61 | + }) |
| 62 | + rawVal := reflect.New(rawType) |
| 63 | + |
| 64 | + if err := pag.client.Request(rawVal.Interface(), "GET", fmt.Sprintf("%s?page=%d", pag.endpoint, pag.nextPage), nil); err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + |
| 68 | + data := rawVal.Elem().FieldByName("Data").Interface() |
| 69 | + pageInfo := rawVal.Elem().FieldByName("Pagination").Interface().(pagination) |
| 70 | + |
| 71 | + // If no more items are available, a page with 0 elements is returned. |
| 72 | + if pag.nextPage > pageInfo.PageCount { |
| 73 | + return data, io.EOF |
| 74 | + } |
| 75 | + |
| 76 | + pag.nextPage++ |
| 77 | + return data, nil |
| 78 | +} |
| 79 | + |
| 80 | +// Stream creates a channel which streams the contents of all remaining pages |
| 81 | +// ony by one. |
| 82 | +// |
| 83 | +// The Paginator is consumed in the process, meaning that after elements have |
| 84 | +// been received, NextPage will return EOF. It is invalid to mix calls to |
| 85 | +// NextPage an Stream, even after the stream channel was closed. |
| 86 | +// |
| 87 | +// If an error occurs, the next item sent over the channel will be an error |
| 88 | +// instead of a regular value. The channel is closed directly after this. |
| 89 | +func (pag *Paginator) Stream() <-chan interface{} { |
| 90 | + out := make(chan interface{}) |
| 91 | + go func() { |
| 92 | + defer close(out) |
| 93 | + for { |
| 94 | + page, err := pag.NextPage() |
| 95 | + if err != nil { |
| 96 | + if err != io.EOF { |
| 97 | + out <- err |
| 98 | + } |
| 99 | + break |
| 100 | + } |
| 101 | + v := reflect.ValueOf(page) |
| 102 | + for i, l := 0, v.Len(); i < l; i++ { |
| 103 | + out <- v.Index(i).Interface() |
| 104 | + } |
| 105 | + } |
| 106 | + }() |
| 107 | + return out |
| 108 | +} |
0 commit comments