@@ -350,4 +350,59 @@ public class Index {
350350 let path = " 1/indexes/ \( urlEncodedIndexName) /browse?page= \( page) &hitsPerPage= \( hitsPerPage) "
351351 client. performHTTPQuery ( path, method: . GET, body: nil , hostnames: client. readQueryHostnames, block: block)
352352 }
353+
354+ public typealias BrowseIteratorHandler = ( iterator: BrowseIterator , end: Bool , error: NSError ? ) -> Void
355+
356+ public class BrowseIterator {
357+ public let index : Index
358+ public let query : Query
359+
360+ private let path : String
361+ private let queryURL : String
362+ private let block : BrowseIteratorHandler
363+ private var cursor = " "
364+ private var end = false
365+
366+ public var result : [ String : AnyObject ] ?
367+
368+ init ( index: Index , query: Query , block: BrowseIteratorHandler ) {
369+ self . index = index
370+ self . query = query
371+ self . block = block
372+
373+ queryURL = query. buildURL ( )
374+ path = " 1/indexes/ \( index. urlEncodedIndexName) /browse? \( queryURL) "
375+ }
376+
377+ public func next( ) {
378+ var requestPath = path
379+ if cursor != " " {
380+ if queryURL != " " {
381+ requestPath += " & "
382+ }
383+
384+ requestPath += " cursor= \( cursor. urlEncode ( ) ) "
385+ }
386+
387+ index. client. performHTTPQuery ( requestPath, method: . GET, body: nil , hostnames: index. client. readQueryHostnames) { ( JSON, error) -> Void in
388+ if let error = error {
389+ self . block ( iterator: self , end: false , error: error)
390+ } else {
391+ self . result = JSON
392+ if let cursor = JSON![ " cursor " ] as? String {
393+ self . cursor = cursor
394+ } else {
395+ self . end = true
396+ }
397+
398+ self . block ( iterator: self , end: self . end, error: nil )
399+ }
400+ }
401+ }
402+ }
403+
404+ public func browse( query: Query , block: BrowseIteratorHandler ) {
405+ let iterator = BrowseIterator ( index: self , query: query, block: block)
406+ iterator. next ( ) // first call
407+ }
353408}
0 commit comments