99import Foundation
1010import 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+ }
0 commit comments