Skip to content
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
31 changes: 15 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ Logging, monitoring, or tracing outgoing requests across the application.

### 💡 Why Use HttpClientToCurl?

- 🧪 Instantly visualize and debug request payloads or headers
- 🧪 Instantly visualise and debug request payloads or headers
- 🤝 Share exact API calls with teammates or QA engineers
- ⚙️ Simplify Postman and CLI reproduction
- 🪶 Lightweight, dependency-free, and easy to integrate
- 🧩 Lightweight, dependency-free, and easy to integrate

---
## ⚙️ Installation
Expand Down Expand Up @@ -111,25 +111,25 @@ curl -X POST 'http://localhost:1213/v1/api/test' \

## 🧩 Automatic Mode Usage Example

### 1️⃣ Global Registration
### 1️⃣ Per-Client Registration

Enable curl generation globally — every `HttpClient` created through `IHttpClientFactory` will automatically log curl commands.
Enable curl logging for specific named clients only.

**Program.cs / Startup.cs**
```csharp
using HttpClientToCurl;

// Register global curl generation
builder.Services.AddHttpClientToCurlInGeneralMode(builder.Configuration);
// Register the curl generator once
builder.Services.AddHttpClientToCurl(builder.Configuration);

// Register default HttpClient (now curl-enabled)
builder.Services.AddHttpClient();
// Enable curl logging for selected clients
builder.Services.AddHttpClient("my-client1", showCurl: true);
```

**appsettings.json**
```json
"HttpClientToCurl": {
"TurnOnAll": true, // Master switch: enable or disable the entire HttpClientToCURL logging system
"Enable": true, // Master switch: enable or disable the entire HttpClientToCURL logging system

"ShowOnConsole": {
"TurnOn": true, // Enable console output for generated curl commands
Expand All @@ -150,21 +150,20 @@ builder.Services.AddHttpClient();

---

### 2️⃣ Per-Client Registration
### 2️⃣ Global Registration

Enable curl logging for specific named clients only.
Enable curl generation globally — every `HttpClient` created through `IHttpClientFactory` will automatically log curl commands.

**Program.cs / Startup.cs**
```csharp
using HttpClientToCurl;

// Register the curl generator once
builder.Services.AddHttpClientToCurl(builder.Configuration);
// Register global curl generation
builder.Services.AddAllHttpClientToCurl(builder.Configuration);

