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

[StrawberryShake] AddScopedXClient #7878

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 1 addition & 11 deletions src/StrawberryShake/Client/src/Core/IClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,8 @@ namespace StrawberryShake;
/// <summary>
/// Represents a builder that can be used to configure a client
/// </summary>
public interface IClientBuilder<T> where T : IStoreAccessor
public interface IClientBuilder<T> : IScopedClientBuilder<T> where T : IStoreAccessor
{
/// <summary>
/// The name of the client
/// </summary>
public string ClientName { get; }

/// <summary>
/// The <see cref="IServiceCollection"/> this client was registered to
/// </summary>
public IServiceCollection Services { get; }

/// <summary>
/// The <see cref="IServiceCollection"/> of the client
/// </summary>
Expand Down
19 changes: 19 additions & 0 deletions src/StrawberryShake/Client/src/Core/IScopedClientBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.Extensions.DependencyInjection;

namespace StrawberryShake;

/// <summary>
/// Represents a builder that can be used to configure a client
/// </summary>
public interface IScopedClientBuilder<T> where T : IStoreAccessor
{
/// <summary>
/// The name of the client
/// </summary>
public string ClientName { get; }

/// <summary>
/// The <see cref="IServiceCollection"/> this client was registered to
/// </summary>
public IServiceCollection Services { get; }
}
27 changes: 27 additions & 0 deletions src/StrawberryShake/Client/src/Core/ScopedClientBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.Extensions.DependencyInjection;

namespace StrawberryShake;

/// <inheritdoc />
/// <summary>
/// Initializes a new instance of a <see cref="ScopedClientBuilder{T}"/>
/// </summary>
/// <param name="clientName">
/// The name of the client
/// </param>
/// <param name="services">
/// The service collection this client was registered to
/// </param>
public class ScopedClientBuilder<T>(
string clientName,
IServiceCollection services)
: IScopedClientBuilder<T>
where T : IStoreAccessor
{

/// <inheritdoc />
public string ClientName { get; } = clientName;

/// <inheritdoc />
public IServiceCollection Services { get; } = services;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,56 @@ public static IClientBuilder<T> ConfigureHttpClient<T>(
Action<HttpClient> configureClient,
Action<IHttpClientBuilder>? configureClientBuilder = null)
where T : IStoreAccessor
{
ConfigureHttpClient((IScopedClientBuilder<T>)clientBuilder, configureClient, configureClientBuilder);
return clientBuilder;
}

/// <summary>
/// Adds the <see cref="IHttpClientFactory"/> and related services to the
/// <see cref="IServiceCollection"/> and configures a <see cref="HttpClient"/>
/// with the correct name
/// </summary>
/// <param name="clientBuilder">
/// The <see cref="IClientBuilder{T}"/>
/// </param>
/// <param name="configureClient">
/// A delegate that is used to configure an <see cref="HttpClient"/>.
/// </param>
/// <param name="configureClientBuilder">
/// A delegate that is used to additionally configure the <see cref="HttpClient"/>
/// with a <see cref="IHttpClientBuilder"/>
/// </param>
public static IClientBuilder<T> ConfigureHttpClient<T>(
this IClientBuilder<T> clientBuilder,
Action<IServiceProvider, HttpClient> configureClient,
Action<IHttpClientBuilder>? configureClientBuilder = null)
where T : IStoreAccessor
{
ConfigureHttpClient((IScopedClientBuilder<T>)clientBuilder, configureClient, configureClientBuilder);
return clientBuilder;
}

/// <summary>
/// Adds the <see cref="IHttpClientFactory"/> and related services to the
/// <see cref="IServiceCollection"/> and configures a <see cref="HttpClient"/>
/// with the correct name
/// </summary>
/// <param name="clientBuilder">
/// The <see cref="IScopedClientBuilder{T}"/>
/// </param>
/// <param name="configureClient">
/// A delegate that is used to configure an <see cref="HttpClient"/>.
/// </param>
/// <param name="configureClientBuilder">
/// A delegate that is used to additionally configure the <see cref="HttpClient"/>
/// with a <see cref="IHttpClientBuilder"/>
/// </param>
public static IScopedClientBuilder<T> ConfigureHttpClient<T>(
this IScopedClientBuilder<T> clientBuilder,
Action<HttpClient> configureClient,
Action<IHttpClientBuilder>? configureClientBuilder = null)
where T : IStoreAccessor
{
if (clientBuilder == null)
{
Expand Down Expand Up @@ -68,7 +118,7 @@ public static IClientBuilder<T> ConfigureHttpClient<T>(
/// with the correct name
/// </summary>
/// <param name="clientBuilder">
/// The <see cref="IClientBuilder{T}"/>
/// The <see cref="IScopedClientBuilder{T}"/>
/// </param>
/// <param name="configureClient">
/// A delegate that is used to configure an <see cref="HttpClient"/>.
Expand All @@ -77,8 +127,8 @@ public static IClientBuilder<T> ConfigureHttpClient<T>(
/// A delegate that is used to additionally configure the <see cref="HttpClient"/>
/// with a <see cref="IHttpClientBuilder"/>
/// </param>
public static IClientBuilder<T> ConfigureHttpClient<T>(
this IClientBuilder<T> clientBuilder,
public static IScopedClientBuilder<T> ConfigureHttpClient<T>(
this IScopedClientBuilder<T> clientBuilder,
Action<IServiceProvider, HttpClient> configureClient,
Action<IHttpClientBuilder>? configureClientBuilder = null)
where T : IStoreAccessor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,58 @@ public static IClientBuilder<T> ConfigureWebSocketClient<T>(
Action<IWebSocketClient> configureClient,
Action<IWebSocketClientBuilder>? configureClientBuilder = null)
where T : IStoreAccessor
{
ConfigureWebSocketClient((IScopedClientBuilder<T>)clientBuilder, configureClient, configureClientBuilder);

return clientBuilder;
}

/// <summary>
/// Adds the <see cref="ISocketClientFactory"/> and related services to the
/// <see cref="IServiceCollection"/> and configures a <see cref="WebSocketClient"/>
/// with the correct name
/// </summary>
/// <param name="clientBuilder">
/// The <see cref="IClientBuilder{T}"/>
/// </param>
/// <param name="configureClient">
/// A delegate that is used to configure an <see cref="WebSocketClient"/>.
/// </param>
/// <param name="configureClientBuilder">
/// A delegate that is used to additionally configure the <see cref="IWebSocketClient"/>
/// with a <see cref="IWebSocketClientBuilder"/>
/// </param>
public static IClientBuilder<T> ConfigureWebSocketClient<T>(
this IClientBuilder<T> clientBuilder,
Action<IServiceProvider, IWebSocketClient> configureClient,
Action<IWebSocketClientBuilder>? configureClientBuilder = null)
where T : IStoreAccessor
{
ConfigureWebSocketClient((IScopedClientBuilder<T>)clientBuilder, configureClient, configureClientBuilder);

return clientBuilder;
}

/// <summary>
/// Adds the <see cref="ISocketClientFactory"/> and related services to the
/// <see cref="IServiceCollection"/> and configures a <see cref="WebSocketClient"/>
/// with the correct name
/// </summary>
/// <param name="clientBuilder">
/// The <see cref="IScopedClientBuilder{T}"/>
/// </param>
/// <param name="configureClient">
/// A delegate that is used to configure an <see cref="WebSocketClient"/>.
/// </param>
/// <param name="configureClientBuilder">
/// A delegate that is used to additionally configure the <see cref="IWebSocketClient"/>
/// with a <see cref="IWebSocketClientBuilder"/>
/// </param>
public static IScopedClientBuilder<T> ConfigureWebSocketClient<T>(
this IScopedClientBuilder<T> clientBuilder,
Action<IWebSocketClient> configureClient,
Action<IWebSocketClientBuilder>? configureClientBuilder = null)
where T : IStoreAccessor
{
if (clientBuilder == null)
{
Expand All @@ -54,7 +106,7 @@ public static IClientBuilder<T> ConfigureWebSocketClient<T>(
/// with the correct name
/// </summary>
/// <param name="clientBuilder">
/// The <see cref="IClientBuilder{T}"/>
/// The <see cref="IScopedClientBuilder{T}"/>
/// </param>
/// <param name="configureClient">
/// A delegate that is used to configure an <see cref="WebSocketClient"/>.
Expand All @@ -63,8 +115,8 @@ public static IClientBuilder<T> ConfigureWebSocketClient<T>(
/// A delegate that is used to additionally configure the <see cref="IWebSocketClient"/>
/// with a <see cref="IWebSocketClientBuilder"/>
/// </param>
public static IClientBuilder<T> ConfigureWebSocketClient<T>(
this IClientBuilder<T> clientBuilder,
public static IScopedClientBuilder<T> ConfigureWebSocketClient<T>(
this IScopedClientBuilder<T> clientBuilder,
Action<IServiceProvider, IWebSocketClient> configureClient,
Action<IWebSocketClientBuilder>? configureClientBuilder = null)
where T : IStoreAccessor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,17 @@ public static CodeBlockBuilder AddBody(this MethodBuilder method)
method.AddCode(code);
return code;
}

public static MethodBuilder If(
this MethodBuilder builder,
bool condition,
Action<MethodBuilder> configure)
{
if (condition)
{
configure(builder);
}

return builder;
}
}
Loading
Loading