request_pagination.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package request
  2. import (
  3. "reflect"
  4. "sync/atomic"
  5. "github.com/aws/aws-sdk-go/aws"
  6. "github.com/aws/aws-sdk-go/aws/awsutil"
  7. )
  8. // A Pagination provides paginating of SDK API operations which are paginatable.
  9. // Generally you should not use this type directly, but use the "Pages" API
  10. // operations method to automatically perform pagination for you. Such as,
  11. // "S3.ListObjectsPages", and "S3.ListObjectsPagesWithContext" methods.
  12. //
  13. // Pagination differs from a Paginator type in that pagination is the type that
  14. // does the pagination between API operations, and Paginator defines the
  15. // configuration that will be used per page request.
  16. //
  17. // cont := true
  18. // for p.Next() && cont {
  19. // data := p.Page().(*s3.ListObjectsOutput)
  20. // // process the page's data
  21. // }
  22. // return p.Err()
  23. //
  24. // See service client API operation Pages methods for examples how the SDK will
  25. // use the Pagination type.
  26. type Pagination struct {
  27. // Function to return a Request value for each pagination request.
  28. // Any configuration or handlers that need to be applied to the request
  29. // prior to getting the next page should be done here before the request
  30. // returned.
  31. //
  32. // NewRequest should always be built from the same API operations. It is
  33. // undefined if different API operations are returned on subsequent calls.
  34. NewRequest func() (*Request, error)
  35. started bool
  36. nextTokens []interface{}
  37. err error
  38. curPage interface{}
  39. }
  40. // HasNextPage will return true if Pagination is able to determine that the API
  41. // operation has additional pages. False will be returned if there are no more
  42. // pages remaining.
  43. //
  44. // Will always return true if Next has not been called yet.
  45. func (p *Pagination) HasNextPage() bool {
  46. return !(p.started && len(p.nextTokens) == 0)
  47. }
  48. // Err returns the error Pagination encountered when retrieving the next page.
  49. func (p *Pagination) Err() error {
  50. return p.err
  51. }
  52. // Page returns the current page. Page should only be called after a successful
  53. // call to Next. It is undefined what Page will return if Page is called after
  54. // Next returns false.
  55. func (p *Pagination) Page() interface{} {
  56. return p.curPage
  57. }
  58. // Next will attempt to retrieve the next page for the API operation. When a page
  59. // is retrieved true will be returned. If the page cannot be retrieved, or there
  60. // are no more pages false will be returned.
  61. //
  62. // Use the Page method to retrieve the current page data. The data will need
  63. // to be cast to the API operation's output type.
  64. //
  65. // Use the Err method to determine if an error occurred if Page returns false.
  66. func (p *Pagination) Next() bool {
  67. if !p.HasNextPage() {
  68. return false
  69. }
  70. req, err := p.NewRequest()
  71. if err != nil {
  72. p.err = err
  73. return false
  74. }
  75. if p.started {
  76. for i, intok := range req.Operation.InputTokens {
  77. awsutil.SetValueAtPath(req.Params, intok, p.nextTokens[i])
  78. }
  79. }
  80. p.started = true
  81. err = req.Send()
  82. if err != nil {
  83. p.err = err
  84. return false
  85. }
  86. p.nextTokens = req.nextPageTokens()
  87. p.curPage = req.Data
  88. return true
  89. }
  90. // A Paginator is the configuration data that defines how an API operation
  91. // should be paginated. This type is used by the API service models to define
  92. // the generated pagination config for service APIs.
  93. //
  94. // The Pagination type is what provides iterating between pages of an API. It
  95. // is only used to store the token metadata the SDK should use for performing
  96. // pagination.
  97. type Paginator struct {
  98. InputTokens []string
  99. OutputTokens []string
  100. LimitToken string
  101. TruncationToken string
  102. }
  103. // nextPageTokens returns the tokens to use when asking for the next page of data.
  104. func (r *Request) nextPageTokens() []interface{} {
  105. if r.Operation.Paginator == nil {
  106. return nil
  107. }
  108. if r.Operation.TruncationToken != "" {
  109. tr, _ := awsutil.ValuesAtPath(r.Data, r.Operation.TruncationToken)
  110. if len(tr) == 0 {
  111. return nil
  112. }
  113. switch v := tr[0].(type) {
  114. case *bool:
  115. if !aws.BoolValue(v) {
  116. return nil
  117. }
  118. case bool:
  119. if v == false {
  120. return nil
  121. }
  122. }
  123. }
  124. tokens := []interface{}{}
  125. tokenAdded := false
  126. for _, outToken := range r.Operation.OutputTokens {
  127. vs, _ := awsutil.ValuesAtPath(r.Data, outToken)
  128. if len(vs) == 0 {
  129. tokens = append(tokens, nil)
  130. continue
  131. }
  132. v := vs[0]
  133. switch tv := v.(type) {
  134. case *string:
  135. if len(aws.StringValue(tv)) == 0 {
  136. tokens = append(tokens, nil)
  137. continue
  138. }
  139. case string:
  140. if len(tv) == 0 {
  141. tokens = append(tokens, nil)
  142. continue
  143. }
  144. }
  145. tokenAdded = true
  146. tokens = append(tokens, v)
  147. }
  148. if !tokenAdded {
  149. return nil
  150. }
  151. return tokens
  152. }
  153. // Ensure a deprecated item is only logged once instead of each time its used.
  154. func logDeprecatedf(logger aws.Logger, flag *int32, msg string) {
  155. if logger == nil {
  156. return
  157. }
  158. if atomic.CompareAndSwapInt32(flag, 0, 1) {
  159. logger.Log(msg)
  160. }
  161. }
  162. var (
  163. logDeprecatedHasNextPage int32
  164. logDeprecatedNextPage int32
  165. logDeprecatedEachPage int32
  166. )
  167. // HasNextPage returns true if this request has more pages of data available.
  168. //
  169. // Deprecated Use Pagination type for configurable pagination of API operations
  170. func (r *Request) HasNextPage() bool {
  171. logDeprecatedf(r.Config.Logger, &logDeprecatedHasNextPage,
  172. "Request.HasNextPage deprecated. Use Pagination type for configurable pagination of API operations")
  173. return len(r.nextPageTokens()) > 0
  174. }
  175. // NextPage returns a new Request that can be executed to return the next
  176. // page of result data. Call .Send() on this request to execute it.
  177. //
  178. // Deprecated Use Pagination type for configurable pagination of API operations
  179. func (r *Request) NextPage() *Request {
  180. logDeprecatedf(r.Config.Logger, &logDeprecatedNextPage,
  181. "Request.NextPage deprecated. Use Pagination type for configurable pagination of API operations")
  182. tokens := r.nextPageTokens()
  183. if len(tokens) == 0 {
  184. return nil
  185. }
  186. data := reflect.New(reflect.TypeOf(r.Data).Elem()).Interface()
  187. nr := New(r.Config, r.ClientInfo, r.Handlers, r.Retryer, r.Operation, awsutil.CopyOf(r.Params), data)
  188. for i, intok := range nr.Operation.InputTokens {
  189. awsutil.SetValueAtPath(nr.Params, intok, tokens[i])
  190. }
  191. return nr
  192. }
  193. // EachPage iterates over each page of a paginated request object. The fn
  194. // parameter should be a function with the following sample signature:
  195. //
  196. // func(page *T, lastPage bool) bool {
  197. // return true // return false to stop iterating
  198. // }
  199. //
  200. // Where "T" is the structure type matching the output structure of the given
  201. // operation. For example, a request object generated by
  202. // DynamoDB.ListTablesRequest() would expect to see dynamodb.ListTablesOutput
  203. // as the structure "T". The lastPage value represents whether the page is
  204. // the last page of data or not. The return value of this function should
  205. // return true to keep iterating or false to stop.
  206. //
  207. // Deprecated Use Pagination type for configurable pagination of API operations
  208. func (r *Request) EachPage(fn func(data interface{}, isLastPage bool) (shouldContinue bool)) error {
  209. logDeprecatedf(r.Config.Logger, &logDeprecatedEachPage,
  210. "Request.EachPage deprecated. Use Pagination type for configurable pagination of API operations")
  211. for page := r; page != nil; page = page.NextPage() {
  212. if err := page.Send(); err != nil {
  213. return err
  214. }
  215. if getNextPage := fn(page.Data, !page.HasNextPage()); !getNextPage {
  216. return page.Error
  217. }
  218. }
  219. return nil
  220. }