// Enable curl logging for selected clients
builder.Services.AddHttpClient("my-client1", showCurl: true);
// Register default HttpClient (now curl-enabled)
builder.Services.AddHttpClient();
```
---

**appsettings.json**
(same configuration options as above)
Expand Down
2 changes: 1 addition & 1 deletion examples/HttpClientToCurl.Sample.InGlobal/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();

builder.Services.AddHttpClientToCurlInGeneralMode(builder.Configuration);
builder.Services.AddAllHttpClientToCurl(builder.Configuration);
builder.Services.AddHttpClient();

var app = builder.Build();
Expand Down
6 changes: 3 additions & 3 deletions examples/HttpClientToCurl.Sample.InGlobal/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
},
"AllowedHosts": "*",
"HttpClientToCurl": {
"TurnOnAll": true,
"Enable": true,

"ShowOnConsole": {
"TurnOn": true, //CAUTION: It will be applied when TurnOnAll is 'true'
"TurnOn": true, //CAUTION: It will be applied when Enable is 'true'
"NeedAddDefaultHeaders": true,
"EnableCompression": false,
"EnableCodeBeautification": true
},

"SaveToFile": {
"TurnOn": true, //CAUTION: It will be applied when TurnOnAll is 'true'
"TurnOn": true, //CAUTION: It will be applied when Enable is 'true'
"NeedAddDefaultHeaders": true,
"EnableCompression": false,
"Filename": "curl_commands",
Expand Down
6 changes: 3 additions & 3 deletions examples/HttpClientToCurl.Sample.InSpecific/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
},
"AllowedHosts": "*",
"HttpClientToCurl": {
"TurnOnAll": true,
"Enable": true,

"ShowOnConsole": {
"TurnOn": true, //CAUTION: It will be applied when TurnOnAll is 'true'
"TurnOn": true, //CAUTION: It will be applied when Enable is 'true'
"NeedAddDefaultHeaders": true,
"EnableCompression": false,
"EnableCodeBeautification": true
},

"SaveToFile": {
"TurnOn": true, //CAUTION: It will be applied when TurnOnAll is 'true'
"TurnOn": true, //CAUTION: It will be applied when Enable is 'true'
"NeedAddDefaultHeaders": true,
"EnableCompression": false,
"Filename": "curl_commands",
Expand Down
6 changes: 3 additions & 3 deletions src/HttpClientToCurl/Config/CompositConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ public sealed class CompositConfig
/// <summary>
/// Set true to create curl output; false to disable it. <c>Default is true</c>.
/// </summary>
public bool TurnOnAll { get; set; } = true;
public bool Enable { get; set; } = true;

/// <summary>
/// Set true to show curl on the console; false to disable it. <c>Default is true</c>.
/// <para>If TurnOnAll is set to false, it will be ignored.</para>
/// <para>If Enable is set to false, it will be ignored.</para>
/// </summary>
public ConsoleConfig ShowOnConsole { get; set; }

/// <summary>
/// Set true to save the curl file; false to disable it. <c>Default is true</c>.
/// <para>If TurnOnAll is set to false, it will be ignored.</para>
/// <para>If Enable is set to false, it will be ignored.</para>
/// </summary>
public FileConfig SaveToFile { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static class ServiceCollectionExtensions
/// Generating curl script for all HTTP requests.
/// <para> By default, show it in the IDE console. </para>
/// </summary>
public static void AddHttpClientToCurlInGeneralMode(
public static void AddAllHttpClientToCurl(
this IServiceCollection services,
IConfiguration configuration)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ protected override Task<HttpResponseMessage> SendAsync(
CancellationToken cancellationToken)
{
var config = _monitorConfig.CurrentValue;
if (config.TurnOnAll)
if (config.Enable)
{
if (config.ShowOnConsole?.TurnOn ?? false)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Extensions.Http;

namespace HttpClientToCurl.HttpMessageHandlers;

public class HttpMessageHandlerAppender(IServiceProvider serviceProvider) : IHttpMessageHandlerBuilderFilter
{
public Action<HttpMessageHandlerBuilder> Configure(Action<HttpMessageHandlerBuilder> next) => builder =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public CompositConfigBuilder()
_config = new CompositConfig();
}

public CompositConfigBuilder SetTurnOnAll(bool turnOnAll)
public CompositConfigBuilder SetEnable(bool value)
{
_config.TurnOnAll = turnOnAll;
_config.Enable = value;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public async Task CurlGeneratorHttpMessageHandler_ReturnsResponse_When_TurnOffAl
{
// Arrange
var config = new CompositConfigBuilder()
.SetTurnOnAll(false)
.SetEnable(false)
.Build();
var handler = new CurlGeneratorHttpMessageHandler(new FakeOptionsMonitor<CompositConfig>(config))
{
Expand All @@ -33,11 +33,11 @@ public async Task CurlGeneratorHttpMessageHandler_ReturnsResponse_When_TurnOffAl
}

[Fact]
public async Task CurlGeneratorHttpMessageHandler_ReturnsResponse_When_TurnOnAll_But_ShowOnConsole_And_SaveToFile_AreNot_Configured()
public async Task CurlGeneratorHttpMessageHandler_ReturnsResponse_When_Enable_But_ShowOnConsole_And_SaveToFile_AreNot_Configured()
{
// Arrange
var config = new CompositConfigBuilder()
.SetTurnOnAll(true)
.SetEnable(true)
.SetShowOnConsole(null)
.SetSaveToFile(null)
.Build();
Expand All @@ -58,11 +58,11 @@ public async Task CurlGeneratorHttpMessageHandler_ReturnsResponse_When_TurnOnAll
}

[Fact]
public async Task CurlGeneratorHttpMessageHandler_ReturnsResponse_When_TurnOnAll_But_ShowOnConsole_And_SaveToFile_TurnOff()
public async Task CurlGeneratorHttpMessageHandler_ReturnsResponse_When_Enable_But_ShowOnConsole_And_SaveToFile_TurnOff()
{
// Arrange
var config = new CompositConfigBuilder()
.SetTurnOnAll(true)
.SetEnable(true)
.SetShowOnConsole(new ConsoleConfig
{
TurnOn = false,
Expand Down Expand Up @@ -93,7 +93,7 @@ public async Task CurlGeneratorHttpMessageHandler_WritesToConsole_When_ShowOnCon
{
// Arrange
var config = new CompositConfigBuilder()
.SetTurnOnAll(true)
.SetEnable(true)
.SetShowOnConsole(new ConsoleConfig
{
TurnOn = true,
Expand Down Expand Up @@ -139,7 +139,7 @@ public async Task CurlGeneratorHttpMessageHandler_WritesToFile_When_SaveToFile_T
var filename = Guid.NewGuid().ToString("N");

var config = new CompositConfigBuilder()
.SetTurnOnAll(true)
.SetEnable(true)
.SetSaveToFile(new FileConfig
{
TurnOn = true,
Expand Down Expand Up @@ -194,7 +194,7 @@ public async Task CurlGeneratorHttpMessageHandler_WritesToConsole_And_WritesToFi
var filename = Guid.NewGuid().ToString("N");

var config = new CompositConfigBuilder()
.SetTurnOnAll(true)
.SetEnable(true)
.SetShowOnConsole(new ConsoleConfig
{
TurnOn = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
using HttpMessageHandlerTest.UnitTest.Fakes;

namespace HttpMessageHandlerTest.UnitTest;

public class HttpMessageHandlerAppenderTests
{
[Fact]
public void HttpMessageHandlerAppender_Adds_Handler_To_Builder()
{
// Arrange
var config = new CompositConfig { TurnOnAll = false };
var config = new CompositConfig { Enable = false };
var handler = new CurlGeneratorHttpMessageHandler(new FakeOptionsMonitor<CompositConfig>(config));
var sp = new FakeServiceProvider(handler);
var appender = new HttpMessageHandlerAppender(sp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ namespace HttpMessageHandlerTest.UnitTest;
public class ServiceCollectionExtensionsTests
{
[Fact]
public void ServiceCollectionExtensions_Register_Services_In_GeneralMode()
public void ServiceCollectionExtensions_Register_Services_For_All()
{
// Arrange
var services = new ServiceCollection();
var configuration = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
.Build();

// Act
services.AddHttpClientToCurlInGeneralMode(configuration);
services.AddAllHttpClientToCurl(configuration);
var provider = services.BuildServiceProvider();

// Assert
Expand Down
Loading