Skip to content

Commit 8786cbf

Browse files
authored
Merge pull request #60 from AmirhosseinEnayati/feature/HCG-57
Add "AddHttpClientToCurl" for global and specific the httpClient(s) usage. - Implement AddHttpClientToCurl extension method for global HttpClient registration - Add support for specific HttpClient instances configuration - Enable curl generation for HttpClient instances created via IHttpClientFactory - Add configuration options for curl generation behavior This enhancement allows developers to register HttpClientToCurl functionality directly in their DI container, making it easier to integrate curl generation into applications using IHttpClientFactory pattern. Closes #60
2 parents 7d18a69 + daaa437 commit 8786cbf

File tree

28 files changed

+575
-18
lines changed

28 files changed

+575
-18
lines changed

.lutignore

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
## The .lutignore file is used by Live Unit Testing to ignore Visual Studio temporary files, build results,
2+
## and files generated by popular Visual Studio add-ons when creating a private copy of the source tree that
3+
## Live Unit Testing uses for its build.
4+
##
5+
## This file has same format as git's .gitignore file (https://git-scm.com/docs/gitignore). In fact, in the
6+
## case where a .lutignore file is not found, but a .gitignore file is found, Live Unit Testing will use the
7+
## .gitignore file directly for the above purpose.
8+
9+
# User-specific files
10+
*.suo
11+
*.user
12+
*.userprefs
13+
*.sln.docstates
14+
.vs/
15+
.vscode/
16+
.packages/
17+
.dotnet/
18+
.tools/
19+
.idea/
20+
21+
# Build results
22+
[Dd]ebug/
23+
[Rr]elease/
24+
[Bb]inaries/
25+
[Bb]in/
26+
[Oo]bj/
27+
x64/
28+
TestResults/
29+
30+
# Debug artifacts
31+
launchSettings.json
32+
33+
# Click-Once directory
34+
publish/
35+
36+
# Publish Web Output
37+
*.Publish.xml
38+
39+
# NuGet
40+
packages/
41+
[Nn]u[Gg]et.exe
42+
*-packages.config
43+
*.nuget.props
44+
*.nuget.targets
45+
project.lock.json
46+
msbuild.binlog
47+
*.project.lock.json
48+
49+
# Miscellaneous
50+
*_i.c
51+
*_p.c
52+
*.ilk
53+
*.meta
54+
*.obj
55+
*.pch
56+
*.pdb
57+
*.pgc
58+
*.pgd
59+
*.sbr
60+
*.tlb
61+
*.tli
62+
*.tlh
63+
*.tmp
64+
*.tmp_proj
65+
*.log
66+
*.wrn
67+
*.vspscc
68+
*.vssscc
69+
.builds
70+
*.pidb
71+
*.scc
72+
sql/
73+
*.Cache
74+
ClientBin/
75+
[Ss]tyle[Cc]op.*
76+
~$*
77+
*~
78+
*.dbmdl
79+
*.[Pp]ublish.xml
80+
*.pfx
81+
*.publishsettings
82+
83+
# Cache and temp files
84+
ipch/
85+
*.aps
86+
*.ncb
87+
*.opensdf
88+
*.sdf
89+
*.cachefile
90+
*.VC.opendb
91+
*.VC.db
92+
AppPackages/
93+
_ReSharper*/
94+
*.[Rr]e[Ss]harper
95+
*.DotSettings
96+
$RECYCLE.BIN/
97+
.DS_Store
98+
*wpftmp.*
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<LUTConfig Version="1.0">
2+
<Repository />
3+
<ParallelBuilds>true</ParallelBuilds>
4+
<ParallelTestRuns>true</ParallelTestRuns>
5+
<TestCaseTimeout>180000</TestCaseTimeout>
6+
</LUTConfig>

