Skip to content

Commit

Permalink
feat(clients): add helper to check if an index exists (generated)
Browse files Browse the repository at this point in the history
algolia/api-clients-automation#3646

Co-authored-by: algolia-bot <accounts+algolia-api-client-bot@algolia.com>
Co-authored-by: Pierre Millot <pierre.millot@algolia.com>
Co-authored-by: Thomas Raffray <Fluf22@users.noreply.github.com>
  • Loading branch information
3 people committed Sep 4, 2024
1 parent 31d9692 commit 795bf75
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Sources/Search/Extra/SearchClientExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ public extension SearchClient {
batchSize: Int = 1000,
requestOptions: RequestOptions? = nil
) async throws -> ReplaceAllObjectsResponse {
let tmpIndexName = try "\(indexName)_tmp_\(Int.random(in: 1_000_000 ..< 10_000_000))"
let tmpIndexName = "\(indexName)_tmp_\(Int.random(in: 1_000_000 ..< 10_000_000))"

var copyOperationResponse = try await operationIndex(
indexName: indexName,
Expand Down Expand Up @@ -627,4 +627,14 @@ public extension SearchClient {

return timestampDate.timeIntervalSince1970 - Date().timeIntervalSince1970
}

func indexExists(indexName: String) async throws -> Bool {
do {
_ = try await self.getSettings(indexName: indexName)
} catch let AlgoliaError.httpError(error) where error.statusCode == 404 {
return false
}

return true
}
}

0 comments on commit 795bf75

Please sign in to comment.