Skip to content

Commit 5049bd0

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 829f745 commit 5049bd0

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

algoliasearch/Utils/SearchClientExtensions.cs

Lines changed: 20 additions & 0 deletions
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
}

0 commit comments

Comments
 (0)