@@ -33,33 +33,62 @@ public typealias CompletionHandlerType = (JSON: AnyObject?, error: NSError?) ->
3333/// You should instantiate a Client object with your AppID, ApiKey and Hosts
3434/// to start using Algolia Search API
3535public class Client {
36- public let appID : String
37- public let apiKey : String
38- public let tagFilters : String ?
39- public let userToken : String ?
40- public let hostnames : [ String ]
36+ public var appID : String {
37+ didSet {
38+ setExtraHeader ( appID, forKey: " X-Algolia-Application-Id " )
39+ }
40+ }
41+
42+ public var apiKey : String {
43+ didSet {
44+ setExtraHeader ( apiKey, forKey: " X-Algolia-API-Key " )
45+ }
46+ }
47+
48+ public var tagFilters : String ? {
49+ didSet {
50+ if let tagFilters = tagFilters {
51+ setExtraHeader ( tagFilters, forKey: " X-Algolia-TagFilters " )
52+ } else {
53+ Alamofire . Manager. sharedInstance. session. configuration. HTTPAdditionalHeaders? . removeValueForKey ( " X-Algolia-TagFilters " )
54+ }
55+ }
56+ }
57+
58+ public var userToken : String ? {
59+ didSet {
60+ if let userToken = userToken {
61+ setExtraHeader ( userToken, forKey: " X-Algolia-UserToken " )
62+ } else {
63+ Alamofire . Manager. sharedInstance. session. configuration. HTTPAdditionalHeaders? . removeValueForKey ( " X-Algolia-UserToken " )
64+ }
65+ }
66+ }
4167
4268 public var timeout : NSTimeInterval = 30 {
4369 didSet {
4470 Alamofire . Manager. sharedInstance. session. configuration. timeoutIntervalForRequest = timeout;
4571 }
4672 }
4773
74+ let hostnames : [ String ]
75+
4876 /// Algolia Search initialization
4977 ///
5078 /// :param: appID the application ID you have in your admin interface
5179 /// :param: apiKey a valid API key for the service
5280 /// :param: hostnames the list of hosts that you have received for the service
81+ /// :param: dsn set to true if your account has the Distributed Search Option
82+ /// :param: dsnHost the host that you have received for the Distributed Search Option
5383 /// :param: tagFilters value of the header X-Algolia-TagFilters
5484 /// :param: userToken value of the header X-Algolia-UserToken
55- public init ( appID: String , apiKey: String , hostnames: [ String ] ? = nil , tagFilters: String ? = nil , userToken: String ? = nil ) {
85+ public init ( appID: String , apiKey: String , hostnames: [ String ] ? = nil , dsn : Bool = false , dsnHost : String ? = nil , tagFilters: String ? = nil , userToken: String ? = nil ) {
5686 if countElements ( appID) == 0 {
5787 NSException ( name: " InvalidArgument " , reason: " Application ID must be set " , userInfo: nil ) . raise ( )
5888 } else if countElements ( apiKey) == 0 {
5989 NSException ( name: " InvalidArgument " , reason: " APIKey must be set " , userInfo: nil ) . raise ( )
6090 }
6191
62- // TODO: dsn, dsnhost?
6392 self . appID = appID
6493 self . apiKey = apiKey
6594 self . tagFilters = tagFilters
@@ -76,6 +105,12 @@ public class Client {
76105 }
77106 self . hostnames. shuffle ( )
78107
108+ if let dsnHost = dsnHost {
109+ self . hostnames. insert ( dsnHost, atIndex: 0 )
110+ } else {
111+ self . hostnames. insert ( " \( appID) -dsn.algolia.net " , atIndex: 0 )
112+ }
113+
79114 let version = NSBundle ( identifier: " com.algolia.AlgoliaSearch " ) !. infoDictionary![ " CFBundleShortVersionString " ] as String
80115 var HTTPHeader = [
81116 " X-Algolia-API-Key " : self . apiKey,
@@ -93,7 +128,7 @@ public class Client {
93128 Alamofire . Manager. sharedInstance. session. configuration. HTTPAdditionalHeaders = HTTPHeader
94129 }
95130
96- public func setExtraHeader( value: String , forKey key: String ) {
131+ func setExtraHeader( value: String , forKey key: String ) {
97132 if ( Alamofire . Manager. sharedInstance. session. configuration. HTTPAdditionalHeaders != nil ) {
98133 Alamofire . Manager. sharedInstance. session. configuration. HTTPAdditionalHeaders!. updateValue ( value, forKey: key)
99134 } else {
@@ -246,6 +281,10 @@ public class Client {
246281 performHTTPQuery ( path, method: . PUT, body: request, block: block)
247282 }
248283
284+ public func getIndex( indexName: String ) -> Index {
285+ return Index ( client: self , indexName: indexName)
286+ }
287+
249288 // MARK: - Network
250289
251290 /// Perform an HTTP Query
0 commit comments