HttpClientToCurlGenerator.sln

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ VisualStudioVersion = 17.7.34221.43
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpClientToCurl", "src\HttpClientToCurl\HttpClientToCurl.csproj", "{18B3309D-B84C-453D-8EF7-16CA9E58F5DC}"
77
EndProject
8-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpClientToCurlGeneratorTest", "tests\HttpClientToCurlTest\HttpClientToCurlTest.csproj", "{BF3321A5-A590-44DD-BA5D-44978D9B6125}"
9-
EndProject
108
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{A8574DB9-8411-4F81-A82E-F97AD00EF8AF}"
119
EndProject
1210
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HttpClientToCurl.Sample.InConsole", "examples\HttpClientToCurl.Sample.InConsole\HttpClientToCurl.Sample.InConsole.csproj", "{323022D2-AAA7-443B-895C-77F5B1634D68}"
@@ -25,7 +23,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "solutionItems", "solutionIt
2523
pre-push = pre-push
2624
EndProjectSection
2725
EndProject
28-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpRequestMessageToCurlGeneratorTest", "tests\HttpRequestMessageToCurlTest\HttpRequestMessageToCurlTest.csproj", "{007CA9E0-CDF0-4375-8E8C-A24C9A7BF531}"
26+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpClientToCurl.Sample.InGlobal", "examples\HttpClientToCurl.Sample.InGlobal\HttpClientToCurl.Sample.InGlobal.csproj", "{5A8427BC-0821-E973-7221-263D13156248}"
27+
EndProject
28+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpClientToCurlTest", "tests\HttpClientToCurlTest\HttpClientToCurlTest.csproj", "{8CC76F1F-5845-D81E-5E9A-113F913A444B}"
29+
EndProject
30+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpRequestMessageToCurlTest", "tests\HttpRequestMessageToCurlTest\HttpRequestMessageToCurlTest.csproj", "{69E31075-F14E-1DE2-1D6E-D934A5C0480F}"
31+
EndProject
32+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpClientToCurl.Sample.InSpecific", "examples\HttpClientToCurl.Sample.InSpecific\HttpClientToCurl.Sample.InSpecific.csproj", "{9D56718F-C9E6-4C45-926D-97599072DA35}"
2933
EndProject
3034
Global
3135
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -37,10 +41,6 @@ Global
3741
{18B3309D-B84C-453D-8EF7-16CA9E58F5DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
3842
{18B3309D-B84C-453D-8EF7-16CA9E58F5DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
3943
{18B3309D-B84C-453D-8EF7-16CA9E58F5DC}.Release|Any CPU.Build.0 = Release|Any CPU
40-
{BF3321A5-A590-44DD-BA5D-44978D9B6125}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
41-
{BF3321A5-A590-44DD-BA5D-44978D9B6125}.Debug|Any CPU.Build.0 = Debug|Any CPU
42-
{BF3321A5-A590-44DD-BA5D-44978D9B6125}.Release|Any CPU.ActiveCfg = Release|Any CPU
43-
{BF3321A5-A590-44DD-BA5D-44978D9B6125}.Release|Any CPU.Build.0 = Release|Any CPU
4444
{323022D2-AAA7-443B-895C-77F5B1634D68}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
4545
{323022D2-AAA7-443B-895C-77F5B1634D68}.Debug|Any CPU.Build.0 = Debug|Any CPU
4646
{323022D2-AAA7-443B-895C-77F5B1634D68}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -53,21 +53,35 @@ Global
5353
{403B236B-D7D8-43FB-B47E-DCCAF8BA96C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
5454
{403B236B-D7D8-43FB-B47E-DCCAF8BA96C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
5555
{403B236B-D7D8-43FB-B47E-DCCAF8BA96C4}.Release|Any CPU.Build.0 = Release|Any CPU
56-
{007CA9E0-CDF0-4375-8E8C-A24C9A7BF531}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
57-
{007CA9E0-CDF0-4375-8E8C-A24C9A7BF531}.Debug|Any CPU.Build.0 = Debug|Any CPU
58-
{007CA9E0-CDF0-4375-8E8C-A24C9A7BF531}.Release|Any CPU.ActiveCfg = Release|Any CPU
59-
{007CA9E0-CDF0-4375-8E8C-A24C9A7BF531}.Release|Any CPU.Build.0 = Release|Any CPU
56+
{5A8427BC-0821-E973-7221-263D13156248}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
57+
{5A8427BC-0821-E973-7221-263D13156248}.Debug|Any CPU.Build.0 = Debug|Any CPU
58+
{5A8427BC-0821-E973-7221-263D13156248}.Release|Any CPU.ActiveCfg = Release|Any CPU
59+
{5A8427BC-0821-E973-7221-263D13156248}.Release|Any CPU.Build.0 = Release|Any CPU
60+
{8CC76F1F-5845-D81E-5E9A-113F913A444B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
61+
{8CC76F1F-5845-D81E-5E9A-113F913A444B}.Debug|Any CPU.Build.0 = Debug|Any CPU
62+
{8CC76F1F-5845-D81E-5E9A-113F913A444B}.Release|Any CPU.ActiveCfg = Release|Any CPU
63+
{8CC76F1F-5845-D81E-5E9A-113F913A444B}.Release|Any CPU.Build.0 = Release|Any CPU
64+
{69E31075-F14E-1DE2-1D6E-D934A5C0480F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
65+
{69E31075-F14E-1DE2-1D6E-D934A5C0480F}.Debug|Any CPU.Build.0 = Debug|Any CPU
66+
{69E31075-F14E-1DE2-1D6E-D934A5C0480F}.Release|Any CPU.ActiveCfg = Release|Any CPU
67+
{69E31075-F14E-1DE2-1D6E-D934A5C0480F}.Release|Any CPU.Build.0 = Release|Any CPU
68+
{9D56718F-C9E6-4C45-926D-97599072DA35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
69+
{9D56718F-C9E6-4C45-926D-97599072DA35}.Debug|Any CPU.Build.0 = Debug|Any CPU
70+
{9D56718F-C9E6-4C45-926D-97599072DA35}.Release|Any CPU.ActiveCfg = Release|Any CPU
71+
{9D56718F-C9E6-4C45-926D-97599072DA35}.Release|Any CPU.Build.0 = Release|Any CPU
6072
EndGlobalSection
6173
GlobalSection(SolutionProperties) = preSolution
6274
HideSolutionNode = FALSE
6375
EndGlobalSection
6476
GlobalSection(NestedProjects) = preSolution
6577
{18B3309D-B84C-453D-8EF7-16CA9E58F5DC} = {D3967ABF-F7C6-4432-9B67-A3F804CBC3E7}
66-
{BF3321A5-A590-44DD-BA5D-44978D9B6125} = {E36BF269-7F5D-4DE7-99B0-14567F9CD6B3}
6778
{323022D2-AAA7-443B-895C-77F5B1634D68} = {A8574DB9-8411-4F81-A82E-F97AD00EF8AF}
6879
{F7B35144-A00C-45BE-BC41-36C1B21FCD18} = {A8574DB9-8411-4F81-A82E-F97AD00EF8AF}
6980
{403B236B-D7D8-43FB-B47E-DCCAF8BA96C4} = {A8574DB9-8411-4F81-A82E-F97AD00EF8AF}
70-
{007CA9E0-CDF0-4375-8E8C-A24C9A7BF531} = {E36BF269-7F5D-4DE7-99B0-14567F9CD6B3}
81+
{5A8427BC-0821-E973-7221-263D13156248} = {A8574DB9-8411-4F81-A82E-F97AD00EF8AF}
82+
{8CC76F1F-5845-D81E-5E9A-113F913A444B} = {E36BF269-7F5D-4DE7-99B0-14567F9CD6B3}
83+
{69E31075-F14E-1DE2-1D6E-D934A5C0480F} = {E36BF269-7F5D-4DE7-99B0-14567F9CD6B3}
84+
{9D56718F-C9E6-4C45-926D-97599072DA35} = {A8574DB9-8411-4F81-A82E-F97AD00EF8AF}
7185
EndGlobalSection
7286
GlobalSection(ExtensibilityGlobals) = postSolution
7387
SolutionGuid = {E5E0FFF6-54C3-4BA1-91F3-EF3513A18D5D}

examples/HttpClientToCurl.Sample.InConsole/ApiCaller.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Text;
2+
using HttpClientToCurl.Extensions;
23

34
namespace HttpClientToCurl.Sample.InConsole;
45

examples/HttpClientToCurl.Sample.InFile/ApiCaller.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Text;
2+
using HttpClientToCurl.Extensions;
23

34
namespace HttpClientToCurl.Sample.InFile;
45

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System.Text;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace HttpClientToCurl.Sample.InGlobal.Controllers;
5+
6+
[ApiController]
7+
[Route("[controller]")]
8+
public class MyController(HttpClient httpClient) : ControllerBase
9+
{
10+
private readonly HttpClient _httpClient = httpClient;
11+
12+
[HttpGet]
13+
public async Task Send()
14+
{
15+
string apiUrl = "https://jsonplaceholder.typicode.com/posts";
16+
17+
try
18+
{
19+
// Create a sample JSON payload
20+
string jsonPayload =
21+
$"{{\"title\":\"New Post\",\"body\":\"This is the body of the new post\",\"userId\":1}}";
22+
23+
// Create HttpRequestMessage with the POST verb
24+
HttpRequestMessage request = new(HttpMethod.Post, apiUrl);
25+
26+
// Set up the request headers
27+
request.Headers.Add("Authorization", "Bearer YourAccessToken"); // Add any necessary headers
28+
29+
// Set the request content with the JSON payload
30+
request.Content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
31+
32+
// Log the curl command for debugging or testing.
33+
// This generates a curl command that can be imported into Postman.
34+
// Use it to check and compare against all the requirements.
35+
36+
// Send the request
37+
HttpResponseMessage response = await _httpClient.SendAsync(request);
38+
39+
// Check if the request was successful (status code 200-299)
40+
if (response.IsSuccessStatusCode)
41+
{
42+
// Read and print the response content as a string
43+
string responseBody = await response.Content.ReadAsStringAsync();
44+
Console.WriteLine("Response from the API:\n" + responseBody);
45+
}
46+
else
47+
{
48+
Console.WriteLine($"Error: {response.StatusCode} - {response.ReasonPhrase}");
49+
}
50+
}
51+
catch (Exception ex)
52+
{
53+
Console.WriteLine($"Exception: {ex.Message}");
54+
}
55+
}
56+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\..\src\HttpClientToCurl\HttpClientToCurl.csproj" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using static HttpClientToCurl.Extensions.ServiceCollectionExtensions;
2+
3+
var builder = WebApplication.CreateBuilder(args);
4+
builder.Services.AddControllers();
5+
6+
builder.Services.AddHttpClientToCurlInGeneralMode(builder.Configuration);
7+
builder.Services.AddHttpClient();
8+
9+
var app = builder.Build();
10+
11+
app.UseHttpsRedirection();
12+
app.UseAuthorization();
13+
app.MapControllers();
14+
app.Run();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"http": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": false,
8+
"applicationUrl": "http://localhost:5272",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development"
11+
}
12+
},
13+
"https": {
14+
"commandName": "Project",
15+
"dotnetRunMessages": true,
16+
"launchBrowser": false,
17+
"applicationUrl": "https://localhost:7038;http://localhost:5272",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
}
22+
}
23+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)