Skip to content

Commit 3d79edf

Browse files
committed
add new browse API
1 parent fc4f719 commit 3d79edf

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

Source/Index.swift

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Tests/IndexTests.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,48 @@ class IndexTests: XCTestCase {
649649
waitForExpectationsWithTimeout(expectationTimeout, handler: nil)
650650
}
651651

652+
func testBrowseWithQuery() {
653+
let expectation = expectationWithDescription("testBrowseWithQuery")
654+
var objects = [AnyObject]()
655+
for i in 0...1500 {
656+
objects.append(["i": i])
657+
}
658+
659+
index.addObjects(objects, block: { (JSON, error) -> Void in
660+
if let error = error {
661+
XCTFail("Error during addObjects: \(error)")
662+
expectation.fulfill()
663+
} else {
664+
self.index.waitTask(JSON!["taskID"] as! Int, block: { (JSON, error) -> Void in
665+
if let error = error {
666+
XCTFail("Error during waitTask: \(error)")
667+
expectation.fulfill()
668+
} else {
669+
let query = Query()
670+
query.hitsPerPage = 10
671+
672+
var n = 0;
673+
674+
self.index.browse(query, block: { (iterator, end, error) -> Void in
675+
if let error = error {
676+
XCTFail("Error during browse: \(error)")
677+
expectation.fulfill()
678+
} else if end {
679+
XCTAssertEqual(n, 1500 / 10, "Wrong number of page")
680+
expectation.fulfill()
681+
} else {
682+
++n
683+
iterator.next()
684+
}
685+
})
686+
}
687+
})
688+
}
689+
})
690+
691+
waitForExpectationsWithTimeout(expectationTimeout, handler: nil)
692+
}
693+
652694
func testKeyOperations() {
653695
let expectation = expectationWithDescription("testKeyOperations")
654696
let object = ["city": "San Francisco", "objectID": "a/go/?à"]

0 commit comments

Comments
 (0)