forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding SearchClientBuilderExtensions
Fixes Azure#10848
- Loading branch information
Showing
2 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
sdk/search/Azure.Search.Documents/src/SearchClientBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using Azure; | ||
using Azure.Core.Extensions; | ||
using Azure.Search.Documents; | ||
using Azure.Search.Documents.Indexes; | ||
|
||
namespace Microsoft.Extensions.Azure | ||
{ | ||
/// <summary> | ||
/// Extension methods to add <see cref="SearchClient"/> to the Azure client | ||
/// builder. | ||
/// </summary> | ||
public static class SearchClientBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Registers a <see cref="SearchClient"/> instance with the provided | ||
/// <paramref name="endpoint"/>, <paramref name="indexName"/>, and | ||
/// <paramref name="credential"/>. | ||
/// </summary> | ||
/// <typeparam name="TBuilder">Type of the client factory builder.</typeparam> | ||
/// <param name="builder">The client factory builder.</param> | ||
/// <param name="endpoint"> | ||
/// Required. The URI endpoint of the Search Service. This is likely | ||
/// to be similar to "https://{search_service}.search.windows.net". | ||
/// The URI must use HTTPS. | ||
/// </param> | ||
/// <param name="indexName"> | ||
/// Required. The name of the Search Index. | ||
/// </param> | ||
/// <param name="credential"> | ||
/// Required. The API key credential used to authenticate requests | ||
/// against the search service. You need to use an admin key to | ||
/// modify the documents in a Search Index. See | ||
/// <see href="https://docs.microsoft.com/azure/search/search-security-api-keys"/> | ||
/// for more information about API keys in Azure Cognitive Search. | ||
/// </param> | ||
/// <returns>An Azure client builder.</returns> | ||
public static IAzureClientBuilder<SearchClient, SearchClientOptions> AddSearchClient<TBuilder>( | ||
this TBuilder builder, | ||
Uri endpoint, | ||
string indexName, | ||
AzureKeyCredential credential) | ||
where TBuilder : IAzureClientFactoryBuilder => | ||
builder.RegisterClientFactory<SearchClient, SearchClientOptions>( | ||
options => new SearchClient(endpoint, indexName, credential, options)); | ||
|
||
/// <summary> | ||
/// Registers a <see cref="SearchClient"/> instance with connection | ||
/// options loaded from the provided <paramref name="configuration"/> | ||
/// instance. | ||
/// </summary> | ||
/// <typeparam name="TBuilder">Type of the client factory builder.</typeparam> | ||
/// <typeparam name="TConfiguration">Type of the configuration.</typeparam> | ||
/// <param name="builder">The client factory builder.</param> | ||
/// <param name="configuration">The client configuration.</param> | ||
/// <returns>An Azure client builder.</returns> | ||
public static IAzureClientBuilder<SearchClient, SearchClientOptions> AddSearchClient<TBuilder, TConfiguration>( | ||
this TBuilder builder, | ||
TConfiguration configuration) | ||
where TBuilder : IAzureClientFactoryBuilderWithConfiguration<TConfiguration> => | ||
builder.RegisterClientFactory<SearchClient, SearchClientOptions>(configuration); | ||
|
||
|
||
/// <summary> | ||
/// Registers a <see cref="SearchIndexClient"/> instance with the | ||
/// provided <paramref name="endpoint"/> and <paramref name="credential"/>. | ||
/// </summary> | ||
/// <typeparam name="TBuilder">Type of the client factory builder.</typeparam> | ||
/// <param name="builder">The client factory builder.</param> | ||
/// <param name="endpoint"> | ||
/// Required. The URI endpoint of the Search Service. This is likely | ||
/// to be similar to "https://{search_service}.search.windows.net". | ||
/// The URI must use HTTPS. | ||
/// </param> | ||
/// <param name="credential"> | ||
/// Required. The API key credential used to authenticate requests | ||
/// against the search service. You need to use an admin key to | ||
/// modify the documents in a Search Index. See | ||
/// <see href="https://docs.microsoft.com/azure/search/search-security-api-keys"/> | ||
/// for more information about API keys in Azure Cognitive Search. | ||
/// </param> | ||
/// <returns>An Azure client builder.</returns> | ||
public static IAzureClientBuilder<SearchIndexClient, SearchClientOptions> AddSearchIndexClient<TBuilder>( | ||
this TBuilder builder, | ||
Uri endpoint, | ||
AzureKeyCredential credential) | ||
where TBuilder : IAzureClientFactoryBuilder => | ||
builder.RegisterClientFactory<SearchIndexClient, SearchClientOptions>( | ||
options => new SearchIndexClient(endpoint, credential, options)); | ||
|
||
/// <summary> | ||
/// Registers a <see cref="SearchIndexClient"/> instance with connection | ||
/// options loaded from the provided <paramref name="configuration"/> | ||
/// instance. | ||
/// </summary> | ||
/// <typeparam name="TBuilder">Type of the client factory builder.</typeparam> | ||
/// <typeparam name="TConfiguration">Type of the configuration.</typeparam> | ||
/// <param name="builder">The client factory builder.</param> | ||
/// <param name="configuration">The client configuration.</param> | ||
/// <returns>An Azure client builder.</returns> | ||
public static IAzureClientBuilder<SearchIndexClient, SearchClientOptions> AddSearchIndexClient<TBuilder, TConfiguration>( | ||
this TBuilder builder, | ||
TConfiguration configuration) | ||
where TBuilder : IAzureClientFactoryBuilderWithConfiguration<TConfiguration> => | ||
builder.RegisterClientFactory<SearchIndexClient, SearchClientOptions>(configuration); | ||
} | ||
} |