@@ -25,8 +25,7 @@ import Foundation
2525import Alamofire
2626
2727// TODO: need to know for which key is the response, sometimes other information needed. Fix this.
28-
29- public typealias CompletionHandlerType = ( JSON: AnyObject ? , error: NSError ? ) -> Void ?
28+ // TODO: is it really safe to change the appID? I mean, the hostname will not change...
3029
3130/// Entry point in the Swift API.
3231///
@@ -73,6 +72,8 @@ public class Client {
7372
7473 let hostnames : [ String ]
7574
75+ private var requestBuffer = RingBuffer < Alamofire . Request > ( maxCapacity: 10 )
76+
7677 /// Algolia Search initialization
7778 ///
7879 /// :param: appID the application ID you have in your admin interface
@@ -128,7 +129,11 @@ public class Client {
128129 Alamofire . Manager. sharedInstance. session. configuration. HTTPAdditionalHeaders = HTTPHeader
129130 }
130131
131- func setExtraHeader( value: String , forKey key: String ) {
132+ /// Allow to set custom extra header
133+ ///
134+ /// :param: value value of the header
135+ /// :param: forKey key of the header
136+ public func setExtraHeader( value: String , forKey key: String ) {
132137 if ( Alamofire . Manager. sharedInstance. session. configuration. HTTPAdditionalHeaders != nil ) {
133138 Alamofire . Manager. sharedInstance. session. configuration. HTTPAdditionalHeaders!. updateValue ( value, forKey: key)
134139 } else {
@@ -208,15 +213,18 @@ public class Client {
208213 performHTTPQuery ( path, method: . GET, body: nil , block: block)
209214 }
210215
216+ /// List all existing user keys with their associated ACLs
211217 public func listUserKeys( block: CompletionHandlerType ) {
212218 performHTTPQuery ( " 1/keys " , method: . GET, body: nil , block: block)
213219 }
214220
221+ /// Get ACL of a user key
215222 public func getUserKeyACL( key: String , block: CompletionHandlerType ) {
216223 let path = " 1/keys/ \( key) "
217224 performHTTPQuery ( path, method: . GET, body: nil , block: block)
218225 }
219226
227+ /// Delete an existing user key
220228 public func deleteUserKey( key: String , block: CompletionHandlerType ? = nil ) {
221229 let path = " 1/keys/ \( key) "
222230 performHTTPQuery ( path, method: . DELETE, body: nil , block: block)
@@ -281,17 +289,41 @@ public class Client {
281289 performHTTPQuery ( path, method: . PUT, body: request, block: block)
282290 }
283291
292+ /// Get the index object initialized (no server call needed for initialization)
293+ ///
294+ /// :param: indexName the name of index
284295 public func getIndex( indexName: String ) -> Index {
285296 return Index ( client: self , indexName: indexName)
286297 }
287298
299+ /// Query multiple indexes with one API call
300+ ///
301+ /// :param: queries An array of queries with the associated index (Array of Dictionnary object ["indexName": "targettedIndex", "query": "theASQuery"]).
302+ public func multipleQueries( queries: [ AnyObject ] , block: CompletionHandlerType ? ) {
303+ let path = " 1/indexes/*/queries "
304+
305+ var convertedQueries = [ AnyObject] ( )
306+ convertedQueries. reserveCapacity ( queries. count)
307+ for query in queries {
308+ if let query = query as? [ String : String ] {
309+ convertedQueries. append ( [
310+ " params " : query [ " query " ] !. urlEncode ( ) ,
311+ " indexName " : query [ " indexName " ] !
312+ ] )
313+ }
314+ }
315+
316+ let request = [ " requests " : convertedQueries]
317+ performHTTPQuery ( path, method: . POST, body: request, block: block)
318+ }
319+
288320 // MARK: - Network
289321
290322 /// Perform an HTTP Query
291323 func performHTTPQuery( path: String , method: Alamofire . Method , body: [ String : AnyObject ] ? , index: Int = 0 , block: CompletionHandlerType ? = nil ) {
292324 assert ( index < hostnames. count, " \( index) < \( hostnames. count) ! " )
293325
294- Alamofire . request ( method, " https:// \( hostnames [ index] ) / \( path) " , parameters: body) . responseJSON {
326+ let request = Alamofire . request ( method, " https:// \( hostnames [ index] ) / \( path) " , parameters: body) . responseJSON {
295327 ( request, response, data, error) -> Void in
296328 if let statusCode = response? . statusCode {
297329 if let block = block {
@@ -320,5 +352,17 @@ public class Client {
320352 }
321353 }
322354 }
355+
356+ requestBuffer. append ( request)
357+ }
358+
359+ func cancelQueries( method: Alamofire . Method , path: String ) {
360+ for request in requestBuffer {
361+ if request. request. URL. path == path {
362+ if request. request. HTTPMethod == method. rawValue {
363+ request. cancel ( )
364+ }
365+ }
366+ }
323367 }
324368}
0 commit comments