Skip to content

Commit 5851ee6

Browse files
committed
rename RemoteIndex to Index + add computed properties + add others API
1 parent 07ec66b commit 5851ee6

File tree

3 files changed

+53
-14
lines changed

3 files changed

+53
-14
lines changed

AlgoliaSearch.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
5D9F70821A93B4BE004B751B /* Alamofire.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5D9F70811A93B4BE004B751B /* Alamofire.framework */; };
1616
5D9F70851A93B9C9004B751B /* Alamofire.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5D9F70811A93B4BE004B751B /* Alamofire.framework */; };
1717
5D9F70871A93BE2A004B751B /* Alamofire.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 5D9F70811A93B4BE004B751B /* Alamofire.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
18-
5DECA2DA1A960BC5001A6088 /* RemoteIndex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5DECA2D91A960BC5001A6088 /* RemoteIndex.swift */; };
18+
5DECA2DA1A960BC5001A6088 /* Index.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5DECA2D91A960BC5001A6088 /* Index.swift */; };
1919
/* End PBXBuildFile section */
2020

2121
/* Begin PBXContainerItemProxy section */
@@ -51,7 +51,7 @@
5151
5D7495A01A8E277400B0263F /* Client.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Client.swift; sourceTree = "<group>"; };
5252
5D7495A61A8E499B00B0263F /* Query.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Query.swift; sourceTree = "<group>"; };
5353
5D9F70811A93B4BE004B751B /* Alamofire.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Alamofire.framework; path = Carthage/Build/Mac/Alamofire.framework; sourceTree = "<group>"; };
54-
5DECA2D91A960BC5001A6088 /* RemoteIndex.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RemoteIndex.swift; sourceTree = "<group>"; };
54+
5DECA2D91A960BC5001A6088 /* Index.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Index.swift; sourceTree = "<group>"; };
5555
/* End PBXFileReference section */
5656

5757
/* Begin PBXFrameworksBuildPhase section */
@@ -99,7 +99,7 @@
9999
children = (
100100
5D7495A01A8E277400B0263F /* Client.swift */,
101101
5D7495A61A8E499B00B0263F /* Query.swift */,
102-
5DECA2D91A960BC5001A6088 /* RemoteIndex.swift */,
102+
5DECA2D91A960BC5001A6088 /* Index.swift */,
103103
5D74959F1A8E277400B0263F /* Extension.swift */,
104104
5D7495811A8E25A600B0263F /* Supporting Files */,
105105
);
@@ -238,7 +238,7 @@
238238
isa = PBXSourcesBuildPhase;
239239
buildActionMask = 2147483647;
240240
files = (
241-
5DECA2DA1A960BC5001A6088 /* RemoteIndex.swift in Sources */,
241+
5DECA2DA1A960BC5001A6088 /* Index.swift in Sources */,
242242
5D7495A21A8E277400B0263F /* Client.swift in Sources */,
243243
5D7495A71A8E499B00B0263F /* Query.swift in Sources */,
244244
5D7495A11A8E277400B0263F /* Extension.swift in Sources */,

Source/Client.swift

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3535
public 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

Source/RemoteIndex.swift renamed to Source/Index.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Foundation
1111
/// Contains all the functions related to one index
1212
///
1313
/// You can use Client.getIndex(indexName) to retrieve this object
14-
public class RemoteIndex {
14+
public class Index {
1515
let indexName: String
1616
let client: Client
1717
let urlEncodedIndexName: String
@@ -220,7 +220,7 @@ public class RemoteIndex {
220220
client.performHTTPQuery(path, method: .PUT, body: request, block: block)
221221
}
222222

223-
public func updateUserKey(key: Sring, withACL acls: [String], withValidity validity: UInt, maxQueriesPerIPPerHour maxQueries: UInt, maxHitsPerQuery maxHits: UInt, block: CompletionHandlerType? = nil) {
223+
public func updateUserKey(key: String, withACL acls: [String], withValidity validity: UInt, maxQueriesPerIPPerHour maxQueries: UInt, maxHitsPerQuery maxHits: UInt, block: CompletionHandlerType? = nil) {
224224
let path = "1/indexes/\(urlEncodedIndexName)/keys/\(key.urlEncode())"
225225
let request: [String: AnyObject] = [
226226
"acl": acls,

0 commit comments

Comments
 (0)