Skip to content

Commit 41c024c

Browse files
Simplify "AddHttpClientToCurl" configuration
1 parent 450a5bc commit 41c024c

File tree

5 files changed

+68
-45
lines changed

5 files changed

+68
-45
lines changed

examples/HttpClientToCurl.Sample.InGlobal/Program.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
1+
using HttpClientToCurl.Config.Others;
12
using static HttpClientToCurl.Extensions.ServiceCollectionExtensions;
23

34
var builder = WebApplication.CreateBuilder(args);
45
builder.Services.AddControllers();
56
builder.Services.AddHttpClientToCurl(config =>
67
{
7-
config.TurnOnAll = true;
8-
config.ShowOnConsole = consoleConfig =>
9-
{
10-
consoleConfig.TurnOn = true;
11-
consoleConfig.EnableCodeBeautification = true;
12-
};
13-
config.SaveToFile = fileConfig =>
14-
{
15-
fileConfig.TurnOn = true;
16-
fileConfig.Filename = "curl_commands.txt";
17-
fileConfig.Path = "C:\\logs";
18-
fileConfig.NeedAddDefaultHeaders = true;
19-
};
8+
config.ShowMode = ShowMode.Console | ShowMode.File;
9+
config.NeedAddDefaultHeaders = true;
10+
config.ConsoleEnableCodeBeautification = true;
11+
config.FileConfigPath = "C:\\Users\\Public";
12+
config.FileConfigFileName = "curl_commands";
2013
});
2114
builder.Services.AddHttpClient();
2215

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1-
namespace HttpClientToCurl.Config;
1+
using HttpClientToCurl.Config.Others;
2+
3+
namespace HttpClientToCurl.Config;
24

35
public sealed class GlobalConfig
46
{
5-
/// <summary>
6-
/// Set true to create curl output; false to disable it. <c>Default is true</c>.
7-
/// </summary>
8-
public bool TurnOnAll { get; set; } = true;
9-
10-
/// <summary>
11-
/// Set true to show curl on the console; false to disable it. <c>Default is true</c>.
12-
/// <para>If TurnOnAll is set to false, it will be ignored.</para>
13-
/// </summary>
14-
public Action<ConsoleConfig> ShowOnConsole { get; set; }
7+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0032:Use auto property", Justification = "<Pending>")]
8+
private ShowMode _showMode;
159

16-
/// <summary>
17-
/// Set true to save the curl file; false to disable it. <c>Default is true</c>.
18-
/// <para>If TurnOnAll is set to false, it will be ignored.</para>
19-
/// </summary>
20-
public Action<FileConfig> SaveToFile { get; set; }
10+
public ShowMode ShowMode
11+
{
12+
get => _showMode;
13+
set
14+
{
15+
if (value is not ShowMode.None && value.HasFlag(ShowMode.None))
16+
{
17+
throw new ArgumentException("ShowMode.None cannot be combined with other flags.");
18+
}
19+
_showMode = value;
20+
}
21+
}
22+
public bool NeedAddDefaultHeaders { get; set; } = true;
23+
public bool ConsoleEnableCodeBeautification { get; set; } = false;
24+
public bool ConsoleEnableCompression { get; set; } = false;
25+
public string FileConfigFileName { get; set; }
26+
public string FileConfigPath { get; set; }
2127
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace HttpClientToCurl.Config.Others;
2+
3+
[Flags]
4+
public enum ShowMode
5+
{
6+
None = 1,
7+
Console = 2,
8+
File = 4
9+
}

src/HttpClientToCurl/Extensions/ServiceCollectionExtensions.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using HttpClientToCurl.Config;
2+
using HttpClientToCurl.Config.Others;
23
using HttpClientToCurl.HttpMessageHandlers;
34
using Microsoft.Extensions.DependencyInjection;
45
using Microsoft.Extensions.Http;
@@ -12,15 +13,23 @@ public static class ServiceCollectionExtensions
1213
/// <para> By default, show it in the IDE console. </para>
1314
/// </summary>
1415
/// <param name="configAction">Optional</param>
16+
/// <param name="isGlobal">Apply for all http requests in the application or not. Default is true.</param>
1517
public static void AddHttpClientToCurl(
16-
this IServiceCollection services,
17-
Action<GlobalConfig> configAction = null)
18+
this IServiceCollection services,
19+
Action<GlobalConfig> configAction = null,
20+
bool isGlobal = true)
1821
{
22+
configAction ??= config => config.ShowMode = ShowMode.Console;
23+
1924
var config = new GlobalConfig();
2025
configAction?.Invoke(config);
2126

2227
services.AddSingleton(config);
2328
services.AddTransient<CurlGeneratorHttpMessageHandler>();
24-
services.Add(ServiceDescriptor.Transient<IHttpMessageHandlerBuilderFilter, HttpMessageHandlerAppender>());
29+
30+
if (isGlobal)
31+
{
32+
services.Add(ServiceDescriptor.Transient<IHttpMessageHandlerBuilderFilter, HttpMessageHandlerAppender>());
33+
}
2534
}
2635
}
Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
using HttpClientToCurl.Config;
2+
using HttpClientToCurl.Config.Others;
23
using HttpClientToCurl.Extensions;
34

45
namespace HttpClientToCurl.HttpMessageHandlers;
56

67
public class CurlGeneratorHttpMessageHandler(GlobalConfig config) : DelegatingHandler
78
{
89
protected override Task<HttpResponseMessage> SendAsync(
9-
HttpRequestMessage request,
10+
HttpRequestMessage httpRequestMessage,
1011
CancellationToken cancellationToken)
1112
{
12-
if (config.TurnOnAll)
13+
if (config.ShowMode.HasFlag(ShowMode.Console))
1314
{
14-
var consoleConfig = new ConsoleConfig();
15-
config.ShowOnConsole?.Invoke(consoleConfig);
16-
if (consoleConfig.TurnOn)
15+
httpRequestMessage.GenerateCurlInConsole(httpRequestMessage.RequestUri, consoleConfig =>
1716
{
18-
HttpRequestMessageExtensions.GenerateCurlInConsole(request, null);
19-
}
17+
consoleConfig.TurnOn = true;
18+
consoleConfig.EnableCodeBeautification = config.ConsoleEnableCodeBeautification;
19+
consoleConfig.EnableCompression = config.ConsoleEnableCompression;
20+
consoleConfig.NeedAddDefaultHeaders = config.NeedAddDefaultHeaders;
21+
});
22+
}
2023

21-
var fileConfig = new FileConfig();
22-
config.SaveToFile?.Invoke(fileConfig);
23-
if (fileConfig.TurnOn)
24+
if (config.ShowMode.HasFlag(ShowMode.File))
25+
{
26+
httpRequestMessage.GenerateCurlInFile(httpRequestMessage.RequestUri, fileConfig =>
2427
{
25-
HttpRequestMessageExtensions.GenerateCurlInFile(request, null, config.SaveToFile);
26-
}
28+
fileConfig.TurnOn = true;
29+
fileConfig.Filename = config.FileConfigFileName;
30+
fileConfig.Path = config.FileConfigPath;
31+
fileConfig.NeedAddDefaultHeaders = config.NeedAddDefaultHeaders;
32+
});
2733
}
2834

29-
var response = base.SendAsync(request, cancellationToken);
35+
var response = base.SendAsync(httpRequestMessage, cancellationToken);
3036
return response;
3137
}
3238
}

0 commit comments

Comments
 (0)