Skip to content

Commit 9451edc

Browse files
authored
fix(swift): api key helpers (#3337)
1 parent 930639b commit 9451edc

File tree

7 files changed

+38
-1111
lines changed

7 files changed

+38
-1111
lines changed

clients/algoliasearch-client-swift/Sources/Core/Networking/Transporter.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ open class Transporter {
132132
self.retryStrategy.notify(host: host, error: error)
133133

134134
guard self.retryStrategy.canRetry(inCaseOf: error) else {
135-
throw AlgoliaError.requestError(error)
135+
throw error
136136
}
137137

138138
intermediateErrors.append(error)

clients/algoliasearch-client-swift/Sources/Search/Extra/SearchClientExtension.swift

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public extension SearchClient {
2020
/// - returns: GetTaskResponse
2121
@discardableResult
2222
func waitForTask(
23-
with taskID: Int64,
24-
in indexName: String,
23+
indexName: String,
24+
taskID: Int64,
2525
maxRetries: Int = 50,
2626
timeout: (Int) -> TimeInterval = { count in
2727
min(TimeInterval(count) * 0.2, 5)
@@ -62,7 +62,7 @@ public extension SearchClient {
6262
/// - returns: GetTaskResponse
6363
@discardableResult
6464
func waitForAppTask(
65-
with taskID: Int64,
65+
taskID: Int64,
6666
maxRetries: Int = 50,
6767
timeout: (Int) -> TimeInterval = { count in
6868
min(TimeInterval(count) * 0.2, 5)
@@ -105,7 +105,7 @@ public extension SearchClient {
105105
/// - returns: GetApiKeyResponse?
106106
@discardableResult
107107
func waitForApiKey(
108-
with key: String,
108+
key: String,
109109
operation: ApiKeyOperation,
110110
apiKey: ApiKey? = nil,
111111
maxRetries: Int = 50,
@@ -126,23 +126,23 @@ public extension SearchClient {
126126
try await self.getApiKey(key: key, requestOptions: requestOptions)
127127
},
128128
validate: { response in
129-
if apiKey.description != response.description {
129+
if apiKey.description != nil && apiKey.description != response.description {
130130
return false
131131
}
132132

133-
if apiKey.queryParameters != response.queryParameters {
133+
if apiKey.queryParameters != nil && apiKey.queryParameters != response.queryParameters {
134134
return false
135135
}
136136

137-
if apiKey.maxHitsPerQuery != response.maxHitsPerQuery {
137+
if apiKey.maxHitsPerQuery != nil && apiKey.maxHitsPerQuery != response.maxHitsPerQuery {
138138
return false
139139
}
140140

141-
if apiKey.maxQueriesPerIPPerHour != response.maxQueriesPerIPPerHour {
141+
if apiKey.maxQueriesPerIPPerHour != nil && apiKey.maxQueriesPerIPPerHour != response.maxQueriesPerIPPerHour {
142142
return false
143143
}
144144

145-
if apiKey.validity != response.validity {
145+
if apiKey.validity != nil && apiKey.validity != response.validity {
146146
return false
147147
}
148148

@@ -152,16 +152,20 @@ public extension SearchClient {
152152
return false
153153
}
154154

155-
let expectedIndexes = apiKey.indexes?.sorted { $0 > $1 }
156-
let responseIndexes = response.indexes?.sorted { $0 > $1 }
157-
if expectedIndexes != responseIndexes {
158-
return false
155+
if let apiKeyIndexes = apiKey.indexes {
156+
let expectedIndexes = apiKeyIndexes.sorted { $0 > $1 }
157+
let responseIndexes = response.indexes?.sorted { $0 > $1 }
158+
if expectedIndexes != responseIndexes {
159+
return false
160+
}
159161
}
160162

161-
let expectedReferers = apiKey.referers?.sorted { $0 > $1 }
162-
let responseReferers = response.referers?.sorted { $0 > $1 }
163-
if expectedReferers != responseReferers {
164-
return false
163+
if let apiKeyReferers = apiKey.referers {
164+
let expectedReferers = apiKeyReferers.sorted { $0 > $1 }
165+
let responseReferers = response.referers?.sorted { $0 > $1 }
166+
if expectedReferers != responseReferers {
167+
return false
168+
}
165169
}
166170

167171
return true
@@ -185,12 +189,15 @@ public extension SearchClient {
185189

186190
return try await createIterable(
187191
execute: { _ in
188-
let response = try await self.getApiKeyWithHTTPInfo(key: key, requestOptions: requestOptions)
189-
if response.statusCode == 404 {
190-
return nil
192+
do {
193+
return try await self.getApiKey(key: key, requestOptions: requestOptions)
194+
} catch AlgoliaError.httpError(let error) {
195+
if error.statusCode == 404 {
196+
return nil
197+
}
198+
199+
throw error
191200
}
192-
193-
return response.body
194201
},
195202
validate: { response in
196203
switch operation {
@@ -232,7 +239,7 @@ public extension SearchClient {
232239
/// - returns: BrowseResponse
233240
@discardableResult
234241
func browseObjects<T: Codable>(
235-
in indexName: String,
242+
indexName: String,
236243
browseParams: BrowseParamsObject,
237244
validate: (BrowseResponse<T>) -> Bool = { response in
238245
response.cursor == nil
@@ -267,7 +274,7 @@ public extension SearchClient {
267274
/// - returns: SearchRulesResponse
268275
@discardableResult
269276
func browseRules(
270-
in indexName: String,
277+
indexName: String,
271278
searchRulesParams: SearchRulesParams,
272279
validate: ((SearchRulesResponse) -> Bool)? = nil,
273280
aggregator: @escaping (SearchRulesResponse) -> Void,
@@ -310,7 +317,7 @@ public extension SearchClient {
310317
/// - returns: SearchSynonymsResponse
311318
@discardableResult
312319
func browseSynonyms(
313-
in indexName: String,
320+
indexName: String,
314321
searchSynonymsParams: SearchSynonymsParams,
315322
validate: ((SearchSynonymsResponse) -> Bool)? = nil,
316323
aggregator: @escaping (SearchSynonymsResponse) -> Void,
@@ -449,7 +456,7 @@ public extension SearchClient {
449456

450457
if waitForTasks {
451458
for batchResponse in responses {
452-
try await self.waitForTask(with: batchResponse.taskID, in: indexName)
459+
try await self.waitForTask(indexName: indexName, taskID: batchResponse.taskID)
453460
}
454461
}
455462

@@ -557,7 +564,7 @@ public extension SearchClient {
557564
batchSize: batchSize,
558565
requestOptions: requestOptions
559566
)
560-
try await self.waitForTask(with: copyOperationResponse.taskID, in: tmpIndexName)
567+
try await self.waitForTask(indexName: tmpIndexName, taskID: copyOperationResponse.taskID)
561568

562569
copyOperationResponse = try await operationIndex(
563570
indexName: indexName,
@@ -568,7 +575,7 @@ public extension SearchClient {
568575
),
569576
requestOptions: requestOptions
570577
)
571-
try await self.waitForTask(with: copyOperationResponse.taskID, in: tmpIndexName)
578+
try await self.waitForTask(indexName: tmpIndexName, taskID: copyOperationResponse.taskID)
572579

573580
let moveOperationResponse = try await self.operationIndex(
574581
indexName: tmpIndexName,
@@ -578,7 +585,7 @@ public extension SearchClient {
578585
),
579586
requestOptions: requestOptions
580587
)
581-
try await self.waitForTask(with: moveOperationResponse.taskID, in: tmpIndexName)
588+
try await self.waitForTask(indexName: tmpIndexName, taskID: moveOperationResponse.taskID)
582589

583590
return ReplaceAllObjectsResponse(
584591
copyOperationResponse: copyOperationResponse,
@@ -603,7 +610,7 @@ public extension SearchClient {
603610
/// Get the remaining validity of a secured API key
604611
/// - parameter securedApiKey: The secured API key
605612
/// - returns: TimeInterval?
606-
func getSecuredApiKeyRemainingValidity(for securedApiKey: String) -> TimeInterval? {
613+
func getSecuredApiKeyRemainingValidity(securedApiKey: String) -> TimeInterval? {
607614
guard let rawDecodedAPIKey = String(data: Data(base64Encoded: securedApiKey) ?? Data(), encoding: .utf8),
608615
!rawDecodedAPIKey.isEmpty else {
609616
return nil

templates/swift/tests/Package.mustache

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,6 @@ let package = Package(
3939
.product(name: "DotEnv", package: "DotEnv"),
4040
.target(name: "Utils"),
4141
] + libraries
42-
),
43-
.testTarget(
44-
name: "handwritten",
45-
dependencies: [
46-
.product(name: "DotEnv", package: "DotEnv"),
47-
.target(name: "Utils"),
48-
.product(name: "Core", package: "algoliasearch-client-swift"),
49-
.product(name: "Search", package: "algoliasearch-client-swift")
50-
]
5142
)
5243
]
5344
)

0 commit comments

Comments
 (0)