Skip to content

Commit 026e524

Browse files
committed
refactor + handler is now optional
1 parent 8c488a1 commit 026e524

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

Source/Client.swift

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import Foundation
1010
import Alamofire
1111

12+
public typealias CompletionHandlerType = (JSON: AnyObject?, error: NSError?) -> Void?
13+
1214
/// Entry point in the Swift API.
1315
///
1416
/// You should instantiate a Client object with your AppID, ApiKey and Hosts
@@ -90,59 +92,58 @@ public class Client {
9092
/// :return: JSON Object in the success block in the form:
9193
/// { "items": [ {"name": "contacts", "createdAt": "2013-01-18T15:33:13.556Z"},
9294
/// {"name": "notes", "createdAt": "2013-01-18T15:33:13.556Z"}]}
93-
public func listIndexes(block: (client: Client, JSON: AnyObject?, error: NSError?) -> Void) {
94-
performHTTPQuery("1/indexes", method: .GET, body: nil, block: { (JSON, error) -> Void in
95-
block(client: self, JSON: JSON, error: error)
96-
})
95+
public func listIndexes(block: CompletionHandlerType?) {
96+
performHTTPQuery("1/indexes", method: .GET, body: nil, block: block)
9797
}
9898

9999
/// Move an existing index.
100100
///
101101
/// :param: sourceIndexName the name of index to copy.
102102
/// :param: destinationIndexName the new index name that will contains a copy of sourceIndexName (destination will be overriten if it already exist).
103-
public func moveIndex(sourceIndexName srcIndexName: String, destinationIndexName dstIndexName: String, block: (client: Client, JSON: AnyObject?, error: NSError?) -> Void) {
103+
public func moveIndex(sourceIndexName srcIndexName: String, destinationIndexName dstIndexName: String, block: CompletionHandlerType?) {
104104
let path = "1/indexes/\(srcIndexName.urlEncode())/operation"
105105
let request = [
106106
"destination": dstIndexName,
107107
"operation": "move"
108108
]
109109

110-
performHTTPQuery(path, method: .POST, body: request, block: { (JSON, error) -> Void in
111-
block(client: self, JSON: JSON, error: error)
112-
})
110+
performHTTPQuery(path, method: .POST, body: request, block: block)
113111
}
114112

115113
// MARK: - Network
116114

117115
/// Perform an HTTP Query
118-
func performHTTPQuery(path: String, method: Alamofire.Method, body: [String: AnyObject]?, index: Int = 0, block: (JSON: AnyObject?, error: NSError?) -> Void) {
116+
func performHTTPQuery(path: String, method: Alamofire.Method, body: [String: AnyObject]?, index: Int = 0, block: CompletionHandlerType?) {
119117
assert(index < hostnames.count, "\(index) < \(hostnames.count) !")
118+
120119
Alamofire.request(method, "https://\(hostnames[index])/\(path)", parameters: body).responseJSON {
121120
(request, response, data, error) -> Void in
122121
if let statusCode = response?.statusCode {
123-
switch(statusCode) {
124-
case 200, 201:
125-
block(JSON: data, error: nil)
126-
case 400:
127-
block(JSON: nil, error: NSError(domain: "Bad request argument", code: 400, userInfo: nil))
128-
case 403:
129-
block(JSON: nil, error: NSError(domain: "Invalid Application-ID or API-Key", code: 403, userInfo: nil))
130-
case 404:
131-
block(JSON: nil, error: NSError(domain: "Resource does not exist", code: 404, userInfo: nil))
132-
default:
133-
if let errorMessage = (data as [String: String])["message"] {
134-
block(JSON: nil, error: NSError(domain: errorMessage, code: 0, userInfo: nil))
135-
} else {
136-
block(JSON: nil, error: NSError(domain: "No error message", code: 0, userInfo: nil))
122+
if let block = block {
123+
switch(statusCode) {
124+
case 200, 201:
125+
block(JSON: data, error: nil)
126+
case 400:
127+
block(JSON: nil, error: NSError(domain: "Bad request argument", code: 400, userInfo: nil))
128+
case 403:
129+
block(JSON: nil, error: NSError(domain: "Invalid Application-ID or API-Key", code: 403, userInfo: nil))
130+
case 404:
131+
block(JSON: nil, error: NSError(domain: "Resource does not exist", code: 404, userInfo: nil))
132+
default:
133+
if let errorMessage = (data as [String: String])["message"] {
134+
block(JSON: nil, error: NSError(domain: errorMessage, code: 0, userInfo: nil))
135+
} else {
136+
block(JSON: nil, error: NSError(domain: "No error message", code: 0, userInfo: nil))
137+
}
137138
}
138139
}
139140
} else {
140141
if (index + 1) < self.hostnames.count {
141142
self.performHTTPQuery(path, method: method, body: body, index: index + 1, block: block)
142143
} else {
143-
block(JSON: nil, error: error)
144+
block?(JSON: nil, error: error)
144145
}
145146
}
146147
}
147148
}
148-
}
149+
}

Tests/AlgoliaSearchTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class AlgoliaSearchTests: XCTestCase {
1515

1616
override func setUp() {
1717
super.setUp()
18-
client = AlgoliaSearch.Client(appID: "XXX", apiKey: "XXX")
18+
client = AlgoliaSearch.Client(appID: "***REMOVED***", apiKey: "***REMOVED***")
1919
}
2020

2121
override func tearDown() {
@@ -25,9 +25,9 @@ class AlgoliaSearchTests: XCTestCase {
2525

2626
func testListIndexes() {
2727
let expectation = expectationWithDescription("List indexes")
28-
client.listIndexes { (client, JSON, error) -> Void in
28+
client.listIndexes { (JSON, error) -> Void in
2929
expectation.fulfill()
30-
XCTAssertNil(error, error!.localizedDescription)
30+
XCTAssertNil(error, error?.localizedDescription ?? "Error")
3131
}
3232

3333
waitForExpectationsWithTimeout(100, handler: nil)

0 commit comments

Comments
 (0)