2424import Foundation
2525import Alamofire
2626
27- // TODO: need to know for which key is the response, sometimes other information needed. Fix this.
2827
2928/// Entry point in the Swift API.
3029///
@@ -143,88 +142,68 @@ public class Client {
143142 /// List all existing indexes.
144143 ///
145144 /// :return: JSON Object in the handler in the form: { "items": [ {"name": "contacts", "createdAt": "2013-01-18T15:33:13.556Z"}, {"name": "notes", "createdAt": "2013-01-18T15:33:13.556Z"}]}
146- public func listIndexes( block: CompletionHandlerWithClient ) {
147- performHTTPQuery ( " 1/indexes " , method: . GET, body: nil , block: { ( JSON, error) -> Void in
148- block ( client: self , JSON: JSON, error: error)
149- } )
145+ public func listIndexes( block: CompletionHandler ) {
146+ performHTTPQuery ( " 1/indexes " , method: . GET, body: nil , block: block)
150147 }
151148
152149 /// Delete an index.
153150 ///
154151 /// :param: indexName the name of index to delete
155152 /// :return: JSON Object in the handler containing a "deletedAt" attribute
156- public func deleteIndex( indexName: String , block: ( ( client : Client , indexName : String , JSON : AnyObject ? , error : NSError ? ) -> Void ) ? = nil ) {
153+ public func deleteIndex( indexName: String , block: CompletionHandler ? = nil ) {
157154 let path = " 1/indexes/ \( indexName. urlEncode ( ) ) "
158- performHTTPQuery ( path, method: . DELETE, body: nil , block: { ( JSON, error) -> Void in
159- if let block = block {
160- block ( client: self , indexName: indexName, JSON: JSON, error: error)
161- }
162- } )
155+ performHTTPQuery ( path, method: . DELETE, body: nil , block: block)
163156 }
164157
165158 /// Move an existing index.
166159 ///
167160 /// :param: srcIndexName the name of index to move.
168161 /// :param: dstIndexName the new index name that will contains sourceIndexName (destination will be overriten if it already exist).
169- public func moveIndex( srcIndexName: String , dstIndexName: String , block: ( ( client : Client , srcIndexName : String , dstIndexName : String , JSON : AnyObject ? , error : NSError ? ) -> Void ) ? = nil ) {
162+ public func moveIndex( srcIndexName: String , dstIndexName: String , block: CompletionHandler ? = nil ) {
170163 let path = " 1/indexes/ \( srcIndexName. urlEncode ( ) ) /operation "
171164 let request = [
172165 " destination " : dstIndexName,
173166 " operation " : " move "
174167 ]
175168
176- performHTTPQuery ( path, method: . POST, body: request, block: { ( JSON, error) -> Void in
177- if let block = block {
178- block ( client: self , srcIndexName: srcIndexName, dstIndexName: dstIndexName, JSON: JSON, error: error)
179- }
180- } )
169+ performHTTPQuery ( path, method: . POST, body: request, block: block)
181170 }
182171
183172 /// Copy an existing index.
184173 ///
185174 /// :param: srcIndexName the name of index to copy.
186175 /// :param: dstIndexName the new index name that will contains a copy of sourceIndexName (destination will be overriten if it already exist).
187- public func copyIndex( srcIndexName: String , dstIndexName: String , block: ( ( client : Client , srcIndexName : String , dstIndexName : String , JSON : AnyObject ? , error : NSError ? ) -> Void ) ? = nil ) {
176+ public func copyIndex( srcIndexName: String , dstIndexName: String , block: CompletionHandler ? = nil ) {
188177 let path = " 1/indexes/ \( srcIndexName. urlEncode ( ) ) /operation "
189178 let request = [
190179 " destination " : dstIndexName,
191180 " operation " : " copy "
192181 ]
193182
194- performHTTPQuery ( path, method: . POST, body: request, block: { ( JSON, error) -> Void in
195- if let block = block {
196- block ( client: self , srcIndexName: srcIndexName, dstIndexName: dstIndexName, JSON: JSON, error: error)
197- }
198- } )
183+ performHTTPQuery ( path, method: . POST, body: request, block: block)
199184 }
200185
201186 /// Return 10 last log entries.
202- public func getLogs( block: CompletionHandlerWithClient ) {
203- performHTTPQuery ( " 1/logs " , method: . GET, body: nil , block: { ( JSON, error) -> Void in
204- block ( client: self , JSON: JSON, error: error)
205- } )
187+ public func getLogs( block: CompletionHandler ) {
188+ performHTTPQuery ( " 1/logs " , method: . GET, body: nil , block: block)
206189 }
207190
208191 /// Return last logs entries.
209192 ///
210193 /// :param: offset Specify the first entry to retrieve (0-based, 0 is the most recent log entry).
211194 /// :param: length Specify the maximum number of entries to retrieve starting at offset. Maximum allowed value: 1000.
212- public func getLogsWithOffset( offset: UInt , lenght: UInt , block: CompletionHandlerWithClient ) {
195+ public func getLogsWithOffset( offset: UInt , lenght: UInt , block: CompletionHandler ) {
213196 let path = " 1/logs?offset= \( offset) &lenght= \( lenght) "
214- performHTTPQuery ( path, method: . GET, body: nil , block: { ( JSON, error) -> Void in
215- block ( client: self , JSON: JSON, error: error)
216- } )
197+ performHTTPQuery ( path, method: . GET, body: nil , block: block)
217198 }
218199
219200 /// Return last logs entries.
220201 ///
221202 /// :param: offset Specify the first entry to retrieve (0-based, 0 is the most recent log entry).
222203 /// :param: length Specify the maximum number of entries to retrieve starting at offset. Maximum allowed value: 1000.
223- public func getLogsWithType( type: String , offset: UInt , lenght: UInt , block: CompletionHandlerWithClient ) {
204+ public func getLogsWithType( type: String , offset: UInt , lenght: UInt , block: CompletionHandler ) {
224205 let path = " 1/logs?offset= \( offset) &lenght= \( lenght) &type= \( type) "
225- performHTTPQuery ( path, method: . GET, body: nil , block: { ( JSON, error) -> Void in
226- block ( client: self , JSON: JSON, error: error)
227- } )
206+ performHTTPQuery ( path, method: . GET, body: nil , block: block)
228207 }
229208
230209 /// Get the index object initialized (no server call needed for initialization).
@@ -235,40 +214,28 @@ public class Client {
235214 }
236215
237216 /// List all existing user keys with their associated ACLs.
238- public func listUserKeys( block: CompletionHandlerWithClient ) {
239- performHTTPQuery ( " 1/keys " , method: . GET, body: nil , block: { ( JSON, error) -> Void in
240- block ( client: self , JSON: JSON, error: error)
241- } )
217+ public func listUserKeys( block: CompletionHandler ) {
218+ performHTTPQuery ( " 1/keys " , method: . GET, body: nil , block: block)
242219 }
243220
244221 /// Get ACL of a user key.
245- public func getUserKeyACL( key: String , block: CompletionHandlerWithKey ) {
222+ public func getUserKeyACL( key: String , block: CompletionHandler ) {
246223 let path = " 1/keys/ \( key) "
247- performHTTPQuery ( path, method: . GET, body: nil , block: { ( JSON, error) -> Void in
248- block ( client: self , key: key, JSON: JSON, error: error)
249- } )
224+ performHTTPQuery ( path, method: . GET, body: nil , block: block)
250225 }
251226
252227 /// Delete an existing user key.
253- public func deleteUserKey( key: String , block: CompletionHandlerWithKey ? = nil ) {
228+ public func deleteUserKey( key: String , block: CompletionHandler ? = nil ) {
254229 let path = " 1/keys/ \( key) "
255- performHTTPQuery ( path, method: . DELETE, body: nil , block: { ( JSON, error) -> Void in
256- if let block = block {
257- block ( client: self , key: key, JSON: JSON, error: error)
258- }
259- } )
230+ performHTTPQuery ( path, method: . DELETE, body: nil , block: block)
260231 }
261232
262233 /// Create a new user key
263234 ///
264235 /// :param: acls The list of ACL for this key. The list can contains the following values (as String): search, addObject, deleteObject, deleteIndex, settings, editSettings
265- public func addUserKey( acls: [ String ] , block: CompletionHandlerWithACLs ? = nil ) {
236+ public func addUserKey( acls: [ String ] , block: CompletionHandler ? = nil ) {
266237 let request = [ " acl " : acls]
267- performHTTPQuery ( " 1/keys " , method: . POST, body: request, block: { ( JSON, error) -> Void in
268- if let block = block {
269- block ( client: self , acls: acls, JSON: JSON, error: error)
270- }
271- } )
238+ performHTTPQuery ( " 1/keys " , method: . POST, body: request, block: block)
272239 }
273240
274241 /// Create a new user key
@@ -277,19 +244,15 @@ public class Client {
277244 /// :param: withValidity The number of seconds after which the key will be automatically removed (0 means no time limit for this key)
278245 /// :param: maxQueriesPerIPPerHour Specify the maximum number of API calls allowed from an IP address per hour. Defaults to 0 (unlimited).
279246 /// :param: maxHitsPerQuery Specify the maximum number of hits this API key can retrieve in one call. Defaults to 0 (unlimited)
280- public func addUserKey( acls: [ String ] , withValidity validity: UInt , maxQueriesPerIPPerHour maxQueries: UInt , maxHitsPerQuery maxHits: UInt , block: CompletionHandlerWithACLs ? = nil ) {
247+ public func addUserKey( acls: [ String ] , withValidity validity: UInt , maxQueriesPerIPPerHour maxQueries: UInt , maxHitsPerQuery maxHits: UInt , block: CompletionHandler ? = nil ) {
281248 let request : [ String : AnyObject ] = [
282249 " acl " : acls,
283250 " validity " : validity,
284251 " maxQueriesPerIPPerHour " : maxQueries,
285252 " maxHitsPerQuery " : maxHits,
286253 ]
287254
288- performHTTPQuery ( " 1/keys " , method: . POST, body: request, block: { ( JSON, error) -> Void in
289- if let block = block {
290- block ( client: self , acls: acls, JSON: JSON, error: error)
291- }
292- } )
255+ performHTTPQuery ( " 1/keys " , method: . POST, body: request, block: block)
293256 }
294257
295258 /// Create a new user key
@@ -299,7 +262,7 @@ public class Client {
299262 /// :param: withValidity The number of seconds after which the key will be automatically removed (0 means no time limit for this key)
300263 /// :param: maxQueriesPerIPPerHour Specify the maximum number of API calls allowed from an IP address per hour. Defaults to 0 (unlimited).
301264 /// :param: maxHitsPerQuery Specify the maximum number of hits this API key can retrieve in one call. Defaults to 0 (unlimited)
302- public func addUserKey( acls: [ String ] , forIndexes indexes: [ String ] , withValidity validity: UInt , maxQueriesPerIPPerHour maxQueries: UInt , maxHitsPerQuery maxHits: UInt , block: ( ( client : Client , acls : [ String ] , indexes : [ String ] , JSON : AnyObject ? , error : NSError ? ) -> Void ) ? = nil ) {
265+ public func addUserKey( acls: [ String ] , forIndexes indexes: [ String ] , withValidity validity: UInt , maxQueriesPerIPPerHour maxQueries: UInt , maxHitsPerQuery maxHits: UInt , block: CompletionHandler ? = nil ) {
303266 let request : [ String : AnyObject ] = [
304267 " acl " : acls,
305268 " indexes " : indexes,
@@ -308,25 +271,17 @@ public class Client {
308271 " maxHitsPerQuery " : maxHits,
309272 ]
310273
311- performHTTPQuery ( " 1/keys " , method: . POST, body: request, block: { ( JSON, error) -> Void in
312- if let block = block {
313- block ( client: self , acls: acls, indexes: indexes, JSON: JSON, error: error)
314- }
315- } )
274+ performHTTPQuery ( " 1/keys " , method: . POST, body: request, block: block)
316275 }
317276
318277 /// Update a user key
319278 ///
320279 /// :param: key The key to update
321280 /// :param: withAcls The list of ACL for this key. The list can contains the following values (as String): search, addObject, deleteObject, deleteIndex, settings, editSettings
322- public func updateUserKey( key: String , withACL acls: [ String ] , block: CompletionHandlerWithKeyAndACLs ? = nil ) {
281+ public func updateUserKey( key: String , withACL acls: [ String ] , block: CompletionHandler ? = nil ) {
323282 let path = " 1/keys/ \( key) "
324283 let request = [ " acl " : acls]
325- performHTTPQuery ( path, method: . PUT, body: request, block: { ( JSON, error) -> Void in
326- if let block = block {
327- block ( client: self , key: key, acls: acls, JSON: JSON, error: error)
328- }
329- } )
284+ performHTTPQuery ( path, method: . PUT, body: request, block: block)
330285 }
331286
332287 /// Update a user key
@@ -336,7 +291,7 @@ public class Client {
336291 /// :param: andValidity The number of seconds after which the key will be automatically removed (0 means no time limit for this key)
337292 /// :param: maxQueriesPerIPPerHour Specify the maximum number of API calls allowed from an IP address per hour. Defaults to 0 (unlimited).
338293 /// :param: maxHitsPerQuery Specify the maximum number of hits this API key can retrieve in one call. Defaults to 0 (unlimited)
339- public func updateUserKey( key: String , withACL acls: [ String ] , andValidity validity: UInt , maxQueriesPerIPPerHour maxQueries: UInt , maxHitsPerQuery maxHits: UInt , block: CompletionHandlerWithKeyAndACLs ? = nil ) {
294+ public func updateUserKey( key: String , withACL acls: [ String ] , andValidity validity: UInt , maxQueriesPerIPPerHour maxQueries: UInt , maxHitsPerQuery maxHits: UInt , block: CompletionHandler ? = nil ) {
340295 let path = " 1/keys/ \( key) "
341296 let request : [ String : AnyObject ] = [
342297 " acl " : acls,
@@ -345,11 +300,7 @@ public class Client {
345300 " maxHitsPerQuery " : maxHits,
346301 ]
347302
348- performHTTPQuery ( path, method: . PUT, body: request, block: { ( JSON, error) -> Void in
349- if let block = block {
350- block ( client: self , key: key, acls: acls, JSON: JSON, error: error)
351- }
352- } )
303+ performHTTPQuery ( path, method: . PUT, body: request, block: block)
353304 }
354305
355306 /// Update a user key
@@ -360,7 +311,7 @@ public class Client {
360311 /// :param: forIndexes restrict this API key to specific index names
361312 /// :param: maxQueriesPerIPPerHour Specify the maximum number of API calls allowed from an IP address per hour. Defaults to 0 (unlimited).
362313 /// :param: maxHitsPerQuery Specify the maximum number of hits this API key can retrieve in one call. Defaults to 0 (unlimited)
363- public func updateUserKey( key: String , withACL acls: [ String ] , andValidity validity: UInt , forIndexes indexes: [ String ] , maxQueriesPerIPPerHour maxQueries: UInt , maxHitsPerQuery maxHits: UInt , block: ( ( client : Client , key : String , acls : [ String ] , indexes : [ String ] , JSON : AnyObject ? , error : NSError ? ) -> Void ) ? = nil ) {
314+ public func updateUserKey( key: String , withACL acls: [ String ] , andValidity validity: UInt , forIndexes indexes: [ String ] , maxQueriesPerIPPerHour maxQueries: UInt , maxHitsPerQuery maxHits: UInt , block: CompletionHandler ? = nil ) {
364315 let path = " 1/keys/ \( key) "
365316 let request : [ String : AnyObject ] = [
366317 " acl " : acls,
@@ -370,17 +321,13 @@ public class Client {
370321 " maxHitsPerQuery " : maxHits,
371322 ]
372323
373- performHTTPQuery ( path, method: . PUT, body: request, block: { ( JSON, error) -> Void in
374- if let block = block {
375- block ( client: self , key: key, acls: acls, indexes: indexes, JSON: JSON, error: error)
376- }
377- } )
324+ performHTTPQuery ( path, method: . PUT, body: request, block: block)
378325 }
379326
380327 /// Query multiple indexes with one API call.
381328 ///
382329 /// :param: queries An array of queries with the associated index (Array of Dictionnary object ["indexName": "targettedIndex", "query": "theASQuery"]).
383- public func multipleQueries( queries: [ AnyObject ] , block: ( ( client : Client , queries : [ AnyObject ] , JSON : AnyObject ? , error : NSError ? ) -> Void ) ? = nil ) {
330+ public func multipleQueries( queries: [ AnyObject ] , block: CompletionHandler ? = nil ) {
384331 let path = " 1/indexes/*/queries "
385332
386333 var convertedQueries = [ AnyObject] ( )
@@ -395,11 +342,7 @@ public class Client {
395342 }
396343
397344 let request = [ " requests " : convertedQueries]
398- performHTTPQuery ( path, method: . POST, body: request, block: { ( JSON, error) -> Void in
399- if let block = block {
400- block ( client: self , queries: queries, JSON: JSON, error: error)
401- }
402- } )
345+ performHTTPQuery ( path, method: . POST, body: request, block: block)
403346 }
404347
405348 // MARK: - Network
0 commit comments