Skip to content

Commit 795bf75

Browse files
algolia-botmillotpFluf22
committed
feat(clients): add helper to check if an index exists (generated)
algolia/api-clients-automation#3646 Co-authored-by: algolia-bot <[email protected]> Co-authored-by: Pierre Millot <[email protected]> Co-authored-by: Thomas Raffray <[email protected]>
1 parent 31d9692 commit 795bf75

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

Sources/Search/Extra/SearchClientExtension.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ public extension SearchClient {
541541
batchSize: Int = 1000,
542542
requestOptions: RequestOptions? = nil
543543
) async throws -> ReplaceAllObjectsResponse {
544-
let tmpIndexName = try "\(indexName)_tmp_\(Int.random(in: 1_000_000 ..< 10_000_000))"
544+
let tmpIndexName = "\(indexName)_tmp_\(Int.random(in: 1_000_000 ..< 10_000_000))"
545545

546546
var copyOperationResponse = try await operationIndex(
547547
indexName: indexName,
@@ -627,4 +627,14 @@ public extension SearchClient {
627627

628628
return timestampDate.timeIntervalSince1970 - Date().timeIntervalSince1970
629629
}
630+
631+
func indexExists(indexName: String) async throws -> Bool {
632+
do {
633+
_ = try await self.getSettings(indexName: indexName)
634+
} catch let AlgoliaError.httpError(error) where error.statusCode == 404 {
635+
return false
636+
}
637+
638+
return true
639+
}
630640
}

0 commit comments

Comments
 (0)