Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding SearchClientBuilderExtensions #12628

Merged
merged 2 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion sdk/search/Azure.Search.Documents/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Release History

## 1.0.0-preview.4 (Unreleased)
## 1.0.0-preview.4 (2020-05-09)

### Added

- Added `SearchClientBuilderExtensions` to integrate with ASP.NET Core.
- Added `SearchModelFactory` to mock output model types.

### Breaking Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2049,3 +2049,13 @@ public ValueFacetResult(long count, T value) { }
public T Value { get { throw null; } }
}
}
namespace Microsoft.Extensions.Azure
{
public static partial class SearchClientBuilderExtensions
{
public static Azure.Core.Extensions.IAzureClientBuilder<Azure.Search.Documents.SearchClient, Azure.Search.Documents.SearchClientOptions> AddSearchClient<TBuilder>(this TBuilder builder, System.Uri endpoint, string indexName, Azure.AzureKeyCredential credential) where TBuilder : Azure.Core.Extensions.IAzureClientFactoryBuilder { throw null; }
public static Azure.Core.Extensions.IAzureClientBuilder<Azure.Search.Documents.SearchClient, Azure.Search.Documents.SearchClientOptions> AddSearchClient<TBuilder, TConfiguration>(this TBuilder builder, TConfiguration configuration) where TBuilder : Azure.Core.Extensions.IAzureClientFactoryBuilderWithConfiguration<TConfiguration> { throw null; }
public static Azure.Core.Extensions.IAzureClientBuilder<Azure.Search.Documents.Indexes.SearchIndexClient, Azure.Search.Documents.SearchClientOptions> AddSearchIndexClient<TBuilder>(this TBuilder builder, System.Uri endpoint, Azure.AzureKeyCredential credential) where TBuilder : Azure.Core.Extensions.IAzureClientFactoryBuilder { throw null; }
public static Azure.Core.Extensions.IAzureClientBuilder<Azure.Search.Documents.Indexes.SearchIndexClient, Azure.Search.Documents.SearchClientOptions> AddSearchIndexClient<TBuilder, TConfiguration>(this TBuilder builder, TConfiguration configuration) where TBuilder : Azure.Core.Extensions.IAzureClientFactoryBuilderWithConfiguration<TConfiguration> { throw null; }
}
}
110 changes: 110 additions & 0 deletions sdk/search/Azure.Search.Documents/src/SearchClientBuilderExtensions.cs
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);
}
}