Skip to content

Commit 07ec66b

Browse files
committed
add RemoteIndexes class with most of the stuff already implemented
1 parent 8ef2240 commit 07ec66b

File tree

4 files changed

+309
-42
lines changed

4 files changed

+309
-42
lines changed

AlgoliaSearch.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +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 */; };
1819
/* End PBXBuildFile section */
1920

2021
/* Begin PBXContainerItemProxy section */
@@ -50,6 +51,7 @@
5051
5D7495A01A8E277400B0263F /* Client.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Client.swift; sourceTree = "<group>"; };
5152
5D7495A61A8E499B00B0263F /* Query.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Query.swift; sourceTree = "<group>"; };
5253
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>"; };
5355
/* End PBXFileReference section */
5456

5557
/* Begin PBXFrameworksBuildPhase section */
@@ -97,6 +99,7 @@
9799
children = (
98100
5D7495A01A8E277400B0263F /* Client.swift */,
99101
5D7495A61A8E499B00B0263F /* Query.swift */,
102+
5DECA2D91A960BC5001A6088 /* RemoteIndex.swift */,
100103
5D74959F1A8E277400B0263F /* Extension.swift */,
101104
5D7495811A8E25A600B0263F /* Supporting Files */,
102105
);
@@ -235,6 +238,7 @@
235238
isa = PBXSourcesBuildPhase;
236239
buildActionMask = 2147483647;
237240
files = (
241+
5DECA2DA1A960BC5001A6088 /* RemoteIndex.swift in Sources */,
238242
5D7495A21A8E277400B0263F /* Client.swift in Sources */,
239243
5D7495A71A8E499B00B0263F /* Query.swift in Sources */,
240244
5D7495A11A8E277400B0263F /* Extension.swift in Sources */,

Source/Client.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,15 @@ public class Client {
109109
/// :return: JSON Object in the block in the form:
110110
/// { "items": [ {"name": "contacts", "createdAt": "2013-01-18T15:33:13.556Z"},
111111
/// {"name": "notes", "createdAt": "2013-01-18T15:33:13.556Z"}]}
112-
public func listIndexes(block: CompletionHandlerType?) {
112+
public func listIndexes(block: CompletionHandlerType? = nil) {
113113
performHTTPQuery("1/indexes", method: .GET, body: nil, block: block)
114114
}
115115

116116
/// Move an existing index.
117117
///
118118
/// :param: srcIndexName the name of index to move.
119119
/// :param: dstIndexName the new index name that will contains sourceIndexName (destination will be overriten if it already exist).
120-
public func moveIndex(srcIndexName: String, dstIndexName: String, block: CompletionHandlerType?) {
120+
public func moveIndex(srcIndexName: String, dstIndexName: String, block: CompletionHandlerType? = nil) {
121121
let path = "1/indexes/\(srcIndexName.urlEncode())/operation"
122122
let request = [
123123
"destination": dstIndexName,
@@ -131,7 +131,7 @@ public class Client {
131131
///
132132
/// :param: srcIndexName the name of index to copy.
133133
/// :param: dstIndexName the new index name that will contains a copy of sourceIndexName (destination will be overriten if it already exist).
134-
public func copyIndex(srcIndexName: String, dstIndexName: String, block: CompletionHandlerType?) {
134+
public func copyIndex(srcIndexName: String, dstIndexName: String, block: CompletionHandlerType? = nil) {
135135
let path = "1/indexes/\(srcIndexName.urlEncode())/operation"
136136
let request = [
137137
"destination": dstIndexName,
@@ -145,7 +145,7 @@ public class Client {
145145
///
146146
/// :param: indexName the name of index to delete
147147
/// :return: JSON Object in the block in the form: { "deletedAt": "2013-01-18T15:33:13.556Z", "taskID": 721 }
148-
public func deleteIndex(indexName: String, block: CompletionHandlerType?) {
148+
public func deleteIndex(indexName: String, block: CompletionHandlerType? = nil) {
149149
let path = "1/indexes/\(indexName.urlEncode())"
150150
performHTTPQuery(path, method: .DELETE, body: nil, block: block)
151151
}
@@ -182,17 +182,17 @@ public class Client {
182182
performHTTPQuery(path, method: .GET, body: nil, block: block)
183183
}
184184

185-
public func deleteUserKey(key: String, block: CompletionHandlerType?) {
185+
public func deleteUserKey(key: String, block: CompletionHandlerType? = nil) {
186186
let path = "1/keys/\(key)"
187187
performHTTPQuery(path, method: .DELETE, body: nil, block: block)
188188
}
189189

190-
public func addUserKey(acls: [String], block: CompletionHandlerType?) {
190+
public func addUserKey(acls: [String], block: CompletionHandlerType? = nil) {
191191
let request = ["acl": acls]
192192
performHTTPQuery("1/keys", method: .POST, body: request, block: block)
193193
}
194194

195-
public func addUserKey(acls: [String], withValidity validity: UInt, maxQueriesPerIPPerHour maxQueries: UInt, maxHitsPerQuery maxHits: UInt, block: CompletionHandlerType?) {
195+
public func addUserKey(acls: [String], withValidity validity: UInt, maxQueriesPerIPPerHour maxQueries: UInt, maxHitsPerQuery maxHits: UInt, block: CompletionHandlerType? = nil) {
196196
let request: [String: AnyObject] = [
197197
"acl": acls,
198198
"validity": validity,
@@ -203,7 +203,7 @@ public class Client {
203203
performHTTPQuery("1/keys", method: .POST, body: request, block: block)
204204
}
205205

206-
public func addUserKey(acls: [String], withIndexes indexes: [String], withValidity validity: UInt, maxQueriesPerIPPerHour maxQueries: UInt, maxHitsPerQuery maxHits: UInt, block: CompletionHandlerType?) {
206+
public func addUserKey(acls: [String], withIndexes indexes: [String], withValidity validity: UInt, maxQueriesPerIPPerHour maxQueries: UInt, maxHitsPerQuery maxHits: UInt, block: CompletionHandlerType? = nil) {
207207
let request: [String: AnyObject] = [
208208
"acl": acls,
209209
"indexes": indexes,
@@ -215,13 +215,13 @@ public class Client {
215215
performHTTPQuery("1/keys", method: .POST, body: request, block: block)
216216
}
217217

218-
public func updateUserKey(key: String, withACL acls: [String], block: CompletionHandlerType?) {
218+
public func updateUserKey(key: String, withACL acls: [String], block: CompletionHandlerType? = nil) {
219219
let path = "1/keys/\(key)"
220220
let request = ["acl": acls]
221221
performHTTPQuery(path, method: .PUT, body: request, block: block)
222222
}
223223

224-
public func updateUserKey(key: String, withACL acls: [String], andValidity validity: UInt, maxQueriesPerIPPerHour maxQueries: UInt, maxHitsPerQuery maxHits: UInt, block: CompletionHandlerType?) {
224+
public func updateUserKey(key: String, withACL acls: [String], andValidity validity: UInt, maxQueriesPerIPPerHour maxQueries: UInt, maxHitsPerQuery maxHits: UInt, block: CompletionHandlerType? = nil) {
225225
let path = "1/keys/\(key)"
226226
let request: [String: AnyObject] = [
227227
"acl": acls,
@@ -233,7 +233,7 @@ public class Client {
233233
performHTTPQuery(path, method: .PUT, body: request, block: block)
234234
}
235235

236-
public func updateUserKey(key: String, withACL acls: [String], withIndexes indexes: [String], andValidity validity: UInt, maxQueriesPerIPPerHour maxQueries: UInt, maxHitsPerQuery maxHits: UInt, block: CompletionHandlerType?) {
236+
public func updateUserKey(key: String, withACL acls: [String], withIndexes indexes: [String], andValidity validity: UInt, maxQueriesPerIPPerHour maxQueries: UInt, maxHitsPerQuery maxHits: UInt, block: CompletionHandlerType? = nil) {
237237
let path = "1/keys/\(key)"
238238
let request: [String: AnyObject] = [
239239
"acl": acls,
@@ -249,7 +249,7 @@ public class Client {
249249
// MARK: - Network
250250

251251
/// Perform an HTTP Query
252-
func performHTTPQuery(path: String, method: Alamofire.Method, body: [String: AnyObject]?, index: Int = 0, block: CompletionHandlerType?) {
252+
func performHTTPQuery(path: String, method: Alamofire.Method, body: [String: AnyObject]?, index: Int = 0, block: CompletionHandlerType? = nil) {
253253
assert(index < hostnames.count, "\(index) < \(hostnames.count) !")
254254

255255
Alamofire.request(method, "https://\(hostnames[index])/\(path)", parameters: body).responseJSON {

Source/Query.swift

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -123,77 +123,77 @@ public class Query : Printable {
123123
public func buildURL() -> String {
124124
var url = [String]()
125125
if let attributesToRetrieve = attributesToRetrieve {
126-
url.append(encodeForQuery(attributesToRetrieve, key: "attributes"))
126+
url.append(Query.encodeForQuery(attributesToRetrieve, withKey: "attributes"))
127127
}
128128
if let attributesToHighlight = attributesToHighlight {
129-
url.append(encodeForQuery(attributesToHighlight, key: "attributesToHighlight"))
129+
url.append(Query.encodeForQuery(attributesToHighlight, withKey: "attributesToHighlight"))
130130
}
131131
if let attributesToSnippet = attributesToSnippet {
132-
url.append(encodeForQuery(attributesToSnippet, key: "attributesToSnippet"))
132+
url.append(Query.encodeForQuery(attributesToSnippet, withKey: "attributesToSnippet"))
133133
}
134134

135135
if let facetFilters = facetFilters {
136136
// TODO: complete code (JSON)
137137
} else if let facetFiltersRaw = facetFiltersRaw {
138-
url.append(encodeForQuery(facetFiltersRaw, key: "facetFilters="))
138+
url.append(Query.encodeForQuery(facetFiltersRaw, withKey: "facetFilters="))
139139
}
140140

141141
if let facets = facets {
142-
url.append(encodeForQuery(facets, key: "facets"))
142+
url.append(Query.encodeForQuery(facets, withKey: "facets"))
143143
}
144144
if let optionalWords = optionalWords {
145-
url.append(encodeForQuery(optionalWords, key: "optionalWords"))
145+
url.append(Query.encodeForQuery(optionalWords, withKey: "optionalWords"))
146146
}
147147
if optionalWordsMinimumMatched > 0 {
148-
url.append(encodeForQuery(optionalWordsMinimumMatched, key: "optionalWordsMinimumMatched"))
148+
url.append(Query.encodeForQuery(optionalWordsMinimumMatched, withKey: "optionalWordsMinimumMatched"))
149149
}
150150
if minWordSizeForApprox1 != 3 {
151-
url.append(encodeForQuery(minWordSizeForApprox1, key: "minWordSizefor1Typo"))
151+
url.append(Query.encodeForQuery(minWordSizeForApprox1, withKey: "minWordSizefor1Typo"))
152152
}
153153
if minWordSizeForApprox2 != 7 {
154-
url.append(encodeForQuery(minWordSizeForApprox2, key: "minWordSizefor2Typos"))
154+
url.append(Query.encodeForQuery(minWordSizeForApprox2, withKey: "minWordSizefor2Typos"))
155155
}
156156
if ignorePlural {
157-
url.append(encodeForQuery(ignorePlural, key: "ignorePlural"))
157+
url.append(Query.encodeForQuery(ignorePlural, withKey: "ignorePlural"))
158158
}
159159
if getRankingInfo {
160-
url.append(encodeForQuery(getRankingInfo, key: "getRankingInfo"))
160+
url.append(Query.encodeForQuery(getRankingInfo, withKey: "getRankingInfo"))
161161
}
162162
if !typosOnNumericTokens { // default True
163-
url.append(encodeForQuery(typosOnNumericTokens, key: "allowTyposOnNumericTokens"))
163+
url.append(Query.encodeForQuery(typosOnNumericTokens, withKey: "allowTyposOnNumericTokens"))
164164
}
165165
if let typoTolerance = typoTolerance {
166-
url.append(encodeForQuery(typoTolerance, key: "typoTolerance"))
166+
url.append(Query.encodeForQuery(typoTolerance, withKey: "typoTolerance"))
167167
}
168168
if distinct {
169-
url.append(encodeForQuery(distinct, key: "distinct"))
169+
url.append(Query.encodeForQuery(distinct, withKey: "distinct"))
170170
}
171171
if !analytics { // default True
172-
url.append(encodeForQuery(analytics, key: "analytics"))
172+
url.append(Query.encodeForQuery(analytics, withKey: "analytics"))
173173
}
174174
if !synonyms { // default True
175-
url.append(encodeForQuery(synonyms, key: "synonyms"))
175+
url.append(Query.encodeForQuery(synonyms, withKey: "synonyms"))
176176
}
177177
if !replaceSynonyms { // default True
178-
url.append(encodeForQuery(replaceSynonyms, key: "replaceSynonymsInHighlight"))
178+
url.append(Query.encodeForQuery(replaceSynonyms, withKey: "replaceSynonymsInHighlight"))
179179
}
180180
if page > 0 {
181-
url.append(encodeForQuery(page, key: "page"))
181+
url.append(Query.encodeForQuery(page, withKey: "page"))
182182
}
183183
if hitsPerPage != 20 && hitsPerPage > 0 {
184-
url.append(encodeForQuery(hitsPerPage, key: "hitsPerPage"))
184+
url.append(Query.encodeForQuery(hitsPerPage, withKey: "hitsPerPage"))
185185
}
186186
if let queryType = queryType {
187-
url.append(encodeForQuery(queryType, key: "queryType"))
187+
url.append(Query.encodeForQuery(queryType, withKey: "queryType"))
188188
}
189189
if let removeWordsIfNoResult = removeWordsIfNoResult {
190-
url.append(encodeForQuery(removeWordsIfNoResult, key: "removeWordsIfNoResult"))
190+
url.append(Query.encodeForQuery(removeWordsIfNoResult, withKey: "removeWordsIfNoResult"))
191191
}
192192
if let tagFilters = tagFilters {
193-
url.append(encodeForQuery(tagFilters, key: "tagFilters"))
193+
url.append(Query.encodeForQuery(tagFilters, withKey: "tagFilters"))
194194
}
195195
if let numericFilters = numericFilters {
196-
url.append(encodeForQuery(numericFilters, key: "numericFilters"))
196+
url.append(Query.encodeForQuery(numericFilters, withKey: "numericFilters"))
197197
}
198198

199199
if let insideBoundingBox = insideBoundingBox {
@@ -203,29 +203,29 @@ public class Query : Printable {
203203
}
204204

205205
if aroundLatLongViaIP {
206-
url.append(encodeForQuery(aroundLatLongViaIP, key: "aroundLatLngViaIP"))
206+
url.append(Query.encodeForQuery(aroundLatLongViaIP, withKey: "aroundLatLngViaIP"))
207207
}
208208
if let fullTextQuery = fullTextQuery {
209-
url.append(encodeForQuery(fullTextQuery, key: "query"))
209+
url.append(Query.encodeForQuery(fullTextQuery, withKey: "query"))
210210
}
211211
if let restrictSearchableAttributes = restrictSearchableAttributes {
212-
url.append(encodeForQuery(restrictSearchableAttributes, key: "restrictSearchableAttributes"))
212+
url.append(Query.encodeForQuery(restrictSearchableAttributes, withKey: "restrictSearchableAttributes"))
213213
}
214214

215215
return "&".join(url)
216216
}
217217

218-
// MARK: - Private methods
218+
// MARK: - Helper methods to build URL
219219

220-
private func encodeForQuery(elements: [String], key: String) -> String {
220+
class func encodeForQuery(elements: [String], withKey key: String) -> String {
221221
return "\(key)=" + ",".join(elements.map { $0.urlEncode() })
222222
}
223223

224-
private func encodeForQuery(element: String, key: String) -> String {
224+
class func encodeForQuery(element: String, withKey key: String) -> String {
225225
return "\(key)=\(element.urlEncode())"
226226
}
227227

228-
private func encodeForQuery<T>(element: T, key: String) -> String {
228+
class func encodeForQuery<T>(element: T, withKey key: String) -> String {
229229
return "\(key)=\(element)"
230230
}
231231
}

0 commit comments

Comments
 (0)