Skip to content

Commit 3d02b31

Browse files
millotpFluf22
andauthoredSep 4, 2024··
feat(clients): add helper to check if an index exists (#3646)
Co-authored-by: Thomas Raffray <Fluf22@users.noreply.github.com>
1 parent 883cdaa commit 3d02b31

File tree

52 files changed

+557
-296
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+557
-296
lines changed
 

‎clients/algoliasearch-client-csharp/algoliasearch/Utils/SearchClientExtensions.cs

+20
Original file line numberDiff line numberDiff line change
@@ -593,4 +593,24 @@ private static async Task<List<TU>> CreateIterable<TU>(Func<TU, Task<TU>> execut
593593

594594
return responses;
595595
}
596+
597+
public bool Exists(string indexName, CancellationToken cancellationToken = default) => AsyncHelper.RunSync(() => IndexExistsAsync(indexName, cancellationToken));
598+
599+
public async Task<bool> IndexExistsAsync(string indexName, CancellationToken cancellationToken = default)
600+
{
601+
try
602+
{
603+
await GetSettingsAsync(indexName, null, cancellationToken);
604+
}
605+
catch (AlgoliaApiException ex) when (ex.HttpErrorCode == 404)
606+
{
607+
return await Task.FromResult(false);
608+
}
609+
catch (Exception ex)
610+
{
611+
throw ex;
612+
}
613+
614+
return await Task.FromResult(true);
615+
}
596616
}

‎clients/algoliasearch-client-kotlin/client/src/commonMain/kotlin/com/algolia/client/extensions/SearchClient.kt

+13
Original file line numberDiff line numberDiff line change
@@ -521,3 +521,16 @@ public fun securedApiKeyRemainingValidity(apiKey: String): Duration {
521521
val validUntil = Instant.fromEpochMilliseconds(match.groupValues[1].toLong())
522522
return validUntil - Clock.System.now()
523523
}
524+
525+
public suspend fun SearchClient.indexExists(indexName: String): Boolean {
526+
try {
527+
getSettings(indexName)
528+
} catch (e: AlgoliaApiException) {
529+
if (e.httpErrorCode == 404) {
530+
return false
531+
}
532+
throw e
533+
}
534+
535+
return true
536+
}

0 commit comments

Comments
 (0)
Please sign in to comment.